mirror of
https://github.com/nlohmann/json.git
synced 2026-08-05 08:52:20 +00:00
avoid sign extension in char_traits<signed char>::to_int_type (#5336)
* avoid sign extension in char_traits<signed char>::to_int_type Signed-off-by: Angadi Yashaswini <angadi@digiscrypt.com> * spell out-of-range signed char constants as negative values (MSVC C4309) Signed-off-by: Angadi Yashaswini <angadi@digiscrypt.com> --------- Signed-off-by: Angadi Yashaswini <angadi@digiscrypt.com>
This commit is contained in:
@@ -231,7 +231,9 @@ struct char_traits<signed char> : std::char_traits<char>
|
||||
// Redefine to_int_type function
|
||||
static int_type to_int_type(char_type c) noexcept
|
||||
{
|
||||
return static_cast<int_type>(c);
|
||||
// cast via unsigned char: sign-extending a negative char_type would make
|
||||
// byte 0xFF indistinguishable from eof()
|
||||
return static_cast<int_type>(static_cast<unsigned char>(c));
|
||||
}
|
||||
|
||||
static char_type to_char_type(int_type i) noexcept
|
||||
|
||||
@@ -3994,7 +3994,9 @@ struct char_traits<signed char> : std::char_traits<char>
|
||||
// Redefine to_int_type function
|
||||
static int_type to_int_type(char_type c) noexcept
|
||||
{
|
||||
return static_cast<int_type>(c);
|
||||
// cast via unsigned char: sign-extending a negative char_type would make
|
||||
// byte 0xFF indistinguishable from eof()
|
||||
return static_cast<int_type>(static_cast<unsigned char>(c));
|
||||
}
|
||||
|
||||
static char_type to_char_type(int_type i) noexcept
|
||||
|
||||
@@ -427,6 +427,30 @@ TEST_CASE("deserialization")
|
||||
CHECK(l.events == std::vector<std::string>({"boolean(true)"}));
|
||||
}
|
||||
|
||||
SECTION("from std::vector<signed char>")
|
||||
{
|
||||
std::vector<signed char> const v = {'t', 'r', 'u', 'e'};
|
||||
CHECK(json::parse(v) == json(true));
|
||||
CHECK(json::accept(v));
|
||||
|
||||
SaxEventLogger l;
|
||||
CHECK(json::sax_parse(v, &l));
|
||||
CHECK(l.events.size() == 1);
|
||||
CHECK(l.events == std::vector<std::string>({"boolean(true)"}));
|
||||
|
||||
// bytes outside ASCII are negative here and must not be sign-extended;
|
||||
// 0xC3 and 0xA9 do not fit in signed char (MSVC C4309), so spell them as negative values
|
||||
std::vector<signed char> const umlaut = {'"', static_cast<signed char>(0xC3 - 0x100), static_cast<signed char>(0xA9 - 0x100), '"'};
|
||||
CHECK(json::parse(umlaut) == json("\xC3\xA9"));
|
||||
CHECK(json::accept(umlaut));
|
||||
|
||||
// 0xFF (spelled as -1 to stay in range) must not be reported as end of input
|
||||
std::vector<signed char> const trailing = {'t', 'r', 'u', 'e', static_cast<signed char>(0xFF - 0x100)};
|
||||
json _;
|
||||
CHECK_THROWS_WITH_AS(_ = json::parse(trailing), "[json.exception.parse_error.101] parse error at line 1, column 5: syntax error while parsing value - invalid literal; last read: 'true\xFF'; expected end of input", json::parse_error&);
|
||||
CHECK(!json::accept(trailing));
|
||||
}
|
||||
|
||||
SECTION("from std::array")
|
||||
{
|
||||
std::array<uint8_t, 5> const v { {'t', 'r', 'u', 'e'} };
|
||||
|
||||
@@ -53,4 +53,34 @@ TEST_CASE("type traits")
|
||||
// NOLINTEND(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("char_traits")
|
||||
{
|
||||
SECTION("to_int_type does not sign-extend")
|
||||
{
|
||||
using unsigned_traits = nlohmann::detail::char_traits<unsigned char>;
|
||||
using signed_traits = nlohmann::detail::char_traits<signed char>;
|
||||
|
||||
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(0x7F)) == 0x7F);
|
||||
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(0x80)) == 0x80);
|
||||
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(0xFF)) == 0xFF);
|
||||
|
||||
CHECK(signed_traits::to_int_type(static_cast<signed char>(0x7F)) == 0x7F);
|
||||
// 0x80 and 0xFF do not fit in signed char (MSVC C4309), so spell them as negative values
|
||||
CHECK(signed_traits::to_int_type(static_cast<signed char>(0x80 - 0x100)) == 0x80);
|
||||
CHECK(signed_traits::to_int_type(static_cast<signed char>(0xFF - 0x100)) == 0xFF);
|
||||
}
|
||||
|
||||
SECTION("no byte value collides with eof")
|
||||
{
|
||||
using unsigned_traits = nlohmann::detail::char_traits<unsigned char>;
|
||||
using signed_traits = nlohmann::detail::char_traits<signed char>;
|
||||
|
||||
for (int i = 0; i < 256; ++i)
|
||||
{
|
||||
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(i)) != unsigned_traits::eof());
|
||||
CHECK(signed_traits::to_int_type(static_cast<signed char>(i)) != signed_traits::eof());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user