Repository navigation
Conversation
An OOM vulnerability exists in the json_pointer_set function (and related functions). See issue #916 for more details. To fix that, added a sanity check in the is_valid_index function to limit the maximum value of a parsed array index. Provided a configurable macro for modification. Signed-off-by: lone <lonechan314@qq.com>
make json_pointer_set_with_array_cb a public api, along with a function with wrapped callback Signed-off-by: lone <lonechan314@qq.com>
Signed-off-by: lone <lonechan314@qq.com>
Signed-off-by: lone <lonechan314@qq.com>
Allows wraps to use the dependency_names syntax instead of the possibly changing variable syntax. Signed-off-by: Rosen Penev <rosenp@gmail.com>
> was missing. Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Rosen Penev <rosenp@gmail.com>
Needed as tests use private APIs that are not exported. Signed-off-by: Rosen Penev <rosenp@gmail.com>
Something in the configure stage goes wrong where it believe strncasecmp is present but the header defining it is not. Work around this. Signed-off-by: Rosen Penev <rosenp@gmail.com>
Signed-off-by: Rosen Penev <rosenp@gmail.com>
meson: use override_dependency
- Changed json_pointer_set_with_array_cb to json_pointer_set_with_cb, related cb updated - Added tests(test_safe_json_pointer_set.*) for new-exported funcs, also updated cmake & meson Signed-off-by: lone <lonechan314@qq.com>
Fix: OOM vulnerability cause by is_valid_index
…he passed value when json_pointer_set failed. Also, reformat.
…cause problems due to stack recursion.
…ted object trees can be freed.
avoid recursion during json_object_put
This patch fixes the build error with clang-21 tests/test_deep_nesting.c:19:34: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes] 19 | static char *generate_json_string() by fixing generate_json_string() definition. It also removes prototypes for static functions that are not needed.
…ify that any change of the fields present on the object being iterated invalidates the iterator. Update the test to show what kind of changes _are_ allowed.
Fix build with clang-21
…pps/json_parse tool. Also, show the failing character in hex to ensure error output is readable.
json_c_set_serialization_double_format() freed the current format string before strdup() of its replacement. In the GLOBAL branch the freed pointer was not reset on the strdup() failure path, leaving it dangling: a later serialization read through the dangling pointer, and a subsequent set(NULL) call freed it a second time. The THREAD branch reset its pointer to NULL right after the free, so it had no dangling pointer; its only defect was that a strdup() failure silently dropped the configured thread-local format and fell back to the global format. strdup() the new format first and replace the old one only once it succeeds, so a failure leaves both the global and thread-local format exactly as they were before the call. Fixes #997.
json_object_array_move_cb() added one to the array length when "from"
and "path" are in the same array, so that the old length counted as a
valid target index. A move is a remove followed by an add (RFC 6902
section 4.4), and the add's index must not be greater than the number
of elements left after the remove. With the extra slot,
json_object_array_insert_idx() was called one past the end and padded
the array with a null:
doc: {"foo": [1, 2, 3]}
patch: [{"op": "move", "from": "/foo/0", "path": "/foo/3"}]
returned 0 with {"foo": [2, 3, null, 1]}. It now fails with EINVAL,
like an add to /foo/3 on a two-element array. Moving to /foo/2 or
/foo/- still puts the element last.
…fails
When json_tokener_parse_ex() finishes a nested value it detaches the
value from its parse level (obj = json_object_get(current)) and then
inserts it into the parent container with json_object_object_add() or
json_object_array_add(). If that insertion fails -- which happens when
an allocation fails while duplicating the key or growing the container --
the error path returned without releasing obj, leaking the value and
everything it referenced.
Track the transfer explicitly: clear obj once the parent owns the value,
and release it at the shared error exit otherwise. This covers both
insertion sites and any future error path between detaching and
inserting.
Found with allocation-failure injection: a sweep failing each allocation
of a parse in turn leaked one object per failing insertion point (469
injection points across a small set of documents).
Regression coverage: tests/test_tokener_oom.c injects an allocation
failure at every step of parsing representative documents and asserts
that nothing remains allocated after each attempt. It fails against the
unpatched parser ("LEAK: doc 0, failing allocation 12") and passes with
this fix; the rest of the suite (30 tests) still passes.
Signed-off-by: huangwenbin <huangwenbin@kylinos.cn>
Signed-off-by: huangwenbin <huangwenbin@kylinos.cn>
json_patch_apply_move_copy() applies the "from must not be a proper prefix of path" rule of RFC 6902 section 4.4 to both move and copy, and treats from == path as a no-op for both. The rule only exists for move: copy is an "add" of the value found at "from" (section 4.5), and since 5647679 that value is a deep copy, so it can be added anywhere. As a result valid patches failed or did nothing: {"a":{"b":1}} copy /a -> /a/c EINVAL, expected {"a":{"b":1,"c":{"b":1}}} {"a":1} copy "" -> /b EINVAL, expected {"a":1,"b":{"a":1}} {"foo":[1,2]} copy /foo/0 -> /foo/0 no-op, expected {"foo":[1,1,2]} Only apply the parent/child check to move.
With JSON_TOKENER_VALIDATE_UTF8, the count of pending continuation bytes lived in a local variable, so a multi-byte character split across two json_tokener_parse_ex() calls was reported as invalid UTF-8 even though the same bytes parse fine in a single call. Keep the validator state in the tokener, only fail on an open sequence when the call did not end with json_tokener_continue, and clear it in json_tokener_reset().
json_object_object_to_json_string() ignored the result of json_escape_str() for member names, and the double and userdata serializers ignored the printbuf_memappend() result and returned the length anyway. When such an append failed (allocation failure, or EFBIG once the buffer would pass INT_MAX) the key or number text was dropped, later shorter appends still fit, and the caller got "successful" output with an empty key or a missing value. Return -1 from those sites like the string serializer already does.
hawicz asked for the split-sequence state to go into the existing flags field rather than growing struct json_tokener. The validator's remaining count and lead byte now pack into 10 bits above the public JSON_TOKENER_* flags; json_tokener_parse_ex() loads them on entry and stores them back on exit, json_tokener_reset() clears them, and json_tokener_set_flags() preserves them so re-setting flags between chunks (as test_parse.c and apps/json_parse do) doesn't drop an open sequence. Also update test_json_parse_cli.expected: a lone lead byte at the end of the input is now left open as json_tokener_continue and reported by json_parse's end-of-data path instead of per chunk.
JSON_TOKENER_STRICT already refuses single-quoted string values, but the
object key state still accepted ' as a key delimiter, so {'a':1} parsed
successfully in strict mode. Apply the same check to keys.
…3/json-c into nab-iha3-tokener-utf8-split-state
json_object_double_to_json_string_format() only checked for lowercase 'e' before appending ".0" to doubles without a decimal point. Custom formats like "%.0G", "%.17G", or "%.0E" can produce uppercase 'E' exponents, so values such as 12.0 or 1e20 were serialized as invalid JSON like "1E+01.0" or "1E+20.0". Check for 'E' as well, and add "%.0G" and "%.0E" cases to tests/test_double_serializer.c.
json_patch_apply_move_copy() returned 0 before calling json_pointer_get_internal() whenever "from" and "path" matched on a move op. A move with a nonexistent or invalid "from" (such as "/bar" to "/bar" or "foo" to "foo") therefore succeeded instead of failing with ENOENT or EINVAL as required by RFC 6902 section 4.4. Restrict the pre-lookup check to proper prefixes so "from" is always resolved first, then return 0 for from == path after lookup succeeds. Add regression tests in tests/json_patch_tests.json.
Check path_len > from_s_len ahead of strncmp() and path[from_s_len] in json_patch_apply_move_copy() so the bounds check is explicit and the separate path[from_s_len] != '\0' test is no longer needed.
Commit b5cd62d changed printbuf_memset() to reserve size_needed + 1 bytes for the NUL terminator, but left the bounds check at len > INT_MAX - offset. When offset + len == INT_MAX, size_needed + 1 overflows signed int, skips printbuf_extend(), and writes past the buffer. Tighten the check to len > INT_MAX - offset - 1, matching printbuf_memappend(), and add a regression check in tests/test_printbuf.c.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See Commits and Changes for more details.
Created by
pull[bot] (v2.0.0-alpha.4)
Can you help keep this open source service alive? 💖 Please sponsor : )