Skip to content

Commit 4e820f9

Browse files
authored
Apply warnings/suggestions reported by clang-tidy v9 (#67)
Clang-tidy version 9 reported a few warnings, and since we treat errors as warnings we better address them. I've disabled the trailing-return-type suggestion because I believe it's ridiculous. Signed-off-by: Marco Magdy <[email protected]>
1 parent a31cc25 commit 4e820f9

File tree

5 files changed

+50
-24
lines changed

5 files changed

+50
-24
lines changed

.clang-tidy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
Checks: 'clang-diagnostic-*,clang-analyzer-*,performance-*,readability-*,modernize-*,bugprone-*,misc-*'
2+
Checks:
3+
'clang-diagnostic-*,clang-analyzer-*,performance-*,readability-*,modernize-*,bugprone-*,misc-*,-modernize-use-trailing-return-type'
34
WarningsAsErrors: '*'
45
HeaderFilterRegex: ''
56
FormatStyle: 'none'
@@ -10,6 +11,7 @@ CheckOptions:
1011
value: '1'
1112
- key: readability-implicit-bool-conversion.AllowIntegerConditions
1213
value: '1'
13-
14+
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
15+
value: '1'
1416

1517
...

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ build
22
tags
33
TODO
44
compile_commands.json
5+
.clangd

include/aws/lambda-runtime/outcome.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <cassert>
18+
#include <utility>
1819

1920
namespace aws {
2021
namespace lambda_runtime {
@@ -26,7 +27,7 @@ class outcome {
2627

2728
outcome(TFailure const& f) : f(f), success(false) {}
2829

29-
outcome(outcome&& other) : success(other.success)
30+
outcome(outcome&& other) noexcept : success(other.success)
3031
{
3132
if (success) {
3233
s = std::move(other.s);

src/logging.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* permissions and limitations under the License.
1414
*/
1515
#include "aws/logging/logging.h"
16+
#include <array>
1617
#include <cstdio>
1718
#include <chrono>
1819

@@ -46,9 +47,10 @@ void log(verbosity v, char const* tag, char const* msg, va_list args)
4647
va_end(copy);
4748
return;
4849
}
49-
char buf[512];
50-
char* out = buf;
51-
if (sz >= static_cast<int>(sizeof(buf))) {
50+
constexpr int max_stack_buffer_size = 512;
51+
std::array<char, max_stack_buffer_size> buf;
52+
char* out = buf.data();
53+
if (sz >= max_stack_buffer_size) {
5254
out = new char[sz];
5355
}
5456

@@ -60,7 +62,7 @@ void log(verbosity v, char const* tag, char const* msg, va_list args)
6062
// stdout is not line-buffered when redirected (for example to a file or to another process) so we must flush it
6163
// manually.
6264
fflush(stdout);
63-
if (out != buf) {
65+
if (out != buf.data()) {
6466
delete[] out;
6567
}
6668
}

src/runtime.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
namespace aws {
3434
namespace lambda_runtime {
3535

36-
static char const LOG_TAG[] = "LAMBDA_RUNTIME";
37-
static char const REQUEST_ID_HEADER[] = "lambda-runtime-aws-request-id";
38-
static char const TRACE_ID_HEADER[] = "lambda-runtime-trace-id";
39-
static char const CLIENT_CONTEXT_HEADER[] = "lambda-runtime-client-context";
40-
static char const COGNITO_IDENTITY_HEADER[] = "lambda-runtime-cognito-identity";
41-
static char const DEADLINE_MS_HEADER[] = "lambda-runtime-deadline-ms";
42-
static char const FUNCTION_ARN_HEADER[] = "lambda-runtime-invoked-function-arn";
36+
static constexpr auto LOG_TAG = "LAMBDA_RUNTIME";
37+
static constexpr auto REQUEST_ID_HEADER = "lambda-runtime-aws-request-id";
38+
static constexpr auto TRACE_ID_HEADER = "lambda-runtime-trace-id";
39+
static constexpr auto CLIENT_CONTEXT_HEADER = "lambda-runtime-client-context";
40+
static constexpr auto COGNITO_IDENTITY_HEADER = "lambda-runtime-cognito-identity";
41+
static constexpr auto DEADLINE_MS_HEADER = "lambda-runtime-deadline-ms";
42+
static constexpr auto FUNCTION_ARN_HEADER = "lambda-runtime-invoked-function-arn";
4343

4444
enum Endpoints {
4545
INIT,
@@ -49,8 +49,10 @@ enum Endpoints {
4949

5050
static bool is_success(aws::http::response_code httpcode)
5151
{
52+
constexpr auto http_first_success_error_code = 200;
53+
constexpr auto http_last_success_error_code = 299;
5254
auto const code = static_cast<int>(httpcode);
53-
return code >= 200 && code <= 299;
55+
return code >= http_first_success_error_code && code <= http_last_success_error_code;
5456
}
5557

5658
static size_t write_data(char* ptr, size_t size, size_t nmemb, void* userdata)
@@ -67,13 +69,28 @@ static size_t write_data(char* ptr, size_t size, size_t nmemb, void* userdata)
6769
return nmemb;
6870
}
6971

72+
// std::isspace has a few edge cases that would trigger UB. In particular, the documentation says:
73+
// "The behavior is undefined if the value of the input is not representable as unsigned char and is not equal to EOF."
74+
// So, this function does the simple obvious thing instead.
7075
static inline bool IsSpace(int ch)
7176
{
72-
if (ch < -1 || ch > 255) {
73-
return false;
77+
constexpr int space = 0x20; // space (0x20, ' ')
78+
constexpr int form_feed = 0x0c; // form feed (0x0c, '\f')
79+
constexpr int line_feed = 0x0a; // line feed (0x0a, '\n')
80+
constexpr int carriage_return = 0x0d; // carriage return (0x0d, '\r')
81+
constexpr int horizontal_tab = 0x09; // horizontal tab (0x09, '\t')
82+
constexpr int vertical_tab = 0x0b; // vertical tab (0x0b, '\v')
83+
switch (ch) {
84+
case space:
85+
case form_feed:
86+
case line_feed:
87+
case carriage_return:
88+
case horizontal_tab:
89+
case vertical_tab:
90+
return true;
91+
default:
92+
return false;
7493
}
75-
76-
return ::isspace(ch) != 0;
7794
}
7895

7996
static inline std::string trim(std::string s)
@@ -276,7 +293,8 @@ runtime::next_outcome runtime::get_next()
276293

277294
if (resp.has_header(DEADLINE_MS_HEADER)) {
278295
auto const& deadline_string = resp.get_header(DEADLINE_MS_HEADER);
279-
unsigned long ms = strtoul(deadline_string.c_str(), nullptr, 10);
296+
constexpr int base = 10;
297+
unsigned long ms = strtoul(deadline_string.c_str(), nullptr, base);
280298
assert(ms > 0);
281299
assert(ms < ULONG_MAX);
282300
req.deadline += std::chrono::milliseconds(ms);
@@ -437,10 +455,11 @@ void run_handler(std::function<invocation_response(invocation_request const&)> c
437455

438456
static std::string json_escape(std::string const& in)
439457
{
458+
constexpr char last_non_printable_character = 31;
440459
std::string out;
441460
out.reserve(in.length()); // most strings will end up identical
442461
for (char ch : in) {
443-
if (ch > 31 && ch != '\"' && ch != '\\') {
462+
if (ch > last_non_printable_character && ch != '\"' && ch != '\\') {
444463
out.append(1, ch);
445464
}
446465
else {
@@ -469,9 +488,10 @@ static std::string json_escape(std::string const& in)
469488
break;
470489
default:
471490
// escape and print as unicode codepoint
472-
char buf[6]; // 4 hex + letter 'u' + \0
473-
sprintf(buf, "u%04x", ch);
474-
out.append(buf, 5); // add only five, discarding the null terminator.
491+
constexpr int printed_unicode_length = 6; // 4 hex + letter 'u' + \0
492+
std::array<char, printed_unicode_length> buf;
493+
sprintf(buf.data(), "u%04x", ch);
494+
out.append(buf.data(), buf.size() - 1); // add only five, discarding the null terminator.
475495
break;
476496
}
477497
}

0 commit comments

Comments
 (0)