Skip to content

Commit 95146d7

Browse files
author
Andrei Popescu
authored
First batch of SonarCube reported issues fixed. (#1287)
This commit fixes some of SonarCube reported issues with major and minor levels including: - Effective move of passed by value parameters. - Moved ougth to be static methods to anonymous namespace. - Pass parameters as reference if they are not copied or moved. - Initialize variables and members before usage. - Add check for out-of-bound array access. Relates-To: OAM-1366 Signed-off-by: Andrei Popescu <[email protected]>
1 parent 732bcf9 commit 95146d7

File tree

20 files changed

+112
-131
lines changed

20 files changed

+112
-131
lines changed

olp-cpp-sdk-authentication/include/olp/authentication/UserAccountInfoResponse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class AUTHENTICATION_API UserAccountInfoResponse {
215215
* @param phone_number The phone number.
216216
*/
217217
void SetPhoneNumber(std::string phone_number) {
218-
phone_number_ = phone_number;
218+
phone_number_ = std::move(phone_number);
219219
}
220220

221221
/**

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ client::OlpClient::RequestBodyType GenerateAppleSignInBody(
140140
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
141141
}
142142

143+
std::string GenerateBearerHeader(const std::string& bearer_token) {
144+
return std::string(http::kBearer + std::string(" ") + bearer_token);
145+
}
146+
147+
client::HttpResponse CallApi(const client::OlpClient& client,
148+
const std::string& endpoint,
149+
client::CancellationContext context,
150+
const std::string& auth_header,
151+
client::OlpClient::RequestBodyType body) {
152+
client::OlpClient::ParametersType headers{
153+
{http::kAuthorizationHeader, auth_header}};
154+
155+
return client.CallApi(endpoint, "POST", {}, std::move(headers), {},
156+
std::move(body), kApplicationJson, std::move(context));
157+
}
158+
143159
} // namespace
144160

145161
AuthenticationClientImpl::RequestTimer::RequestTimer()
@@ -187,17 +203,6 @@ olp::client::HttpResponse AuthenticationClientImpl::CallAuth(
187203
std::move(body), kApplicationJson, std::move(context));
188204
}
189205

190-
olp::client::HttpResponse AuthenticationClientImpl::CallAuth(
191-
const client::OlpClient& client, const std::string& endpoint,
192-
client::CancellationContext context, const std::string& auth_header,
193-
client::OlpClient::RequestBodyType body) {
194-
client::OlpClient::ParametersType headers{
195-
{http::kAuthorizationHeader, auth_header}};
196-
197-
return client.CallApi(endpoint, "POST", {}, std::move(headers), {},
198-
std::move(body), kApplicationJson, std::move(context));
199-
}
200-
201206
SignInResult AuthenticationClientImpl::ParseAuthResponse(
202207
int status, std::stringstream& auth_response) {
203208
auto document = std::make_shared<rapidjson::Document>();
@@ -435,8 +440,8 @@ client::CancellationToken AuthenticationClientImpl::SignInApple(
435440

436441
client::OlpClient client = CreateOlpClient(settings_, boost::none);
437442

438-
auto auth_response = CallAuth(client, kOauthEndpoint, context,
439-
properties.GetAccessToken(), request_body);
443+
auto auth_response = CallApi(client, kOauthEndpoint, context,
444+
properties.GetAccessToken(), request_body);
440445

441446
auto status = auth_response.GetStatus();
442447
if (status < 0) {
@@ -837,13 +842,6 @@ client::CancellationToken AuthenticationClientImpl::GetMyAccount(
837842
std::move(callback));
838843
}
839844

840-
std::string AuthenticationClientImpl::GenerateBearerHeader(
841-
const std::string& bearer_token) {
842-
std::string authorization = http::kBearer + std::string(" ");
843-
authorization += bearer_token;
844-
return authorization;
845-
}
846-
847845
client::OlpClient::RequestBodyType AuthenticationClientImpl::GenerateClientBody(
848846
const SignInProperties& properties) {
849847
rapidjson::StringBuffer data;
@@ -1041,7 +1039,8 @@ AuthenticationClientImpl::GenerateAcceptTermBody(
10411039
}
10421040

10431041
client::OlpClient::RequestBodyType
1044-
AuthenticationClientImpl::GenerateAuthorizeBody(AuthorizeRequest properties) {
1042+
AuthenticationClientImpl::GenerateAuthorizeBody(
1043+
const AuthorizeRequest& properties) {
10451044
rapidjson::StringBuffer data;
10461045
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
10471046
writer.StartObject();

olp-cpp-sdk-authentication/src/AuthenticationClientImpl.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ class AuthenticationClientImpl {
144144

145145
static TimeResponse ParseTimeResponse(std::stringstream& payload);
146146

147-
std::string GenerateBearerHeader(const std::string& bearer_token);
148-
149147
client::OlpClient::RequestBodyType GenerateClientBody(
150148
const SignInProperties& properties);
151149
client::OlpClient::RequestBodyType GenerateUserBody(
@@ -159,20 +157,14 @@ class AuthenticationClientImpl {
159157
client::OlpClient::RequestBodyType GenerateAcceptTermBody(
160158
const std::string& reacceptance_token);
161159
client::OlpClient::RequestBodyType GenerateAuthorizeBody(
162-
AuthorizeRequest properties);
160+
const AuthorizeRequest& properties);
163161

164162
virtual olp::client::HttpResponse CallAuth(
165163
const client::OlpClient& client, const std::string& endpoint,
166164
client::CancellationContext context,
167165
const AuthenticationCredentials& credentials,
168166
client::OlpClient::RequestBodyType body, std::time_t timestamp);
169167

170-
olp::client::HttpResponse CallAuth(const client::OlpClient& client,
171-
const std::string& endpoint,
172-
client::CancellationContext context,
173-
const std::string& auth_header,
174-
client::OlpClient::RequestBodyType body);
175-
176168
SignInResult ParseAuthResponse(int status, std::stringstream& auth_response);
177169

178170
SignInUserResult ParseUserAuthResponse(int status,

olp-cpp-sdk-authentication/src/ResponseFromJsonBuilder.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,12 +110,11 @@ class ResponseFromJsonBuilder {
110110

111111
// in the ideal scenario all fields should be processed
112112
for (const auto& field : fields_) {
113-
OLP_SDK_LOG_WARNING_F(kLogTag,
114-
"Absent value, response=%s, field=%s",
113+
OLP_SDK_LOG_WARNING_F(kLogTag, "Absent value, response=%s, field=%s",
115114
kTargetTypeName.c_str(), field.first.c_str());
116115
}
117116

118-
return std::move(result);
117+
return result;
119118
}
120119

121120
private:

olp-cpp-sdk-authentication/src/SignInResultImpl.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ SignInResultImpl::SignInResultImpl() noexcept
3838
SignInResultImpl::SignInResultImpl(
3939
int status, std::string error,
4040
std::shared_ptr<rapidjson::Document> json_document) noexcept
41-
: BaseResult(status, error, json_document), expiry_time_(), expires_in_() {
41+
: BaseResult(status, std::move(error), json_document),
42+
expiry_time_(),
43+
expires_in_() {
4244
is_valid_ = this->BaseResult::IsValid() &&
4345
json_document->HasMember(Constants::ACCESS_TOKEN) &&
4446
json_document->HasMember(kTokenType) &&

olp-cpp-sdk-authentication/src/SignInUserResultImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ SignInUserResultImpl::SignInUserResultImpl() noexcept
4141
SignInUserResultImpl::SignInUserResultImpl(
4242
int status, std::string error,
4343
std::shared_ptr<rapidjson::Document> json_document) noexcept
44-
: SignInResultImpl(status, error, json_document) {
44+
: SignInResultImpl(status, std::move(error), json_document) {
4545
if (BaseResult::IsValid()) {
4646
if (json_document->HasMember(kTermsReacceptanceToken))
4747
term_acceptance_token_ =

olp-cpp-sdk-authentication/src/SignOutResultImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ SignOutResultImpl::SignOutResultImpl() noexcept
3232
SignOutResultImpl::SignOutResultImpl(
3333
int status, std::string error,
3434
std::shared_ptr<rapidjson::Document> json_document) noexcept
35-
: BaseResult(status, error, json_document) {}
35+
: BaseResult(status, std::move(error), json_document) {}
3636

3737
SignOutResultImpl::~SignOutResultImpl() = default;
3838

olp-cpp-sdk-authentication/src/SignUpResultImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2021 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ SignUpResultImpl::SignUpResultImpl() noexcept
3636
SignUpResultImpl::SignUpResultImpl(
3737
int status, std::string error,
3838
std::shared_ptr<rapidjson::Document> json_document) noexcept
39-
: BaseResult(status, error, json_document) {
39+
: BaseResult(status, std::move(error), json_document) {
4040
if (BaseResult::IsValid()) {
4141
if (json_document->HasMember(kUserId))
4242
user_identifier_ = (*json_document)[kUserId].GetString();

olp-cpp-sdk-authentication/src/TokenEndpointImpl.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ void RetryDelay(const client::RetrySettings& retry_settings, size_t retry) {
9696
retry));
9797
}
9898

99-
TimeResponse ParseTimeResponse(std::stringstream& payload) {
99+
TimeResponse ParseTimeResponse(const std::string& payload) {
100100
rapidjson::Document document;
101-
rapidjson::IStreamWrapper stream(payload);
102-
document.ParseStream(stream);
101+
document.Parse(payload.c_str());
103102

104103
if (!document.IsObject()) {
105104
return AuthenticationError(client::ErrorCode::InternalFailure,
@@ -143,6 +142,35 @@ client::OlpClient::RequestBodyType GenerateClientBody(
143142
auto content = data.GetString();
144143
return std::make_shared<RequestBodyData>(content, content + data.GetSize());
145144
}
145+
146+
TimeResponse GetTimeFromServer(client::CancellationContext& context,
147+
const client::OlpClient& client) {
148+
auto http_result = client.CallApi(kTimestampEndpoint, "GET", {}, {}, {},
149+
nullptr, {}, context);
150+
151+
std::string response;
152+
http_result.GetResponse(response);
153+
154+
if (http_result.status != http::HttpStatusCode::OK) {
155+
OLP_SDK_LOG_WARNING_F(
156+
kLogTag, "Failed to get time from server, status=%d, response='%s'",
157+
http_result.GetStatus(), response.c_str());
158+
159+
return AuthenticationError(http_result.GetStatus(), std::move(response));
160+
}
161+
162+
auto server_time = ParseTimeResponse(response);
163+
if (!server_time) {
164+
const auto& error = server_time.GetError();
165+
const auto& message = error.GetMessage();
166+
OLP_SDK_LOG_WARNING_F(kLogTag,
167+
"Failed to decode time from server, message='%s'",
168+
message.c_str());
169+
}
170+
171+
return server_time;
172+
}
173+
146174
} // namespace
147175

148176
TokenEndpointImpl::TokenEndpointImpl(Settings settings)
@@ -328,33 +356,6 @@ TokenEndpointImpl::RequestTimer TokenEndpointImpl::CreateRequestTimer(
328356
return RequestTimer(server_time.GetResult());
329357
}
330358

331-
TimeResponse TokenEndpointImpl::GetTimeFromServer(
332-
client::CancellationContext& context,
333-
const client::OlpClient& client) const {
334-
auto http_result = client.CallApi(kTimestampEndpoint, "GET", {}, {}, {},
335-
nullptr, {}, context);
336-
337-
if (http_result.status != http::HttpStatusCode::OK) {
338-
auto response = http_result.response.str();
339-
OLP_SDK_LOG_WARNING_F(
340-
kLogTag, "Failed to get time from server, status=%d, response='%s'",
341-
http_result.status, response.c_str());
342-
return AuthenticationError(http_result.status, http_result.response.str());
343-
}
344-
345-
auto server_time = ParseTimeResponse(http_result.response);
346-
347-
if (!server_time) {
348-
const auto& error = server_time.GetError();
349-
const auto& message = error.GetMessage();
350-
OLP_SDK_LOG_WARNING_F(kLogTag,
351-
"Failed to decode time from server, message='%s'",
352-
message.c_str());
353-
}
354-
355-
return server_time;
356-
}
357-
358359
TokenEndpointImpl::RequestTimer::RequestTimer()
359360
: timer_start_{std::chrono::steady_clock::now()},
360361
time_{std::chrono::system_clock::to_time_t(

olp-cpp-sdk-authentication/src/TokenEndpointImpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ class TokenEndpointImpl {
8686
RequestTimer CreateRequestTimer(const client::OlpClient& client,
8787
client::CancellationContext& context) const;
8888

89-
Response<time_t> GetTimeFromServer(client::CancellationContext& context,
90-
const client::OlpClient& client) const;
91-
9289
const AuthenticationCredentials credentials_;
9390
const AuthenticationSettings settings_;
9491
AuthenticationClient auth_client_;

0 commit comments

Comments
 (0)