Skip to content

Commit 72b20eb

Browse files
committed
Reading binary number from wchar as an error, address warnings
1 parent be2eb53 commit 72b20eb

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

include/nlohmann/detail/input/input_adapters.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ class iterator_input_adapter
180180
template<class T>
181181
std::size_t get_elements(T* dest, std::size_t count = 1)
182182
{
183-
auto* ptr = reinterpret_cast<unsigned char*>(dest);
183+
auto* ptr = reinterpret_cast<char*>(dest);
184184
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
185185
{
186186
if (JSON_HEDLEY_LIKELY(current != end))
187187
{
188-
ptr[read_index] = *current;
188+
ptr[read_index] = static_cast<char>(*current);
189189
std::advance(current, 1);
190190
}
191191
else
@@ -359,15 +359,11 @@ class wide_string_input_adapter
359359
return utf8_bytes[utf8_bytes_index++];
360360
}
361361

362+
// parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
362363
template<class T>
363364
std::size_t get_elements(T* dest, std::size_t count = 1)
364365
{
365-
auto* ptr = reinterpret_cast<char*>(dest);
366-
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
367-
{
368-
ptr[read_index] = static_cast<char>(get_character());
369-
}
370-
return count * sizeof(T);
366+
JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
371367
}
372368

373369
private:

single_include/nlohmann/json.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6413,12 +6413,12 @@ class iterator_input_adapter
64136413
template<class T>
64146414
std::size_t get_elements(T* dest, std::size_t count = 1)
64156415
{
6416-
auto* ptr = reinterpret_cast<unsigned char*>(dest);
6416+
auto* ptr = reinterpret_cast<char*>(dest);
64176417
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
64186418
{
64196419
if (JSON_HEDLEY_LIKELY(current != end))
64206420
{
6421-
ptr[read_index] = *current;
6421+
ptr[read_index] = static_cast<char>(*current);
64226422
std::advance(current, 1);
64236423
}
64246424
else
@@ -6592,15 +6592,11 @@ class wide_string_input_adapter
65926592
return utf8_bytes[utf8_bytes_index++];
65936593
}
65946594

6595+
// parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
65956596
template<class T>
65966597
std::size_t get_elements(T* dest, std::size_t count = 1)
65976598
{
6598-
auto* ptr = reinterpret_cast<char*>(dest);
6599-
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
6600-
{
6601-
ptr[read_index] = static_cast<char>(get_character());
6602-
}
6603-
return count * sizeof(T);
6599+
JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
66046600
}
66056601

66066602
private:

tests/src/unit-msgpack.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,18 @@ TEST_CASE("MessagePack")
15111511
SECTION("unexpected end inside int with stream")
15121512
{
15131513
json _;
1514-
std::string data = {static_cast<char>(0xd2), static_cast<char>(0x12), static_cast<char>(0x34), static_cast<char>(0x56)};
1514+
const std::string data = {static_cast<char>(0xd2u), static_cast<char>(0x12u), static_cast<char>(0x34u), static_cast<char>(0x56u)};
15151515
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::istringstream(data, std::ios::binary)),
15161516
"[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
15171517
}
1518+
SECTION("misuse wchar for binary")
1519+
{
1520+
json _;
1521+
// creates 0xd2 after UTF-8 decoding, triggers get_elements in wide_string_input_adapter for code coverage
1522+
const std::u32string data = {static_cast<char32_t>(0x0280)};
1523+
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(data),
1524+
"[json.exception.parse_error.112] parse error at byte 1: wide string type cannot be interpreted as binary data", json::parse_error&);
1525+
}
15181526

15191527
SECTION("unsupported bytes")
15201528
{

0 commit comments

Comments
 (0)