[OpenWrt-Devel] [PATCH libubox 4/4] blobmsg: blobmsg_add_json_element() 64-bit values

Petr Štetiar ynezz at true.cz
Sun Jan 12 17:55:15 EST 2020


From: Dainis Jonitis <dainis.jonitis at ubnt.com>

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.

Signed-off-by: Dainis Jonitis <dainis.jonitis at ubnt.com>
[fixed author to match SoB, added unit test results]
Signed-off-by: Petr Štetiar <ynezz at true.cz>
---
 blobmsg_json.c            | 10 ++++++++--
 tests/cram/test_blobmsg.t | 32 ++++++++++++++++----------------
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/blobmsg_json.c b/blobmsg_json.c
index aee7a64fbca7..aedc2da22ebc 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -66,9 +66,15 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object
 	case json_type_boolean:
 		blobmsg_add_u8(b, name, json_object_get_boolean(obj));
 		break;
-	case json_type_int:
-		blobmsg_add_u32(b, name, json_object_get_int(obj));
+	case json_type_int: {
+		int64_t i64 = json_object_get_int64(obj);
+		if (i64 >= INT32_MIN && i64 <= INT32_MAX) {
+			blobmsg_add_u32(b, name, (uint32_t)i64);
+		} else {
+			blobmsg_add_u64(b, name, (uint64_t)i64);
+		}
 		break;
+	}
 	case json_type_double:
 		blobmsg_add_double(b, name, json_object_get_double(obj));
 		break;
diff --git a/tests/cram/test_blobmsg.t b/tests/cram/test_blobmsg.t
index 84ec143d6ff1..74ad326931fe 100644
--- a/tests/cram/test_blobmsg.t
+++ b/tests/cram/test_blobmsg.t
@@ -46,8 +46,8 @@ check that blobmsg is producing expected results:
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
-  -2147483648 (i32)
-  2147483647 (i32)
+  -9223372036854775808 (i64)
+  9223372036854775807 (i64)
   133.700000 (dbl)
   }
   Testdata: {
@@ -60,8 +60,8 @@ check that blobmsg is producing expected results:
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (esc)
-  \ttaz-min : -2147483648 (i32) (esc)
-  \ttaz-max : 2147483647 (i32) (esc)
+  \ttaz-min : -9223372036854775808 (i64) (esc)
+  \ttaz-max : 9223372036854775807 (i64) (esc)
   \tworld : 2 (str) (esc)
   }
 
@@ -109,8 +109,8 @@ check that blobmsg is producing expected results:
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
-  -2147483648 (i32)
-  2147483647 (i32)
+  -9223372036854775808 (i64)
+  9223372036854775807 (i64)
   133.700000 (dbl)
   }
   Testdata: {
@@ -123,8 +123,8 @@ check that blobmsg is producing expected results:
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (esc)
-  \ttaz-min : -2147483648 (i32) (esc)
-  \ttaz-max : 2147483647 (i32) (esc)
+  \ttaz-min : -9223372036854775808 (i64) (esc)
+  \ttaz-max : 9223372036854775807 (i64) (esc)
   \tworld : 2 (str) (esc)
   }
 
@@ -172,8 +172,8 @@ check that blobmsg is producing expected results:
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
-  -2147483648 (i32)
-  2147483647 (i32)
+  -9223372036854775808 (i64)
+  9223372036854775807 (i64)
   133.700000 (dbl)
   }
   Testdata: {
@@ -186,8 +186,8 @@ check that blobmsg is producing expected results:
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (esc)
-  \ttaz-min : -2147483648 (i32) (esc)
-  \ttaz-max : 2147483647 (i32) (esc)
+  \ttaz-min : -9223372036854775808 (i64) (esc)
+  \ttaz-max : 9223372036854775807 (i64) (esc)
   \tworld : 2 (str) (esc)
   }
 
@@ -235,8 +235,8 @@ check that blobmsg is producing expected results:
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
-  -2147483648 (i32)
-  2147483647 (i32)
+  -9223372036854775808 (i64)
+  9223372036854775807 (i64)
   133.700000 (dbl)
   }
   Testdata: {
@@ -249,7 +249,7 @@ check that blobmsg is producing expected results:
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (esc)
-  \ttaz-min : -2147483648 (i32) (esc)
-  \ttaz-max : 2147483647 (i32) (esc)
+  \ttaz-min : -9223372036854775808 (i64) (esc)
+  \ttaz-max : 9223372036854775807 (i64) (esc)
   \tworld : 2 (str) (esc)
   }

_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list