Compare commits

...

5 Commits

Author SHA1 Message Date
Niels Lohmann 526c8c5079 ♻️ check for truncation
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-06-30 19:03:23 +02:00
Niels Lohmann 250787cb54 ♻️ add reserve_indent helper
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-06-30 18:56:27 +02:00
Niels Lohmann 3249b180cc 🎓 fix compilation
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-05-21 08:31:03 +02:00
Niels Lohmann 61f0d683e0 🚷 avoid overflow
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-05-20 22:08:45 +02:00
Niels Lohmann b3effb609c 🚷 use correct indentation character on resize
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-05-20 21:56:32 +02:00
4 changed files with 105 additions and 38 deletions
+30 -15
View File
@@ -125,11 +125,7 @@ class serializer
o->write_characters("{\n", 2); o->write_characters("{\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step; const auto new_indent = reserve_indent(current_indent, indent_step);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
// first n-1 elements // first n-1 elements
auto i = val.m_data.m_value.object->cbegin(); auto i = val.m_data.m_value.object->cbegin();
@@ -198,11 +194,7 @@ class serializer
o->write_characters("[\n", 2); o->write_characters("[\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step; const auto new_indent = reserve_indent(current_indent, indent_step);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
// first n-1 elements // first n-1 elements
for (auto i = val.m_data.m_value.array->cbegin(); for (auto i = val.m_data.m_value.array->cbegin();
@@ -259,11 +251,7 @@ class serializer
o->write_characters("{\n", 2); o->write_characters("{\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step; const auto new_indent = reserve_indent(current_indent, indent_step);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
@@ -373,6 +361,33 @@ class serializer
} }
} }
/*!
@brief compute the next indentation level and grow the indentation string
Computes the new indentation level @a current_indent + @a indent_step and
grows the indentation string (by doubling its size, but at least to the new
level) so that it can be used as a source for writing that many indentation
characters. The newly added characters use the configured @ref indent_char.
@param[in] current_indent the current indentation level
@param[in] indent_step the number of characters to indent per level
@return the new indentation level @a current_indent + @a indent_step
*/
unsigned int reserve_indent(const unsigned int current_indent, const unsigned int indent_step)
{
const unsigned int new_indent = current_indent + indent_step;
// a very large indent_step can wrap the unsigned accumulation on deep
// nesting, which would silently truncate the indentation
JSON_ASSERT(new_indent >= current_indent);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<std::size_t>(new_indent)), indent_char);
}
JSON_ASSERT(indent_string.size() >= new_indent);
return new_indent;
}
JSON_PRIVATE_UNLESS_TESTED: JSON_PRIVATE_UNLESS_TESTED:
/*! /*!
@brief dump escaped string @brief dump escaped string
+30 -15
View File
@@ -19299,11 +19299,7 @@ class serializer
o->write_characters("{\n", 2); o->write_characters("{\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step; const auto new_indent = reserve_indent(current_indent, indent_step);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
// first n-1 elements // first n-1 elements
auto i = val.m_data.m_value.object->cbegin(); auto i = val.m_data.m_value.object->cbegin();
@@ -19372,11 +19368,7 @@ class serializer
o->write_characters("[\n", 2); o->write_characters("[\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step; const auto new_indent = reserve_indent(current_indent, indent_step);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
// first n-1 elements // first n-1 elements
for (auto i = val.m_data.m_value.array->cbegin(); for (auto i = val.m_data.m_value.array->cbegin();
@@ -19433,11 +19425,7 @@ class serializer
o->write_characters("{\n", 2); o->write_characters("{\n", 2);
// variable to hold indentation for recursive calls // variable to hold indentation for recursive calls
const auto new_indent = current_indent + indent_step; const auto new_indent = reserve_indent(current_indent, indent_step);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize(indent_string.size() * 2, ' ');
}
o->write_characters(indent_string.c_str(), new_indent); o->write_characters(indent_string.c_str(), new_indent);
@@ -19547,6 +19535,33 @@ class serializer
} }
} }
/*!
@brief compute the next indentation level and grow the indentation string
Computes the new indentation level @a current_indent + @a indent_step and
grows the indentation string (by doubling its size, but at least to the new
level) so that it can be used as a source for writing that many indentation
characters. The newly added characters use the configured @ref indent_char.
@param[in] current_indent the current indentation level
@param[in] indent_step the number of characters to indent per level
@return the new indentation level @a current_indent + @a indent_step
*/
unsigned int reserve_indent(const unsigned int current_indent, const unsigned int indent_step)
{
const unsigned int new_indent = current_indent + indent_step;
// a very large indent_step can wrap the unsigned accumulation on deep
// nesting, which would silently truncate the indentation
JSON_ASSERT(new_indent >= current_indent);
if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
{
indent_string.resize((std::max)(indent_string.size() * 2, static_cast<std::size_t>(new_indent)), indent_char);
}
JSON_ASSERT(indent_string.size() >= new_indent);
return new_indent;
}
JSON_PRIVATE_UNLESS_TESTED: JSON_PRIVATE_UNLESS_TESTED:
/*! /*!
@brief dump escaped string @brief dump escaped string
+35
View File
@@ -245,6 +245,41 @@ TEST_CASE("object inspection")
CHECK(binary.dump(1024).size() == 2086); CHECK(binary.dump(1024).size() == 2086);
} }
SECTION("indentation and resize")
{
SECTION("array")
{
const auto j_array = json::parse("[[[[[[]]]]]]");
// check right size after indentation triggering a resize
CHECK(j_array.dump(1024).size() == 25622);
// check if right indentation symbol is used
CHECK(j_array.dump(1024, '\t')[4096] == '\t');
// check resize is large enough
CHECK(j_array.dump(10000).size() == 250022);
}
SECTION("object")
{
const auto j_object = json::parse(R"({"":{"":{"":{"":{"":{}}}}}})");
// check right size after indentation triggering a resize
CHECK(j_object.dump(1024).size() == 25642);
// check if right indentation symbol is used
CHECK(j_object.dump(1024, '\t')[4096] == '\t');
// check resize is large enough
CHECK(j_object.dump(10000).size() == 250042);
}
SECTION("binary")
{
const auto j_binary = json::binary({1, 2, 3}, 128);
// check right size after indentation triggering a resize
CHECK(j_binary.dump(1024).size() == 2086);
CHECK(j_binary.dump(1024, '\t')[1024] == '\t');
// check resize is large enough
CHECK(j_binary.dump(10000).size() == 20038);
}
}
SECTION("dump and floating-point numbers") SECTION("dump and floating-point numbers")
{ {
auto s = json(42.23).dump(); auto s = json(42.23).dump();
+10 -8
View File
@@ -311,14 +311,16 @@ TEST_CASE("dump for basic_json with long double number_float_t")
SECTION("round-trip dump/parse") SECTION("round-trip dump/parse")
{ {
constexpr std::array<long double, 13> values = constexpr std::array<long double, 13> values =
{{ {
0.0L, -0.0L, 1.0L, -1.0L, {
0.5L, -0.5L, 1.5L, -2.25L, 0.0L, -0.0L, 1.0L, -1.0L,
1.23e45L, 1.23e-45L, 0.5L, -0.5L, 1.5L, -2.25L,
(std::numeric_limits<long double>::min)(), 1.23e45L, 1.23e-45L,
std::numeric_limits<long double>::lowest(), (std::numeric_limits<long double>::min)(),
(std::numeric_limits<long double>::max)() std::numeric_limits<long double>::lowest(),
}}; (std::numeric_limits<long double>::max)()
}
};
for (long double v : values) for (long double v : values)
{ {