From 0e31e95217e477620cba456393368183dea55d75 Mon Sep 17 00:00:00 2001 From: Patrick Williams Date: Wed, 10 May 2023 19:40:27 -0500 Subject: multipart-parser: fix clang-tidy issues ``` ../include/multipart_parser.hpp:77:21: error: 'buffer' is an unsafe pointer used for buffer access [-Werror,-Wunsafe-buffer-usage] const char* buffer = req.body().data(); ../include/multipart_parser.hpp:246:38: error: 'buffer' is an unsafe pointer used for buffer access [-Werror,-Wunsafe-buffer-usage] void skipNonBoundary(const char* buffer, size_t len, size_t boundaryEnd, ``` Signed-off-by: Patrick Williams Change-Id: Iad3b4b241ec75a152e240755a307a970798079fb --- include/multipart_parser.hpp | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) (limited to 'include') diff --git a/include/multipart_parser.hpp b/include/multipart_parser.hpp index abc1657fcb..5ac196dd31 100644 --- a/include/multipart_parser.hpp +++ b/include/multipart_parser.hpp @@ -74,13 +74,12 @@ class MultipartParser lookbehind.resize(boundary.size() + 8); state = State::START; - const char* buffer = req.body().data(); - size_t len = req.body().size(); + const std::string& buffer = req.body(); + size_t len = buffer.size(); char cl = 0; for (size_t i = 0; i < len; i++) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) char c = buffer[i]; switch (state) { @@ -142,8 +141,7 @@ class MultipartParser return ParserError::ERROR_EMPTY_HEADER; } - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - currentHeaderName.append(buffer + headerFieldMark, + currentHeaderName.append(&buffer[headerFieldMark], i - headerFieldMark); state = State::HEADER_VALUE_START; break; @@ -165,8 +163,7 @@ class MultipartParser case State::HEADER_VALUE: if (c == cr) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - std::string_view value(buffer + headerValueMark, + std::string_view value(&buffer[headerValueMark], i - headerValueMark); mime_fields.rbegin()->fields.set(currentHeaderName, value); @@ -199,13 +196,11 @@ class MultipartParser { if (index == 0) { - skipNonBoundary(buffer, len, boundary.size() - 1, i); - - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) + skipNonBoundary(buffer, boundary.size() - 1, i); c = buffer[i]; } - const ParserError ec = processPartData(buffer, i, c); - if (ec != ParserError::PARSER_SUCCESS) + if (auto ec = processPartData(buffer, i, c); + ec != ParserError::PARSER_SUCCESS) { return ec; } @@ -246,13 +241,12 @@ class MultipartParser return boundaryIndex[static_cast(c)]; } - void skipNonBoundary(const char* buffer, size_t len, size_t boundaryEnd, + void skipNonBoundary(const std::string& buffer, size_t boundaryEnd, size_t& i) { // boyer-moore derived algorithm to safely skip non-boundary data - while (i + boundary.size() <= len) + while (i + boundary.size() <= buffer.length()) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) if (isBoundaryChar(buffer[i + boundaryEnd])) { break; @@ -261,7 +255,7 @@ class MultipartParser } } - ParserError processPartData(const char* buffer, size_t& i, char c) + ParserError processPartData(const std::string& buffer, size_t& i, char c) { size_t prevIndex = index; @@ -271,8 +265,7 @@ class MultipartParser { if (index == 0) { - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) - const char* start = buffer + partDataMark; + const char* start = &buffer[partDataMark]; size_t size = i - partDataMark; mime_fields.rbegin()->content += std::string_view(start, size); -- cgit v1.2.3