Commit graph

37 commits

Author SHA1 Message Date
Petr Štetiar
1ffa415353 blobmsg_json: prefer snprintf usage
Better safe than sorry and while at it prefer use of PRId16 and PRId32
formatting constants as well.

Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Petr Štetiar <ynezz@true.cz>
2020-01-20 16:54:10 +01:00
Dainis Jonitis
a36ee96618 blobmsg: blobmsg_add_json_element() 64-bit values
libjson-c json_type_int values are stored as int64_t. Use
json_object_get_int64() instead of json_object_get_int()
to avoid clamping to INT32_MAX.

Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Dainis Jonitis <dainis.jonitis@ubnt.com>
[fixed author to match SoB, added unit test results]
Signed-off-by: Petr Štetiar <ynezz@true.cz>
2020-01-20 16:16:49 +01:00
Petr Štetiar
f0da3a4283 blobmsg_json: fix int16 serialization
int16 blobmsg type is currently being serialized as uint16_t due to
missing cast during JSON output.

Following blobmsg content:

 bar-min: -32768 (i16)
 bar-max: 32767 (i16)

Produces following JSON:

 { "bar-min":32768,"bar-max":32767 }

Whereas one would expect:

 { "bar-min":-32768,"bar-max":32767 }

Reviewed-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Petr Štetiar <ynezz@true.cz>
2020-01-20 16:15:34 +01:00
Petr Štetiar
c008294a83 blobmsg_json: fix possible uninitialized struct member
clang-10 analyzer reports following:

 blobmsg_json.c:285:2: warning: The expression is an uninitialized value. The computed value will also be garbage
         s->indent_level++;
         ^~~~~~~~~~~~~~~~~

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-11-24 13:26:58 +01:00
Petr Štetiar
6228df9de9 iron out all extra compiler warnings
gcc-9 on x86/64 has reported following issues:

 base64.c:173:17: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:230:18: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:238:18: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:242:22: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:252:18: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:256:22: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:266:18: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:315:27: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 base64.c:329:15: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 blob.c:207:11: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
 blob.c:210:11: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
 blob.c:243:31: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]
 blob.c:246:31: error: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Werror=sign-compare]
 blob.h:245:37: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
 blob.h:253:37: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
 blobmsg.h:269:37: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
 blobmsg_json.c:155:10: error: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Werror=sign-compare]
 examples/../blob.h:245:37: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
 examples/../blobmsg.h:269:37: error: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Werror=sign-compare]
 json_script.c:590:7: error: this statement may fall through [-Werror=implicit-fallthrough=]

Signed-off-by: Petr Štetiar <ynezz@true.cz>
2019-11-20 14:34:01 +01:00
Yousong Zhou
eeef7b50a0 blobmsg_json: blobmsg_format_string: do not escape '/'
Resolves FS#2147

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2019-02-27 02:50:02 +00:00
André Gaul
7f671b1e68 blobmsg: add support for double
This adds support for double floating point type to make it more JSON
compatible. For type checking it also adds a stub BLOB_ATTR_DOUBLE type.
If necessary, the accessor functions for blob can be added later

Signed-off-by: André Gaul <andre@gaul.io>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2017-01-04 21:36:31 +01:00
Matthias Schiffer
d3fa561e5a blobmsg_json: add new functions blobmsg_format_json_value*
The current blobmsg_format_json* functions will return invalid JSON when
the "list" argument is given as false (blobmsg_format_element() will
output the name of the blob_attr as if the value is printed as part of a
JSON object).

To avoid breaking software relying on this behaviour, introduce new
functions which will never print the blob_attr name and thus always
produce valid JSON.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
Acked-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Felix Fietkau <nbd@nbd.name> [cosmetic style fix]
2016-07-02 10:10:08 +02:00
Matthias Schiffer
1f019ceea1 Fix various memory management issues
Consistently handle allocation failures. Some functions are changed to
return bool or int instead of void to allow returning an error.

Also fix a buffer size miscalculation in lua/uloop and use _exit() instead
of exit() on errors after forking.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2016-06-26 12:53:51 +02:00
Matthias Schiffer
155bf39896 blobmsg_json: simplify add_separator and fix thread-safety
The hard-coded length limits are replaced with strlen to make the code more
robust.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2016-04-06 17:43:47 +02:00
Jo-Philipp Wich
f43da92dd7 blobmsg_json: support json_type_null in blobmsg_add_json_element()
We already serialize BLOBMSG_TYPE_UNSPEC as "null" so represent JSON null
values as blob attribute type unspec with payload size zero.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
2016-02-09 23:30:32 +01:00
Felix Fietkau
6f2c688d68 blobmsg_json: include json.h inside blobmsg_json.c instead of the public header file
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-09-23 12:03:32 +02:00
Felix Fietkau
c8fc0897cd avoid using the deprecated is_error() function from json-c
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-09-23 12:02:31 +02:00
Felix Fietkau
411d10c845 blobmsg_json: use cross-platform macro for fprintf on 64-bit values
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-05-31 14:48:03 +02:00
Felix Fietkau
131b3352d9 blobmsg_json: let blobmsg_format_json_with_cb with list == true also format arrays
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-04-26 10:59:00 +02:00
Felix Fietkau
31b2d244fd blobmsg_json: fix a memleak on error
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-04-12 20:23:23 +02:00
Felix Fietkau
c78b684104 blobmsg_json: unconditionally use blobmsg data/len accessor functions
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2014-03-12 20:18:12 +01:00
Jo-Philipp Wich
f6827c7c93 blobmsg_json: do not emit any whitespace when formatting without indentation 2013-11-27 18:56:04 +00:00
Felix Fietkau
76906bec1c blobmsg_json: allow signed output of integers
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-10-04 16:55:45 +02:00
Felix Fietkau
6f192a6fb0 blobmsg_json: do not corrupt UTF-8 strings
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-06-21 17:19:37 +02:00
Felix Fietkau
7c5d2b3081 blobmsg_json: fix \u escaping for control characters
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-06-21 17:06:34 +02:00
Felix Fietkau
54a0b49326 blobmsg_json: add blobmsg_add_json_from_file
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-05-29 12:14:48 +02:00
Felix Fietkau
0bc317aa4d blobmsg_json: fix buffer growing on blobmsg json formatting 2013-01-29 23:07:10 +01:00
Felix Fietkau
4b5f278195 blobmsg: allow BLOBMSG_TYPE_UNSPEC attributes, treat them as null for JSON conversion
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-01-13 09:02:51 +01:00
Felix Fietkau
76f584c761 blobmsg_json: export blobmsg_add_object
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2013-01-06 02:15:48 +01:00
Felix Fietkau
6a2e4f449d switch blobmsg_json over to permissive license 2012-05-26 18:03:08 +02:00
Felix Fietkau
f565a7af42 blobmsg: add missing endian conversions 2011-11-17 00:47:54 +01:00
Felix Fietkau
5ce9b8912f blobmsg_json: print int16 elements in blobmsg_format_element (patch by Luka Perkov) 2011-11-17 00:40:37 +01:00
Felix Fietkau
abbc140e8d add BLOBMSG_TYPE_BOOL as an alias for BLOBMSG_TYPE_INT8 2011-07-29 12:56:48 +02:00
Felix Fietkau
ed9b5c986b blobmsg_json: support indenting of output data 2011-07-18 13:04:23 +02:00
Felix Fietkau
89122a2c1e remove int16, use int8 as bool instead of integer 2011-03-27 01:08:51 +01:00
Felix Fietkau
1ef7c81dfc add a cast to fix 64 bit issues 2011-02-07 20:30:52 +01:00
Felix Fietkau
81839e259a validate blobmsg attributes before converting them to json 2011-02-07 17:47:02 +01:00
Felix Fietkau
a5e2b50652 properly null-terminate formatted json strings 2011-02-07 01:18:56 +01:00
Felix Fietkau
0918243e90 move json formatting to the blobmsg_json library 2011-02-06 21:23:28 +01:00
Felix Fietkau
be41614060 add missing copyright headers 2011-02-06 16:20:57 +01:00
Felix Fietkau
7e9712363b add a json to blobmsg parsing library 2011-02-06 16:14:15 +01:00