[OpenWrt-Devel] [PATCH] blobmsg_json: handle conversion of large integers from JSON

Florian Larysch fl at n621.de
Wed Oct 19 06:10:34 EDT 2016

Currently, libubox uses json_object_get_int when converting a JSON
document into a blobmsg. However, json-c stores integers as int64_t
values and may clamp the value when asked to return it as an int32_t.

Always use json_object_get_int64 and use a u32/u64 blobmsg value
depending on the magnitude of the returned value.

Signed-off-by: Florian Larysch <fl at n621.de>
 blobmsg_json.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/blobmsg_json.c b/blobmsg_json.c
index 8a6ed8f..1ae4954 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -47,6 +47,7 @@ static bool blobmsg_add_array(struct blob_buf *b, struct array_list *a)
 bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object *obj)
 	bool ret = true;
+	int64_t n;
 	void *c;
 	switch (json_object_get_type(obj)) {
@@ -67,7 +68,13 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object
 		blobmsg_add_u8(b, name, json_object_get_boolean(obj));
 	case json_type_int:
-		blobmsg_add_u32(b, name, json_object_get_int(obj));
+		n = json_object_get_int64(obj);
+		if (n >= INT32_MIN && n <= INT32_MAX)
+			blobmsg_add_u32(b, name, n);
+		else
+			blobmsg_add_u64(b, name, n);
 	case json_type_null:
 		blobmsg_add_field(b, BLOBMSG_TYPE_UNSPEC, name, NULL, 0);
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org

More information about the openwrt-devel mailing list