mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 03:30:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57890cebad |
@@ -678,19 +678,29 @@ class json_pointer
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// may throw parse_error.106/109 for a malformed index; an
|
||||
// index that is syntactically valid but cannot be
|
||||
// represented (out_of_range.404/410) is treated like an
|
||||
// out-of-range index below
|
||||
typename BasicJsonType::size_type idx{};
|
||||
JSON_TRY
|
||||
// tokens that array_index() rejects with parse_error.106/109
|
||||
// are passed on to it; all other tokens that it would reject
|
||||
// with out_of_range.404/410 are detected here, so that this
|
||||
// also works without exceptions
|
||||
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 && !(reference_token[0] >= '1' && reference_token[0] <= '9')))
|
||||
{
|
||||
idx = array_index<BasicJsonType>(reference_token);
|
||||
static_cast<void>(array_index<BasicJsonType>(reference_token)); // throws parse_error.106/109
|
||||
}
|
||||
JSON_INTERNAL_CATCH (detail::out_of_range&)
|
||||
if (JSON_HEDLEY_UNLIKELY(reference_token.empty() || !std::all_of(reference_token.begin(), reference_token.end(), [](const char c)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
})))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
errno = 0; // strtoull() does not reset errno on success
|
||||
char* p_end = nullptr; // NOLINT(misc-const-correctness)
|
||||
const unsigned long long magnitude = std::strtoull(reference_token.data(), &p_end, 10); // NOLINT(runtime/int)
|
||||
if (JSON_HEDLEY_UNLIKELY(errno == ERANGE || magnitude >= static_cast<unsigned long long>((std::numeric_limits<typename BasicJsonType::size_type>::max)()))) // NOLINT(runtime/int)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
const auto idx = static_cast<typename BasicJsonType::size_type>(magnitude);
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size()))
|
||||
{
|
||||
|
||||
@@ -19520,19 +19520,29 @@ class json_pointer
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// may throw parse_error.106/109 for a malformed index; an
|
||||
// index that is syntactically valid but cannot be
|
||||
// represented (out_of_range.404/410) is treated like an
|
||||
// out-of-range index below
|
||||
typename BasicJsonType::size_type idx{};
|
||||
JSON_TRY
|
||||
// tokens that array_index() rejects with parse_error.106/109
|
||||
// are passed on to it; all other tokens that it would reject
|
||||
// with out_of_range.404/410 are detected here, so that this
|
||||
// also works without exceptions
|
||||
if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 && !(reference_token[0] >= '1' && reference_token[0] <= '9')))
|
||||
{
|
||||
idx = array_index<BasicJsonType>(reference_token);
|
||||
static_cast<void>(array_index<BasicJsonType>(reference_token)); // throws parse_error.106/109
|
||||
}
|
||||
JSON_INTERNAL_CATCH (detail::out_of_range&)
|
||||
if (JSON_HEDLEY_UNLIKELY(reference_token.empty() || !std::all_of(reference_token.begin(), reference_token.end(), [](const char c)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
})))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
errno = 0; // strtoull() does not reset errno on success
|
||||
char* p_end = nullptr; // NOLINT(misc-const-correctness)
|
||||
const unsigned long long magnitude = std::strtoull(reference_token.data(), &p_end, 10); // NOLINT(runtime/int)
|
||||
if (JSON_HEDLEY_UNLIKELY(errno == ERANGE || magnitude >= static_cast<unsigned long long>((std::numeric_limits<typename BasicJsonType::size_type>::max)()))) // NOLINT(runtime/int)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
const auto idx = static_cast<typename BasicJsonType::size_type>(magnitude);
|
||||
|
||||
if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size()))
|
||||
{
|
||||
|
||||
@@ -46,6 +46,21 @@ TEST_CASE("Tests with disabled exceptions")
|
||||
CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'");
|
||||
delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
|
||||
SECTION("issue #5672 - value(json_pointer, default) must not abort for array tokens that are not a valid index")
|
||||
{
|
||||
const json j = {1, 2, 3};
|
||||
|
||||
// a syntactically valid index that is out of range for this array
|
||||
CHECK(j.value("/7"_json_pointer, 42) == 42);
|
||||
// a reference token that is not a number at all
|
||||
CHECK(j.value("/1a"_json_pointer, 42) == 42);
|
||||
// the empty reference token (JSON pointer "/")
|
||||
CHECK(j.value("/"_json_pointer, 42) == 42);
|
||||
// an index whose magnitude does not fit into size_type
|
||||
CHECK(j.value("/99999999999999999999999"_json_pointer, 42) == 42);
|
||||
CHECK(j.value("/18446744073709551615"_json_pointer, 42) == 42);
|
||||
}
|
||||
}
|
||||
|
||||
DOCTEST_GCC_SUPPRESS_WARNING_POP
|
||||
|
||||
@@ -482,6 +482,21 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
|
||||
CHECK(j_array.value("/-"_json_pointer, 42) == 42);
|
||||
CHECK(j_array_const.value("/-"_json_pointer, 42) == 42);
|
||||
|
||||
// Test an index with a non-digit after a valid leading digit; this is
|
||||
// out_of_range (not parse_error) and must not throw (see #5672)
|
||||
CHECK(j_array.value("/1a"_json_pointer, 42) == 42);
|
||||
CHECK(j_array_const.value("/1a"_json_pointer, 42) == 42);
|
||||
|
||||
// Test the empty reference token (JSON pointer "/"); see #5672
|
||||
CHECK(j_array.value("/"_json_pointer, 42) == 42);
|
||||
CHECK(j_array_const.value("/"_json_pointer, 42) == 42);
|
||||
|
||||
// Test an index whose magnitude does not fit into size_type (see #5672)
|
||||
CHECK(j_array.value("/99999999999999999999999"_json_pointer, 42) == 42);
|
||||
CHECK(j_array_const.value("/99999999999999999999999"_json_pointer, 42) == 42);
|
||||
CHECK(j_array.value("/18446744073709551615"_json_pointer, 42) == 42);
|
||||
CHECK(j_array_const.value("/18446744073709551615"_json_pointer, 42) == 42);
|
||||
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// Test malformed index (non-numeric) throws parse_error
|
||||
CHECK_THROWS_WITH_AS(j_array.value("/foo"_json_pointer, 1), "[json.exception.parse_error.109] parse error: array index 'foo' is not a number", typename Json::parse_error&);
|
||||
|
||||
@@ -8,6 +8,3 @@ The following changes have been made to the code with respect to <https://github
|
||||
- membership check
|
||||
- made function from `_is_within`
|
||||
- removed unused variable `actual_path`
|
||||
- Added the optional config key `external`: include paths listed there are kept as
|
||||
`#include` directives instead of being inlined (the first directive per path; the
|
||||
repeated ones are commented out).
|
||||
|
||||
@@ -57,11 +57,6 @@ Python v.2.7.0 or higher is required.
|
||||
amalgamation. Have a look at `test/source.c.json` and `test/include.h.json`
|
||||
to see two examples.
|
||||
|
||||
The optional `external` list names include paths that are kept as `#include`
|
||||
directives instead of being inlined, e.g. `["nlohmann/json.hpp"]` for a header
|
||||
that includes another amalgamated header. Only the first directive for each
|
||||
of these paths is kept; the repeated ones are commented out.
|
||||
|
||||
* The `-s, --source` option should specify the path to the source directory.
|
||||
This is useful for supporting separate source and build directories.
|
||||
|
||||
|
||||
@@ -62,10 +62,6 @@ class Amalgamation(object):
|
||||
return None
|
||||
|
||||
def __init__(self, args):
|
||||
# include paths that are kept as #include directives instead of
|
||||
# being inlined (e.g. a header amalgamated on its own)
|
||||
self.external = []
|
||||
self.included_external = []
|
||||
with open(args.config, 'r') as f:
|
||||
config = json.loads(f.read())
|
||||
for key in config:
|
||||
@@ -224,14 +220,11 @@ class TranslationUnit(object):
|
||||
while include_match:
|
||||
if not _is_within(include_match, skippable_contexts):
|
||||
include_path = include_match.group("path")
|
||||
if include_path in self.amalgamation.external:
|
||||
includes.append((include_match, None))
|
||||
else:
|
||||
search_same_dir = include_match.group(1) == '"'
|
||||
found_included_path = self.amalgamation.find_included_file(
|
||||
include_path, self.file_dir if search_same_dir else None)
|
||||
if found_included_path:
|
||||
includes.append((include_match, found_included_path))
|
||||
search_same_dir = include_match.group(1) == '"'
|
||||
found_included_path = self.amalgamation.find_included_file(
|
||||
include_path, self.file_dir if search_same_dir else None)
|
||||
if found_included_path:
|
||||
includes.append((include_match, found_included_path))
|
||||
|
||||
include_match = self.include_pattern.search(self.content,
|
||||
include_match.end())
|
||||
@@ -242,17 +235,6 @@ class TranslationUnit(object):
|
||||
for include in includes:
|
||||
include_match, found_included_path = include
|
||||
tmp_content += self.content[prev_end:include_match.start()]
|
||||
if found_included_path is None:
|
||||
# an external header: keep the first directive and comment
|
||||
# out the repeated ones
|
||||
include_path = include_match.group("path")
|
||||
if include_path in self.amalgamation.included_external:
|
||||
tmp_content += "// {0}".format(include_match.group(0))
|
||||
else:
|
||||
self.amalgamation.included_external.append(include_path)
|
||||
tmp_content += include_match.group(0)
|
||||
prev_end = include_match.end()
|
||||
continue
|
||||
tmp_content += "// {0}\n".format(include_match.group(0))
|
||||
if found_included_path not in self.amalgamation.included_files:
|
||||
t = TranslationUnit(found_included_path, self.amalgamation, False)
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"project": "JSON for Modern C++",
|
||||
"target": "single_include/nlohmann/json_view.hpp",
|
||||
"sources": [
|
||||
"include/nlohmann/json_view.hpp"
|
||||
],
|
||||
"include_paths": ["include"],
|
||||
"external": ["nlohmann/json.hpp"]
|
||||
}
|
||||
Reference in New Issue
Block a user