mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 11:40:30 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80475865c0 |
@@ -70,9 +70,6 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
||||
1. The function can throw the following exceptions:
|
||||
- Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an array
|
||||
or null; in that case, using the `[]` operator with an index makes no sense.
|
||||
- Throws `#!cpp std::length_error` if `idx` equals the maximum value of `size_type`; the array is left unchanged.
|
||||
(This is the one index for which growing the array to hold it cannot be expressed as a `size_type` size, the same
|
||||
way an oversized [`resize`](https://en.cppreference.com/w/cpp/container/vector/resize) throws.)
|
||||
2. The function can throw the following exceptions:
|
||||
- Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an object
|
||||
or null; in that case, using the `[]` operator with a key makes no sense.
|
||||
@@ -260,8 +257,7 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
||||
|
||||
## Version history
|
||||
|
||||
1. Added in version 1.0.0. Fixed in version 3.13.0 to throw `#!cpp std::length_error` instead of emptying the array and
|
||||
accessing it out of bounds when `idx` equals the maximum value of `size_type`.
|
||||
1. Added in version 1.0.0.
|
||||
2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. Removed overloads for `T* key` (replaced by 3)
|
||||
in version 3.11.0.
|
||||
3. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as
|
||||
|
||||
@@ -88,7 +88,12 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
## Exceptions
|
||||
|
||||
- Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token, or
|
||||
empty input like a null `FILE*` or `char*` pointer.
|
||||
empty input like a null `FILE*` or `char*` pointer, or an `std::istream` without a stream buffer
|
||||
(`#!cpp i.rdbuf() == nullptr`, for instance `#!cpp std::istream(nullptr)`).
|
||||
- If reading from an `std::istream` reaches the end of the input and `eofbit` is part of the stream's
|
||||
[`exceptions()`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions) mask, the `std::ios_base::failure`
|
||||
thrown by the stream itself propagates instead of a `parse_error`, the same as it would for the standard library's
|
||||
own extraction operators.
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -254,6 +259,8 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by
|
||||
- Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) in version 3.13.0.
|
||||
- `JSON_STRICT_NUL_HANDLING` added in version 3.13.0 to optionally reject a NUL byte in the input instead of treating
|
||||
it as end of input; planned to become the default in version 4.0.0.
|
||||
- Extended empty-input detection to also cover an `std::istream` without a stream buffer, and fixed a crash
|
||||
(`std::terminate`) when parsing from an `std::istream` with `eofbit` in its exception mask, in version 3.13.0.
|
||||
|
||||
!!! warning "Deprecation"
|
||||
|
||||
|
||||
@@ -20,7 +20,12 @@ the stream `i`
|
||||
|
||||
## Exceptions
|
||||
|
||||
- Throws [`parse_error.101`](../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token.
|
||||
- Throws [`parse_error.101`](../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token, or if
|
||||
`i` has no stream buffer (`#!cpp i.rdbuf() == nullptr`, for instance `#!cpp std::istream(nullptr)`).
|
||||
- If reading from `i` reaches the end of the input and `eofbit` is part of `i`'s
|
||||
[`exceptions()`](https://en.cppreference.com/w/cpp/io/basic_ios/exceptions) mask, the `std::ios_base::failure`
|
||||
thrown by `i` itself propagates instead of a `parse_error`, the same as it would for the standard library's own
|
||||
extraction operators.
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -118,3 +123,5 @@ being read.
|
||||
it as end of input; planned to become the default in version 4.0.0.
|
||||
- `JSON_PRECISE_STREAM_POSITION` added in version 3.13.0 to optionally leave the character that terminates a number in
|
||||
the stream; planned to become the default in version 4.0.0.
|
||||
- Fixed a null pointer dereference for an `std::istream` without a stream buffer (now throws `parse_error.101`), and a
|
||||
crash (`std::terminate`) when `i` has `eofbit` in its exception mask, in version 3.13.0.
|
||||
|
||||
@@ -106,7 +106,13 @@ class input_stream_adapter
|
||||
// was given back with release_lookahead()
|
||||
commit_lookahead();
|
||||
#endif
|
||||
is->clear(is->rdstate() & std::ios::eofbit);
|
||||
// only call clear() if there is something to clear: it throws
|
||||
// std::ios_base::failure if the stream has exceptions() enabled
|
||||
// for a state bit that remains set, and a destructor must not throw
|
||||
if ((is->rdstate() & ~std::ios::eofbit) != 0)
|
||||
{
|
||||
is->clear(is->rdstate() & std::ios::eofbit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -811,12 +817,16 @@ inline file_input_adapter input_adapter(std::FILE* file)
|
||||
|
||||
inline input_stream_adapter input_adapter(std::istream& stream)
|
||||
{
|
||||
if (stream.rdbuf() == nullptr)
|
||||
{
|
||||
JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
|
||||
}
|
||||
return input_stream_adapter(stream);
|
||||
}
|
||||
|
||||
inline input_stream_adapter input_adapter(std::istream&& stream)
|
||||
{
|
||||
return input_stream_adapter(stream);
|
||||
return input_adapter(stream);
|
||||
}
|
||||
#endif // JSON_NO_IO
|
||||
|
||||
|
||||
@@ -36,9 +36,7 @@
|
||||
#include <iosfwd> // istream, ostream
|
||||
#endif // JSON_NO_IO
|
||||
#include <iterator> // make_move_iterator, random_access_iterator_tag
|
||||
#include <limits> // numeric_limits
|
||||
#include <memory> // unique_ptr
|
||||
#include <stdexcept> // length_error
|
||||
#include <string> // string, stoi, to_string
|
||||
#include <utility> // declval, forward, move, pair, swap
|
||||
#include <vector> // vector
|
||||
@@ -2832,13 +2830,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
// fill up the array with null values if given idx is outside the range
|
||||
if (idx >= m_data.m_value.array->size())
|
||||
{
|
||||
// idx + 1 would overflow size_type and wrap to 0, which would empty
|
||||
// the array instead of growing it; reject such an idx the same way
|
||||
// resize() rejects other indices that are too large to represent
|
||||
if (JSON_HEDLEY_UNLIKELY(idx == (std::numeric_limits<size_type>::max)()))
|
||||
{
|
||||
JSON_THROW(std::length_error(detail::concat("array index ", std::to_string(idx), " exceeds size_type")));
|
||||
}
|
||||
#if JSON_DIAGNOSTICS
|
||||
// remember array size & capacity before resizing
|
||||
const auto old_size = m_data.m_value.array->size();
|
||||
|
||||
@@ -36,9 +36,7 @@
|
||||
#include <iosfwd> // istream, ostream
|
||||
#endif // JSON_NO_IO
|
||||
#include <iterator> // make_move_iterator, random_access_iterator_tag
|
||||
#include <limits> // numeric_limits
|
||||
#include <memory> // unique_ptr
|
||||
#include <stdexcept> // length_error
|
||||
#include <string> // string, stoi, to_string
|
||||
#include <utility> // declval, forward, move, pair, swap
|
||||
#include <vector> // vector
|
||||
@@ -7652,7 +7650,13 @@ class input_stream_adapter
|
||||
// was given back with release_lookahead()
|
||||
commit_lookahead();
|
||||
#endif
|
||||
is->clear(is->rdstate() & std::ios::eofbit);
|
||||
// only call clear() if there is something to clear: it throws
|
||||
// std::ios_base::failure if the stream has exceptions() enabled
|
||||
// for a state bit that remains set, and a destructor must not throw
|
||||
if ((is->rdstate() & ~std::ios::eofbit) != 0)
|
||||
{
|
||||
is->clear(is->rdstate() & std::ios::eofbit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8357,12 +8361,16 @@ inline file_input_adapter input_adapter(std::FILE* file)
|
||||
|
||||
inline input_stream_adapter input_adapter(std::istream& stream)
|
||||
{
|
||||
if (stream.rdbuf() == nullptr)
|
||||
{
|
||||
JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
|
||||
}
|
||||
return input_stream_adapter(stream);
|
||||
}
|
||||
|
||||
inline input_stream_adapter input_adapter(std::istream&& stream)
|
||||
{
|
||||
return input_stream_adapter(stream);
|
||||
return input_adapter(stream);
|
||||
}
|
||||
#endif // JSON_NO_IO
|
||||
|
||||
@@ -28913,13 +28921,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
// fill up the array with null values if given idx is outside the range
|
||||
if (idx >= m_data.m_value.array->size())
|
||||
{
|
||||
// idx + 1 would overflow size_type and wrap to 0, which would empty
|
||||
// the array instead of growing it; reject such an idx the same way
|
||||
// resize() rejects other indices that are too large to represent
|
||||
if (JSON_HEDLEY_UNLIKELY(idx == (std::numeric_limits<size_type>::max)()))
|
||||
{
|
||||
JSON_THROW(std::length_error(detail::concat("array index ", std::to_string(idx), " exceeds size_type")));
|
||||
}
|
||||
#if JSON_DIAGNOSTICS
|
||||
// remember array size & capacity before resizing
|
||||
const auto old_size = m_data.m_value.array->size();
|
||||
|
||||
@@ -388,6 +388,37 @@ TEST_CASE("deserialization")
|
||||
}));
|
||||
}
|
||||
|
||||
SECTION("stream with eofbit in its exception mask (issue #5646)")
|
||||
{
|
||||
// reaching EOF while parsing a value that fills the whole input
|
||||
// (e.g., a number, or any value under strict parsing) makes
|
||||
// get_character() call std::istream::clear() to record eofbit;
|
||||
// with eofbit in the exception mask, that clear() itself throws
|
||||
// std::ios_base::failure - it must propagate to the caller instead
|
||||
// of ~input_stream_adapter() throwing a second exception while the
|
||||
// first is still unwinding, which would call std::terminate
|
||||
json _;
|
||||
|
||||
std::istringstream is1("1");
|
||||
is1.exceptions(std::ios::eofbit);
|
||||
CHECK_THROWS_AS(_ = json::parse(is1), std::ios_base::failure&);
|
||||
|
||||
// the same holds for the common std::ifstream::exceptions(failbit |
|
||||
// badbit | eofbit) pattern, because only eofbit ends up set
|
||||
std::istringstream is2("1");
|
||||
is2.exceptions(std::ios::failbit | std::ios::badbit | std::ios::eofbit);
|
||||
CHECK_THROWS_AS(_ = json::parse(is2), std::ios_base::failure&);
|
||||
}
|
||||
|
||||
SECTION("stream without a streambuf (issue #5646)")
|
||||
{
|
||||
// std::istream(nullptr) has badbit set and rdbuf() == nullptr;
|
||||
// get_character() must not dereference that null streambuf
|
||||
std::istream is(nullptr);
|
||||
json _;
|
||||
CHECK_THROWS_WITH_AS(_ = json::parse(is), "[json.exception.parse_error.101] parse error: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
|
||||
}
|
||||
|
||||
SECTION("string")
|
||||
{
|
||||
json::string_t const s = R"(["foo",1,2,3,false,{"one":1})";
|
||||
|
||||
@@ -147,24 +147,6 @@ TEST_CASE("element access 1")
|
||||
CHECK(j_const[7] == json({1, 2, 3}));
|
||||
}
|
||||
|
||||
SECTION("SIZE_MAX index (#5647)")
|
||||
{
|
||||
// idx + 1 must not be computed for idx == SIZE_MAX: it wraps to 0,
|
||||
// which would empty the array and then write out of bounds instead
|
||||
// of growing it; reject it like an oversized resize() would and
|
||||
// leave the array unchanged
|
||||
const auto max_idx = (std::numeric_limits<json::size_type>::max)();
|
||||
const std::string expected = "array index " + std::to_string(max_idx) + " exceeds size_type";
|
||||
const json j_before = j; // NOLINT(performance-unnecessary-copy-initialization)
|
||||
|
||||
CHECK_THROWS_WITH_AS(j[max_idx] = 1, expected.c_str(), std::length_error&);
|
||||
CHECK(j == j_before);
|
||||
|
||||
json j_empty = json::array();
|
||||
CHECK_THROWS_WITH_AS(j_empty[max_idx] = 1, expected.c_str(), std::length_error&);
|
||||
CHECK(j_empty == json::array());
|
||||
}
|
||||
|
||||
SECTION("access on non-array type")
|
||||
{
|
||||
SECTION("null")
|
||||
|
||||
@@ -234,4 +234,33 @@ TEST_CASE("JSON_PRECISE_STREAM_POSITION")
|
||||
CHECK(j == json(1));
|
||||
CHECK(remaining(is) == "true");
|
||||
}
|
||||
|
||||
SECTION("stream with eofbit in its exception mask (issue #5646)")
|
||||
{
|
||||
// with JSON_PRECISE_STREAM_POSITION, get_character() peeks via
|
||||
// sb->sgetc() rather than consuming via sb->sbumpc(), but it still
|
||||
// calls std::istream::clear() to record eofbit once the streambuf is
|
||||
// exhausted; with eofbit in the exception mask, that clear() itself
|
||||
// throws std::ios_base::failure, which must propagate to the caller
|
||||
// instead of ~input_stream_adapter() throwing a second exception
|
||||
// while the first is still unwinding (which would call std::terminate)
|
||||
json _;
|
||||
|
||||
std::istringstream is1("1");
|
||||
is1.exceptions(std::ios::eofbit);
|
||||
CHECK_THROWS_AS(_ = json::parse(is1), std::ios_base::failure&);
|
||||
|
||||
std::istringstream is2("1");
|
||||
is2.exceptions(std::ios::failbit | std::ios::badbit | std::ios::eofbit);
|
||||
CHECK_THROWS_AS(_ = json::parse(is2), std::ios_base::failure&);
|
||||
}
|
||||
|
||||
SECTION("stream without a streambuf (issue #5646)")
|
||||
{
|
||||
// std::istream(nullptr) has badbit set and rdbuf() == nullptr;
|
||||
// get_character() must not dereference that null streambuf
|
||||
std::istream is(nullptr);
|
||||
json _;
|
||||
CHECK_THROWS_WITH_AS(_ = json::parse(is), "[json.exception.parse_error.101] parse error: attempting to parse an empty input; check that your input string or stream contains the expected JSON", json::parse_error&);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user