Skip to content

Commit

Permalink
🏁 adding parentheses around std::snprintf calls #1337
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Nov 9, 2018
1 parent f80efd3 commit da81e7b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions include/nlohmann/detail/input/binary_reader.hpp
Expand Up @@ -264,7 +264,7 @@ class binary_reader
default: // anything else not supported (yet)
{
char cr[3];
snprintf(cr, sizeof(cr), "%.2hhX", static_cast<unsigned char>(element_type));
(std::snprintf)(cr, sizeof(cr), "%.2hhX", static_cast<unsigned char>(element_type));
return sax->parse_error(element_type_parse_position, std::string(cr), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr)));
}
}
Expand Down Expand Up @@ -1921,7 +1921,7 @@ class binary_reader
std::string get_token_string() const
{
char cr[3];
snprintf(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
(std::snprintf)(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
return std::string{cr};
}

Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/input/lexer.hpp
Expand Up @@ -1360,7 +1360,7 @@ class lexer
{
// escape control characters
char cs[9];
snprintf(cs, 9, "<U+%.4X>", static_cast<unsigned char>(c));
(std::snprintf)(cs, 9, "<U+%.4X>", static_cast<unsigned char>(c));
result += cs;
}
else
Expand Down
16 changes: 8 additions & 8 deletions include/nlohmann/detail/output/serializer.hpp
Expand Up @@ -372,15 +372,15 @@ class serializer
{
if (codepoint <= 0xFFFF)
{
std::snprintf(string_buffer.data() + bytes, 7, "\\u%04x",
static_cast<uint16_t>(codepoint));
(std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
static_cast<uint16_t>(codepoint));
bytes += 6;
}
else
{
std::snprintf(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
static_cast<uint16_t>(0xD7C0 + (codepoint >> 10)),
static_cast<uint16_t>(0xDC00 + (codepoint & 0x3FF)));
(std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
static_cast<uint16_t>(0xD7C0 + (codepoint >> 10)),
static_cast<uint16_t>(0xDC00 + (codepoint & 0x3FF)));
bytes += 12;
}
}
Expand Down Expand Up @@ -416,7 +416,7 @@ class serializer
case error_handler_t::strict:
{
std::string sn(3, '\0');
snprintf(&sn[0], sn.size(), "%.2X", byte);
(std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn));
}

Expand Down Expand Up @@ -497,7 +497,7 @@ class serializer
case error_handler_t::strict:
{
std::string sn(3, '\0');
snprintf(&sn[0], sn.size(), "%.2X", static_cast<uint8_t>(s.back()));
(std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast<uint8_t>(s.back()));
JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn));
}

Expand Down Expand Up @@ -616,7 +616,7 @@ class serializer
static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;

// the actual conversion
std::ptrdiff_t len = snprintf(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);

// negative value indicates an error
assert(len > 0);
Expand Down
22 changes: 11 additions & 11 deletions single_include/nlohmann/json.hpp
Expand Up @@ -3812,7 +3812,7 @@ class lexer
{
// escape control characters
char cs[9];
snprintf(cs, 9, "<U+%.4X>", static_cast<unsigned char>(c));
(std::snprintf)(cs, 9, "<U+%.4X>", static_cast<unsigned char>(c));
result += cs;
}
else
Expand Down Expand Up @@ -6605,7 +6605,7 @@ class binary_reader
default: // anything else not supported (yet)
{
char cr[3];
snprintf(cr, sizeof(cr), "%.2hhX", static_cast<unsigned char>(element_type));
(std::snprintf)(cr, sizeof(cr), "%.2hhX", static_cast<unsigned char>(element_type));
return sax->parse_error(element_type_parse_position, std::string(cr), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr)));
}
}
Expand Down Expand Up @@ -8262,7 +8262,7 @@ class binary_reader
std::string get_token_string() const
{
char cr[3];
snprintf(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
(std::snprintf)(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
return std::string{cr};
}

Expand Down Expand Up @@ -11143,15 +11143,15 @@ class serializer
{
if (codepoint <= 0xFFFF)
{
std::snprintf(string_buffer.data() + bytes, 7, "\\u%04x",
static_cast<uint16_t>(codepoint));
(std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
static_cast<uint16_t>(codepoint));
bytes += 6;
}
else
{
std::snprintf(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
static_cast<uint16_t>(0xD7C0 + (codepoint >> 10)),
static_cast<uint16_t>(0xDC00 + (codepoint & 0x3FF)));
(std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
static_cast<uint16_t>(0xD7C0 + (codepoint >> 10)),
static_cast<uint16_t>(0xDC00 + (codepoint & 0x3FF)));
bytes += 12;
}
}
Expand Down Expand Up @@ -11187,7 +11187,7 @@ class serializer
case error_handler_t::strict:
{
std::string sn(3, '\0');
snprintf(&sn[0], sn.size(), "%.2X", byte);
(std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn));
}

Expand Down Expand Up @@ -11268,7 +11268,7 @@ class serializer
case error_handler_t::strict:
{
std::string sn(3, '\0');
snprintf(&sn[0], sn.size(), "%.2X", static_cast<uint8_t>(s.back()));
(std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast<uint8_t>(s.back()));
JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn));
}

Expand Down Expand Up @@ -11387,7 +11387,7 @@ class serializer
static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;

// the actual conversion
std::ptrdiff_t len = snprintf(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);

// negative value indicates an error
assert(len > 0);
Expand Down

0 comments on commit da81e7b

Please sign in to comment.