diff --git a/src/json.hpp b/src/json.hpp index 37080ebb7..f62f3e783 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -757,7 +757,7 @@ class basic_json /// assignment type_data_t& operator=(value_t rhs) { - bits.type = static_cast(rhs); + bits.type = static_cast(rhs) & 15; // avoid overflow return *this; } @@ -765,7 +765,7 @@ class basic_json type_data_t(value_t t) noexcept { *reinterpret_cast(this) = 0; - bits.type = static_cast(t); + bits.type = static_cast(t) & 15; // avoid overflow } /// default constructor @@ -6011,9 +6011,11 @@ class basic_json { // convert a number 0..15 to its hex representation // (0..f) - auto hexify = [](const char v) -> char + const auto hexify = [](const int v) -> char { - return (v < 10) ? ('0' + v) : ('a' + v - 10); + return (v < 10) + ? ('0' + static_cast(v)) + : ('a' + static_cast((v - 10) & 0x1f)); }; // print character c as \uxxxx diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 4321f0aa3..f3d3508cf 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -757,7 +757,7 @@ class basic_json /// assignment type_data_t& operator=(value_t rhs) { - bits.type = static_cast(rhs); + bits.type = static_cast(rhs) & 15; // avoid overflow return *this; } @@ -765,7 +765,7 @@ class basic_json type_data_t(value_t t) noexcept { *reinterpret_cast(this) = 0; - bits.type = static_cast(t); + bits.type = static_cast(t) & 15; // avoid overflow } /// default constructor @@ -6011,9 +6011,11 @@ class basic_json { // convert a number 0..15 to its hex representation // (0..f) - auto hexify = [](const char v) -> char + const auto hexify = [](const int v) -> char { - return (v < 10) ? ('0' + v) : ('a' + v - 10); + return (v < 10) + ? ('0' + static_cast(v)) + : ('a' + static_cast((v - 10) & 0x1f)); }; // print character c as \uxxxx