Commit Graph

194 Commits

Author SHA1 Message Date
Eric Haszlakiewicz
0710c835a1 Reformat the json_object-split branch with clang-format 2020-06-16 13:17:58 +00:00
Eric Haszlakiewicz
5ebfeaedc5 Drop the _delete field from struct json_object and call the type-specific delete functions directly from json_object_put. (Thanks @dota17 for the suggestion in PR #632!) 2020-06-13 18:34:35 +00:00
Eric Haszlakiewicz
4c10712114 Drop the useless "char data[1]" from struct json_object. Fix a typo in a comment. 2020-06-13 18:25:32 +00:00
Eric Haszlakiewicz
66d91fdf86 The split of json_object into type-specific sub-structures is now 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.
2020-06-07 17:23:56 +00:00
Eric Haszlakiewicz
c4cc673071 More fixes for old MSVC builds. 2020-06-07 15:25:59 +00:00
Eric Haszlakiewicz
0fc9d91277 Kick json_type_string out of struct json_object.
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.
2020-06-07 02:56:59 +00:00
Eric Haszlakiewicz
9ecb1222bd Kick json_type_int and json_type_double out of struct json_object.
Clean up the code in json_object_new_* a bit by dropping unnecesary json_object_base variables.
2020-05-26 02:31:35 +00:00
Eric Haszlakiewicz
0351bb55c8 Declare variables earlier, to appease Visual Studio 2010. 2020-05-25 04:10:11 +00:00
Eric Haszlakiewicz
d1f83bf5ea Kick json_type_boolean out of struct json_object. 2020-05-25 04:05:32 +00:00
Eric Haszlakiewicz
5d89fc8a9d Add some backwards compat for Visual Studio 2013. 2020-05-25 03:52:36 +00:00
Eric Haszlakiewicz
02b687b9a6 Kick json_type_array out of struct json_object; re-enable the test_deep_copy test. 2020-05-25 03:44:56 +00:00
Eric Haszlakiewicz
853b4b5dee Start splitting struct json_object into multiple sub-types, as descibed at 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.
2020-05-25 03:18:36 +00:00
Tobias Stoeckmann
5385a566db Prevent truncation on custom double formatters.
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.
2020-05-16 15:26:16 +02:00
Eric Haszlakiewicz
0a3d22b9bb Revert part of PR#606 and use isnan/isinf again, but provide macro implementations of those in math_compat.h is needed, as it seems to be on AIX and IBM i systems. 2020-05-16 01:29:18 +00:00
David McCann
add7b13a9a Improved support for IBM operating systems
Fix compiler errors and warnings when building on IBM operating systems such as AIX and IBM i.
2020-05-14 15:39:35 +01:00
Eric Haszlakiewicz
ecb9354bb1 Re-do clang-format. 2020-04-18 02:14:13 +00:00
Eric Haszlakiewicz
23ddcbd4da Make json_abort() internal to json_object.c 2020-04-18 02:05:37 +00:00
Björn Esser
d0dc3489dc Enforce strict prototypes. 2020-04-13 11:29:52 +02:00
Björn Esser
78cd37fb18 Clean trailing white-space. 2020-04-11 09:41:04 +02:00
Eric Haszlakiewicz
0a070ef312 Fix one more assert("!invalid cint_type") to use json_abort(...) instead. 2020-04-10 01:26:04 +00:00
dota17
8b162c4b89 clang-format the files 2020-04-03 11:39:30 +08:00
dota17
c117d8a8a8 add the disabling formatting coments and adjust the partial code manuly 2020-04-03 11:28:04 +08:00
Robert
56f81811c2 Changed order of calloc args to match stdlib
Although it is currently working, it's worth to stick with the stdlib definition to avoid further problems
2020-04-02 19:23:10 +02:00
dota17
3822177473 add test cases 2020-03-31 19:12:45 +08:00
Jehan
b15e7ba470 Fixes various Wreturn-type and Wimplicit-fallthrough errors on Mingw-w64
This is a recent regression since commit
6359b79847 which added various assert(0)
calls (often replacing return-s).
With Ming-W64 compiler, json-c build was failing with various errors of
the sort:

> /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_int_inc':
> /home/jehan/dev/src/json-c/json_object.c:841:1: error: control reaches end of non-void function [-Werror=return-type]
>   841 | }
>       | ^
> In file included from /home/jehan/dev/src/json-c/json_object.c:17:
> /home/jehan/dev/src/json-c/json_object.c: In function 'json_object_get_double':
> /home/jehan/.local/share/crossroad/roads/w64/json-c/include/assert.h:76:4: error: this statement may fall through [-Werror=implicit-fallthrough=]
>    76 |   (_assert(#_Expression,__FILE__,__LINE__),0))
>       |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> /home/jehan/dev/src/json-c/json_object.c:1070:7: note: in expansion of macro 'assert'
>  1070 |       assert(0);
>       |       ^~~~~~
> /home/jehan/dev/src/json-c/json_object.c:1072:3: note: here
>  1072 |   case json_type_boolean:
>       |   ^~~~

The problem is that Mingw-w64 does not consider assert() as a noreturn
(even assert(0)), because it has to be compatible by Microsoft
libraries. See the discussion here:
https://sourceforge.net/p/mingw-w64/bugs/306/

Instead let's create a new json_abort() function which is basically just
an abort() function with an optional message, for such cases where
abortion was non-conditional (using assert() and using the assertion
condition as a message here was clearly a misuse of the function). And
mark json_abort() as 'noreturn', as well as 'cold' for optimization
purpose (this is code we expect to never run, unless there is a bug,
that is).

Finally let's use this json_abort() instead of previous misused assert()
calls.
2020-03-16 19:53:28 +01:00
Eric Haszlakiewicz
b3296e778f Follow up the PR#542: improve assert() calls, simplify code in json_object_equal(). 2020-03-02 02:08:49 +00:00
dota17
6359b79847 update json_object.c and testcase, delete json_object_uint_inc() 2020-02-28 17:51:56 +08:00
dota17
9532f94fa4 modify partial functions and testcase, in order to support automatic conversion for int64/uint64 2020-02-28 09:25:02 +08:00
dota17
c816de212b modify the json_object, replace c_int64/c_uint64 with struct{union{int64, uint64},...} 2020-02-27 10:26:27 +08:00
dota17
3c3b5920f7 add uint64 data to json-c 2020-02-25 14:51:35 +08:00
Eric Haszlakiewicz
518f337ce8 Issue #539: use a internal-only serializer function in json_object_new_double_s() to avoid potential conflicts with user code that uses the json_object_userdata_to_json_string serializer. Also, document the serializer-resetting behavior of json_object_set_double(). 2020-02-14 03:48:02 +00:00
Eric Haszlakiewicz
ae13ca524a Issue #539: reset the serializer when json_object_set_double() is called and the current serializer is the one that json_object_new_double_s() used. 2020-02-13 03:11:10 +00:00
dota17
010f33d460 add json_object_new_null 2020-01-20 16:46:46 +08:00
Eric Haszlakiewicz
7f30afc6e5 Fix some Windows compile issues, add JSON_EXPORT's, fix bogus character escapes, define __func__ and omit unistd.h if needed. 2019-11-23 20:31:14 -05:00
Eric Haszlakiewicz
d0b87ee87b Add an explicit cast to double to squash a -Wimplicit-int-float-conversion warning.
Though we will no longer be comparing exactly against INT64_MAX, this is ok
because any value of that magnitude stored in a double will *also* have been
rounded up, so the comparison will work appropriately.
2019-08-12 00:30:45 +00:00
Eric Haszlakiewicz
485f2a02c7 Issue #486: append a missing ".0" to negative double values too. 2019-05-28 02:44:22 +00:00
Eric Haszlakiewicz
ee4691c9fb Merge pull request #453 from darjankrijan/master
Fixed misalignment in JSON string due to space after \n being printed...
2018-12-11 20:17:43 -05:00
Eric Haszlakiewicz
240627f260 Merge pull request #454 from ramiropolla/json_object_private
json_object_private: save 8 bytes in struct json_object in 64-bit arc…
2018-11-30 09:49:35 -05:00
Eric Haszlakiewicz
745cadc944 Merge pull request #461 from andy5995/issue_422
json_object.c:set errno in json_object_get_double()
2018-11-28 20:39:42 -05:00
andy5995
3b108935d0 json_object.h:document json_object_new_string_len()
I also added a couple trivial, but related, suggestions.

closes #460
2018-11-27 11:35:45 -06:00
andy5995
506a32d4ab json_object.c:set errno in json_object_get_double()
closes #422
2018-11-26 21:12:06 -06:00
Ramiro Polla
5bb5e2e8fc json_object_private: save 8 bytes in struct json_object in 64-bit architectures
- there is no need for _ref_count to be uint_fast32_t (the compiler
  might decide to use a 64-bit int). make it uint32_t instead.
- reorder the 32-bit integer fields (o_type and _ref_count) so that
  there is no wasted 4-byte gap after each of them.
2018-11-24 04:16:36 +01:00
Darjan Krijan
3943960874 Removed spaces after \n for arrays as well 2018-11-21 22:34:01 +01:00
Darjan Krijan
e8cec5c9e4 Fixed misalignment in JSON string due to space after \n being printed when choosing JSON_C_TO_STRING_SPACED together with JSON_C_TO_STRING_PRETTY in json_object_array_to_json_string 2018-11-20 22:21:27 +01:00
Eric Haszlakiewicz
f8c632f579 Issue #407: fix incorrect casts in calls to ctype functions (isdigit and isspace) so we don't crash when asserts are enabled on certain platforms and characters > 128 are parsed. 2018-03-25 18:25:58 -04:00
Eric Haszlakiewicz
c652b6ad29 PR#394: fix breakage with VS build. 2017-12-24 14:42:58 -05:00
Eric Haszlakiewicz
d5da847f51 PR#394: don't always append the ".0" if the double value rounds to zero because some custom formats *will* include it (e.g. %.2f).
Also try to accomodate formats to explicitly exclude the decimal (e.g. %.0f).
2017-12-24 13:45:52 -05:00
Eric Haszlakiewicz
0992aac61f Remove the TRUE and FALSE defines. 2017-12-23 09:42:17 -05:00
Björn Esser
c233f5c05e json_object_private: Use unsigned 32-bit integer type for refcount 2017-12-17 12:51:17 +01:00
Björn Esser
9aca3b6a08 json_object: Avoid invalid free (and thus a segfault) when ref_count gets < 0 2017-12-14 14:36:07 +01:00