summaryrefslogtreecommitdiff
path: root/json_object.c
Commit message (Collapse)AuthorAgeFilesLines
* When serializing with JSON_C_TO_STRING_PRETTY set, keep the opening and ↵Eric Haszlakiewicz2022-07-301-14/+8
| | | | closing curly or square braces on same line for empty objects or arrays. Issue #778.
* Fix memory leak with emtpy strings in json_object_set_stringDaniel Danzberger2022-07-241-3/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When a json string object is updated with a bigger string, a new malloc'ed buffer is used to store the new string and it's size is made negative to indicate that an external buffer is in use. When that same json string object get's updated again with an empty stirng (size = 0), the new external malloc'ed buffer is still used. But the fact that the new size value is not negative removes the indicator that the externally malloc'ed buffer is used. This becomes a problem when the object get's updated again with any other string, because a new buffer will be malloced and linked to the object while to old one won't be free'd. This causes a memory leak when updating a json string with json_object_set_stirng() which has previously been updated with an empty string. Example: -- obj = json_object_new_string("data"); json_object_set_string(obj, "more data"); json_object_set_string(obj, ""); json_object_set_string(obj, "other data"); /* leaks */ -- This commit fixes the issue by free'ing the external buffer when an empty string is set and use the internal one again. Signed-off-by: Daniel Danzberger <daniel@dd-wrt.com>
* Merge pull request #758 from c3h2-ctf/contextEric Hawicz2022-04-041-6/+41
|\ | | | | Preserve context if out of memory
| * Preserve context if out of memoryTobias Stoeckmann2022-03-311-6/+41
| | | | | | | | | | | | If memory allocation fails in json_c_set_serialization_double_format or json_object_copy_serializer_data then return with an error value and preserve previous values without overriding them with NULL.
* | Remove single quote from #error line. Fixes issue #761Eric Haszlakiewicz2022-03-251-1/+1
| |
* | Code style removed unneeded double-quotesBonsaY2022-03-211-1/+1
|/ | | | this way, it complies with the other #error usages
* Fix error messagesTobias Stoeckmann2022-03-191-3/+4
| | | | Error messages did not reflect actual function names.
* Merge pull request #739 from rouault/avoid_unsigned_integer_overflowEric Hawicz2022-02-181-1/+2
|\ | | | | json_escape_str(): avoid harmless unsigned integer overflow
| * json_escape_str(): avoid harmless unsigned integer overflowEven Rouault2022-01-121-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Current behaviour is perfectly valid, since wrap-over upon overflow is well defined behaviour for unsigned types, but it is nevertheless nice to be able to build with -fsanitize=undefined,unsigned-integer-overflow There is no significant effect on the generated assembly as can be seen on the diff of objdump -d output on a optimized build (the compiler just decided to switch the order of a comparison): @@ -135,8 +135,8 @@ 1d0: 0f 84 70 ff ff ff je 146 <json_escape_str+0x146> 1d6: 4c 3b 24 24 cmp (%rsp),%r12 1da: 0f 85 2d ff ff ff jne 10d <json_escape_str+0x10d> - 1e0: 49 39 f4 cmp %rsi,%r12 - 1e3: 0f 87 b7 00 00 00 ja 2a0 <json_escape_str+0x2a0> + 1e0: 4c 39 e6 cmp %r12,%rsi + 1e3: 0f 82 b7 00 00 00 jb 2a0 <json_escape_str+0x2a0> 1e9: 48 8b 44 24 18 mov 0x18(%rsp),%rax 1ee: 64 48 33 04 25 28 00 xor %fs:0x28,%rax 1f5: 00 00
* | json_object_copy_serializer_data(): add assertionEven Rouault2022-01-161-0/+1
|/ | | | | | This makes Coverity Scan happier since it believes that the initial check ``if (!src->_userdata && !src->_user_delete)`` could mean that src->_user_data may be nullptr.
* Add linkhash accessor functions (lh_table_head(), lh_entry_next(), etc...) ↵Eric Haszlakiewicz2021-11-301-2/+2
| | | | | | | to pave the way for making the lh_table and lh_entry structure opaque in the future. Update the docs to mark all members of those structures deprecated, and suggest what to use instead.
* Drop the REFCOUNT_DEBUG code, it hasn't been used in ages.Eric Haszlakiewicz2021-11-301-46/+0
|
* Fix for clang ub sanitizerRobert Bielik2021-11-111-5/+5
|
* Really use prefix JSON_C_OBJECT_ADD_*José Bollo2021-10-191-1/+1
| | | | | | | | | | | | | | This change introduces JSON_C_OBJECT_ADD_CONSTANT_KEY as a replacement of JSON_C_OBJECT_KEY_IS_CONSTANT. The description of json_object_object_add_ex tells to look at the flags JSON_C_OBJECT_ADD_* but it is not for JSON_C_OBJECT_KEY_IS_CONSTANT. From the point of vue of a developper using json-c, the function json_object_object_add_ex is mainly used, not the hash facility, it seems more natural to provide a regular naming of prefix JSON_C_OBJECT_ADD_CONSTANT_KEY.
* Merge some old work to include (some of) PR #464 into the current master branch.Eric Haszlakiewicz2021-07-251-13/+14
|\
| * Update json_object.cihsinme2021-02-051-3/+3
| |
| * Cap string length at INT_MAX.Tobias Stoeckmann2020-08-221-3/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Several issues occur if a string is longer than INT_MAX: - The function json_object_get_string_len returns the length of a string as int. If the string is longer than INT_MAX, the result would be negative. - That in turn would lead to possible out of boundary access when comparing these strings with memcmp and the returned length as done in json_object_equal. - If json_escape_str is called with such strings, out of boundary accesses can occur due to internal int handling (also fixed). - The string cannot be printed out due to printbuffer limits at INT_MAX (which is still true after this commit). Such huge strings can only be inserted through API calls at this point because input files are capped at INT_MAX anyway. Due to huge amount of RAM needed to reproduce these issues I have not added test cases.
| * Aligned comment in _json_object_new_stringTobias Stoeckmann2020-08-221-5/+5
| | | | | | | | | | The comment only aligns correctly if tab size is 4. Replaced spaces with tabs to stay in sync with style of other lines.
| * Fixed warningsAram Poghosyan2020-08-141-2/+2
| |
* | Eliminate use of ctype.h and replace isdigit() and tolower() with ↵Eric Haszlakiewicz2020-08-021-3/+5
| | | | | | | | non-locale-sensitive approaches.
* | Take a hint from PR #464 and use json_object_new_string_len() to avoid a ↵Eric Haszlakiewicz2020-08-021-10/+16
|/ | | | | | | needless extra strlen() call. Also, create a _json_object_get_string_len() for internal use when we know for sure the object we're dealing with is a json_type_string.
* Fix "may be used uninitialized" Release build failureMarc2020-07-231-1/+1
| | | Fixes https://github.com/json-c/json-c/issues/647
* Issue #641: Add a cast to void * to address some theoretically undefined ↵Eric Haszlakiewicz2020-07-111-1/+1
| | | | printf behavior.
* Use constants referring to the signed integer types when setting SSIZE_T_MAX.Eric Haszlakiewicz2020-07-011-3/+3
| | | | | | In practice, the sizes of the signed and unsigned integer types will almost cetainly be the same, but this is more correct. Pointed out in issue #638.
* In the json_tokener_state_number case, explicitly adjust what "number" ↵Eric Haszlakiewicz2020-06-291-1/+0
| | | | | | characters are allowed based on the exact micro-state that we're in, and check for invalid following characters in a different way, to allow a valid json_type_number object to be returned at the top level. This causes previously failing strings like "123-456" to return a valid json_object with the appropriate value. If you care about the trailing content, call json_tokener_parse_ex() and check the parse end point with json_tokener_get_parse_end().
* Add json_object_array_shrink() (and array_list_shrink()) and use it in ↵Eric Haszlakiewicz2020-06-201-1/+12
| | | | | | json_tokener to minimize the amount of memory used. This results in a 39%-50% reduction in memory use (peak RSS, peak heap usage) on the jc-bench benchmark and 9% shorter runtime. Also add the json_object_new_array_ext, array_list_new2, and array_list_shrink functions.
* Reformat the json_object-split branch with clang-formatjson_object-splitEric Haszlakiewicz2020-06-161-38/+31
|
* Drop the _delete field from struct json_object and call the type-specific ↵Eric Haszlakiewicz2020-06-131-16/+30
| | | | delete functions directly from json_object_put. (Thanks @dota17 for the suggestion in PR #632!)
* Drop the useless "char data[1]" from struct json_object. Fix a typo in a ↵Eric Haszlakiewicz2020-06-131-1/+1
| | | | comment.
* The split of json_object into type-specific sub-structures is now ↵Eric Haszlakiewicz2020-06-071-399/+75
| | | | | | functionally complete. Remove all of the temporary defines, structures, old compat fuctions, extra fields, etc... that were needed during the conversion to a split set of json_object_* structures.
* More fixes for old MSVC builds.Eric Haszlakiewicz2020-06-071-5/+5
|
* Kick json_type_string out of struct json_object.Eric Haszlakiewicz2020-06-071-91/+142
| | | | | The default is now that string data is stored inline at the end of json_object, though to allow for json_object_set_string() to set a _longer_ string, we still need to allow for the possibility of a separate char * pointer. All json types have been split out now, next step it cleanup.
* Kick json_type_int and json_type_double out of struct json_object.Eric Haszlakiewicz2020-05-261-135/+170
| | | | Clean up the code in json_object_new_* a bit by dropping unnecesary json_object_base variables.
* Declare variables earlier, to appease Visual Studio 2010.Eric Haszlakiewicz2020-05-251-3/+6
|
* Kick json_type_boolean out of struct json_object.Eric Haszlakiewicz2020-05-251-15/+36
|
* Add some backwards compat for Visual Studio 2013.Eric Haszlakiewicz2020-05-251-0/+5
|
* Kick json_type_array out of struct json_object; re-enable the test_deep_copy ↵Eric Haszlakiewicz2020-05-251-28/+81
| | | | test.
* Start splitting struct json_object into multiple sub-types, as descibed at ↵Eric Haszlakiewicz2020-05-251-49/+335
| | | | | | | | | | | | https://github.com/json-c/json-c/wiki/Proposal:-struct-json_object-split The current changes split out _only_ json_type_object, and thus have a number of hacks to allow the code to continue to build and work. Originally mentioned in issue #535. When complete, this will probably invalidate #552. This is likely to cause notable conflicts in any other significant un-merged changes, such as PR#620.
* Prevent truncation on custom double formatters.Tobias Stoeckmann2020-05-161-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | A custom double formatter can lead to truncation of the rest of the JSON document. If a custom formatter completely fills the buffer used by snprintf with a trailing dot or comma and the formatting option JSON_C_TO_STRING_NOZERO has been specified, then an iterator moves past the ending '\0' (off-by-one buffer overflow) to set an additional '\0' and adds the first '\0' into the printbuf. Since '\0' will eventually be considered the terminating character of the complete printbuf result, all trailing characters are lost. This leads to an incomplete JSON string as can be seen with the test case. The off-by-one can be noticed if compiled with address sanitizer. Since this is a very special case and a malformed formatter could do way more harm and is the responsibility of the user of this library, this is just a protective measure to keep json-c code as robust as possible.
* Revert part of PR#606 and use isnan/isinf again, but provide macro ↵Eric Haszlakiewicz2020-05-161-6/+1
| | | | implementations of those in math_compat.h is needed, as it seems to be on AIX and IBM i systems.
* Improved support for IBM operating systemsDavid McCann2020-05-141-1/+8
| | | | Fix compiler errors and warnings when building on IBM operating systems such as AIX and IBM i.
* Re-do clang-format.Eric Haszlakiewicz2020-04-181-1/+0
|
* Make json_abort() internal to json_object.cEric Haszlakiewicz2020-04-181-0/+25
|
* Enforce strict prototypes.Björn Esser2020-04-131-1/+1
|
* Clean trailing white-space.Björn Esser2020-04-111-1/+1
|
* Fix one more assert("!invalid cint_type") to use json_abort(...) instead.Eric Haszlakiewicz2020-04-101-1/+1
|
* clang-format the filesdota172020-04-031-479/+451
|
* add the disabling formatting coments and adjust the partial code manulydota172020-04-031-20/+21
|
* Changed order of calloc args to match stdlibRobert2020-04-021-1/+1
| | | Although it is currently working, it's worth to stick with the stdlib definition to avoid further problems
* add test casesdota172020-03-311-1/+2
|