Skip to content

Commit

Permalink
Reading binary number from wchar as an error, address warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
TianyiChen committed Nov 27, 2024
1 parent be2eb53 commit 72b20eb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 4 additions & 8 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ class iterator_input_adapter
template<class T>
std::size_t get_elements(T* dest, std::size_t count = 1)
{
auto* ptr = reinterpret_cast<unsigned char*>(dest);
auto* ptr = reinterpret_cast<char*>(dest);
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
{
if (JSON_HEDLEY_LIKELY(current != end))
{
ptr[read_index] = *current;
ptr[read_index] = static_cast<char>(*current);
std::advance(current, 1);
}
else
Expand Down Expand Up @@ -359,15 +359,11 @@ class wide_string_input_adapter
return utf8_bytes[utf8_bytes_index++];
}

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

private:
Expand Down
12 changes: 4 additions & 8 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6413,12 +6413,12 @@ class iterator_input_adapter
template<class T>
std::size_t get_elements(T* dest, std::size_t count = 1)
{
auto* ptr = reinterpret_cast<unsigned char*>(dest);
auto* ptr = reinterpret_cast<char*>(dest);
for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
{
if (JSON_HEDLEY_LIKELY(current != end))
{
ptr[read_index] = *current;
ptr[read_index] = static_cast<char>(*current);
std::advance(current, 1);
}
else
Expand Down Expand Up @@ -6592,15 +6592,11 @@ class wide_string_input_adapter
return utf8_bytes[utf8_bytes_index++];
}

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

private:
Expand Down
10 changes: 9 additions & 1 deletion tests/src/unit-msgpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1511,10 +1511,18 @@ TEST_CASE("MessagePack")
SECTION("unexpected end inside int with stream")
{
json _;
std::string data = {static_cast<char>(0xd2), static_cast<char>(0x12), static_cast<char>(0x34), static_cast<char>(0x56)};
const std::string data = {static_cast<char>(0xd2u), static_cast<char>(0x12u), static_cast<char>(0x34u), static_cast<char>(0x56u)};
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(std::istringstream(data, std::ios::binary)),
"[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing MessagePack number: unexpected end of input", json::parse_error&);
}
SECTION("misuse wchar for binary")
{
json _;
// creates 0xd2 after UTF-8 decoding, triggers get_elements in wide_string_input_adapter for code coverage
const std::u32string data = {static_cast<char32_t>(0x0280)};
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(data),
"[json.exception.parse_error.112] parse error at byte 1: wide string type cannot be interpreted as binary data", json::parse_error&);
}

SECTION("unsupported bytes")
{
Expand Down

0 comments on commit 72b20eb

Please sign in to comment.