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 <patrick@stwcx.xyz>
Change-Id: Iad3b4b241ec75a152e240755a307a970798079fb
diff --git a/include/multipart_parser.hpp b/include/multipart_parser.hpp
index abc1657..5ac196d 100644
--- a/include/multipart_parser.hpp
+++ b/include/multipart_parser.hpp
@@ -74,13 +74,12 @@
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 @@
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 @@
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 @@
{
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 @@
return boundaryIndex[static_cast<unsigned char>(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 @@
}
}
- 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 @@
{
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);