Skip to content

Commit

Permalink
Enable -Wshadow warnings (#902)
Browse files Browse the repository at this point in the history
* Enable shadow warning and fix instances
* Json escape define lambda once
* Multipart use members directly and not via reference
* Clang-format: run files trough clang-format-17
* Fix shadowing variable spotted by clang
  • Loading branch information
fliiiix authored Sep 29, 2024
1 parent b8ddff6 commit 2583c27
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 123 deletions.
3 changes: 3 additions & 0 deletions cmake/compiler_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function(add_warnings_optimizations target_name)
-Wextra
-Wpedantic
-Wsuggest-override
-Wshadow
$<$<CONFIG:RELEASE>:-O2>
$<$<CONFIG:DEBUG>:-O0 -g>
)
Expand All @@ -31,6 +32,7 @@ function(add_warnings_optimizations target_name)
-Wextra
-Wpedantic
-Wsuggest-override
-Wshadow
$<$<CONFIG:RELEASE>:-O2>
$<$<CONFIG:DEBUG>:-O0 -g -pg>
)
Expand All @@ -41,6 +43,7 @@ function(add_warnings_optimizations target_name)
-Wextra
-Wpedantic
-Wsuggest-override
-Wshadow
$<$<CONFIG:RELEASE>:-O2>
$<$<CONFIG:DEBUG>:-O0 -g -p -pg>
)
Expand Down
11 changes: 5 additions & 6 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace crow

/// An HTTP connection.
template<typename Adaptor, typename Handler, typename... Middlewares>
class Connection: public std::enable_shared_from_this<Connection<Adaptor, Handler, Middlewares...>>
class Connection : public std::enable_shared_from_this<Connection<Adaptor, Handler, Middlewares...>>
{
friend struct crow::response;

Expand Down Expand Up @@ -194,7 +194,6 @@ namespace crow

if (!res.completed_)
{
auto self = this->shared_from_this();
res.complete_request_handler_ = [self] {
self->complete_request();
};
Expand Down Expand Up @@ -456,12 +455,12 @@ namespace crow
if (res.body.length() > 0)
{
std::vector<asio::const_buffer> buffers{1};
const uint8_t *data = reinterpret_cast<const uint8_t*>(res.body.data());
const uint8_t* data = reinterpret_cast<const uint8_t*>(res.body.data());
size_t length = res.body.length();
for(size_t transferred = 0; transferred < length;)
for (size_t transferred = 0; transferred < length;)
{
size_t to_transfer = CROW_MIN(16384UL, length-transferred);
buffers[0] = asio::const_buffer(data+transferred, to_transfer);
size_t to_transfer = CROW_MIN(16384UL, length - transferred);
buffers[0] = asio::const_buffer(data + transferred, to_transfer);
do_write_sync(buffers);
transferred += to_transfer;
}
Expand Down
4 changes: 2 additions & 2 deletions include/crow/http_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
{}

/// Construct a request with all values assigned.
request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body, unsigned char http_major, unsigned char http_minor, bool has_keep_alive, bool has_close_connection, bool is_upgrade):
method(method), raw_url(std::move(raw_url)), url(std::move(url)), url_params(std::move(url_params)), headers(std::move(headers)), body(std::move(body)), http_ver_major(http_major), http_ver_minor(http_minor), keep_alive(has_keep_alive), close_connection(has_close_connection), upgrade(is_upgrade)
request(HTTPMethod method_, std::string raw_url_, std::string url_, query_string url_params_, ci_map headers_, std::string body_, unsigned char http_major, unsigned char http_minor, bool has_keep_alive, bool has_close_connection, bool is_upgrade):
method(method_), raw_url(std::move(raw_url_)), url(std::move(url_)), url_params(std::move(url_params_)), headers(std::move(headers_)), body(std::move(body_)), http_ver_major(http_major), http_ver_minor(http_minor), keep_alive(has_keep_alive), close_connection(has_close_connection), upgrade(is_upgrade)
{}

void add_header(std::string key, std::string value)
Expand Down
24 changes: 12 additions & 12 deletions include/crow/http_response.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#endif
#include <sys/stat.h>
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif

#include "crow/http_request.h"
Expand Down Expand Up @@ -118,9 +118,9 @@ namespace crow

// clang-format off
response() {}
explicit response(int code) : code(code) {}
response(std::string body) : body(std::move(body)) {}
response(int code, std::string body) : code(code), body(std::move(body)) {}
explicit response(int code_) : code(code_) {}
response(std::string body_) : body(std::move(body_)) {}
response(int code_, std::string body_) : code(code_), body(std::move(body_)) {}
// clang-format on
response(returnable&& value)
{
Expand All @@ -132,14 +132,14 @@ namespace crow
body = value.dump();
set_header("Content-Type", value.content_type);
}
response(int code, returnable& value):
code(code)
response(int code_, returnable& value):
code(code_)
{
body = value.dump();
set_header("Content-Type", value.content_type);
}
response(int code, returnable&& value):
code(code), body(value.dump())
response(int code_, returnable&& value):
code(code_), body(value.dump())
{
set_header("Content-Type", std::move(value.content_type));
}
Expand All @@ -149,14 +149,14 @@ namespace crow
*this = std::move(r);
}

response(std::string contentType, std::string body):
body(std::move(body))
response(std::string contentType, std::string body_):
body(std::move(body_))
{
set_header("Content-Type", get_mime_type(contentType));
}

response(int code, std::string contentType, std::string body):
code(code), body(std::move(body))
response(int code_, std::string contentType, std::string body_):
code(code_), body(std::move(body_))
{
set_header("Content-Type", get_mime_type(contentType));
}
Expand Down
29 changes: 16 additions & 13 deletions include/crow/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ namespace crow // NOTE: Already documented in "crow/app.h"

namespace json
{
static inline char to_hex(char c)
{
c = c & 0xf;
if (c < 10)
return '0' + c;
return 'a' + c - 10;
};

inline void escape(const std::string& str, std::string& ret)
{
ret.reserve(ret.size() + str.size() + str.size() / 4);
Expand All @@ -52,12 +60,6 @@ namespace crow // NOTE: Already documented in "crow/app.h"
if (c >= 0 && c < 0x20)
{
ret += "\\u00";
auto to_hex = [](char c) {
c = c & 0xf;
if (c < 10)
return '0' + c;
return 'a' + c - 10;
};
ret += to_hex(c / 16);
ret += to_hex(c % 16);
}
Expand Down Expand Up @@ -878,8 +880,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
//static const char* escaped = "\"\\/\b\f\n\r\t";
struct Parser
{
Parser(char* data, size_t /*size*/):
data(data)
Parser(char* data_, size_t /*size*/):
data(data_)
{
}

Expand Down Expand Up @@ -1887,7 +1889,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
snprintf(outbuf, sizeof(outbuf), "%f", v.num.d);
#endif
}
char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0
char* p = &outbuf[0];
char* pos_first_trailing_0 = nullptr;
f_state = start;
while (*p != '\0')
{
Expand All @@ -1909,22 +1912,22 @@ namespace crow // NOTE: Already documented in "crow/app.h"
if (ch == '0')
{
f_state = zero;
o = p;
pos_first_trailing_0 = p;
}
p++;
break;
case zero: // if a non 0 is found (e.g. 1.00004) remove the earlier recorded 0 position and look for more trailing 0s
if (ch != '0')
{
o = nullptr;
pos_first_trailing_0 = nullptr;
f_state = decp;
}
p++;
break;
}
}
if (o != nullptr) // if any trailing 0s are found, terminate the string where they begin
*o = '\0';
if (pos_first_trailing_0 != nullptr) // if any trailing 0s are found, terminate the string where they begin
*pos_first_trailing_0 = '\0';
out += outbuf;
}
else if (v.nt == num_type::Signed_integer)
Expand Down
8 changes: 4 additions & 4 deletions include/crow/middleware.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
template<>
struct middleware_call_criteria_dynamic<false>
{
middleware_call_criteria_dynamic(const std::vector<int>& indices):
indices(indices), slider(0) {}
middleware_call_criteria_dynamic(const std::vector<int>& indices_):
indices(indices_), slider(0) {}

template<typename>
bool enabled(int mw_index) const
Expand All @@ -308,8 +308,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
template<>
struct middleware_call_criteria_dynamic<true>
{
middleware_call_criteria_dynamic(const std::vector<int>& indices):
indices(indices), slider(int(indices.size()) - 1) {}
middleware_call_criteria_dynamic(const std::vector<int>& indices_):
indices(indices_), slider(int(indices_.size()) - 1) {}

template<typename>
bool enabled(int mw_index) const
Expand Down
33 changes: 16 additions & 17 deletions include/crow/multipart.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ namespace crow
}

/// Default constructor using default values
message(const ci_map& headers, const std::string& boundary, const std::vector<part>& sections):
returnable("multipart/form-data; boundary=CROW-BOUNDARY"), headers(headers), boundary(boundary), parts(sections)
message(const ci_map& headers_, const std::string& boundary_, const std::vector<part>& sections):
returnable("multipart/form-data; boundary=CROW-BOUNDARY"), headers(headers_), boundary(boundary_), parts(sections)
{
if (!boundary.empty())
content_type = "multipart/form-data; boundary=" + boundary;
Expand All @@ -148,7 +148,7 @@ namespace crow
{
if (!boundary.empty())
content_type = "multipart/form-data; boundary=" + boundary;
parse_body(req.body, parts, part_map);
parse_body(req.body);
}

private:
Expand All @@ -168,9 +168,8 @@ namespace crow
return std::string();
}

void parse_body(std::string body, std::vector<part>& sections, mp_map& part_map)
void parse_body(std::string body)
{

std::string delimiter = dd + boundary;

// TODO(EDev): Exit on error
Expand All @@ -193,7 +192,7 @@ namespace crow
part_map.emplace(
(get_header_object(parsed_section.headers, "Content-Disposition").params.find("name")->second),
parsed_section);
sections.push_back(std::move(parsed_section));
parts.push_back(std::move(parsed_section));
}
}
}
Expand All @@ -217,17 +216,17 @@ namespace crow
{
header to_add;

size_t found = lines.find(crlf);
std::string line = lines.substr(0, found);
const size_t found_crlf = lines.find(crlf);
std::string line = lines.substr(0, found_crlf);
std::string key;
lines.erase(0, found + 2);
lines.erase(0, found_crlf + 2);
// Add the header if available
if (!line.empty())
{
size_t found = line.find("; ");
std::string header = line.substr(0, found);
if (found != std::string::npos)
line.erase(0, found + 2);
const size_t found_semicolon = line.find("; ");
std::string header = line.substr(0, found_semicolon);
if (found_semicolon != std::string::npos)
line.erase(0, found_semicolon + 2);
else
line = std::string();

Expand All @@ -240,10 +239,10 @@ namespace crow
// Add the parameters
while (!line.empty())
{
size_t found = line.find("; ");
std::string param = line.substr(0, found);
if (found != std::string::npos)
line.erase(0, found + 2);
const size_t found_semicolon = line.find("; ");
std::string param = line.substr(0, found_semicolon);
if (found_semicolon != std::string::npos)
line.erase(0, found_semicolon + 2);
else
line = std::string();

Expand Down
Loading

0 comments on commit 2583c27

Please sign in to comment.