[OpenWrt-Devel] [PATCH libubox 3/4] blobmsg_json: fix int16 serialization

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


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 }

Signed-off-by: Petr Štetiar <ynezz at true.cz>
---
 blobmsg_json.c            |  2 +-
 tests/cram/test_blobmsg.t | 24 ++++++++++++------------
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/blobmsg_json.c b/blobmsg_json.c
index 18592111b4dd..aee7a64fbca7 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -249,7 +249,7 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo
 		sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false");
 		break;
 	case BLOBMSG_TYPE_INT16:
-		sprintf(buf, "%d", be16_to_cpu(*(uint16_t *)data));
+		sprintf(buf, "%d", (int16_t) be16_to_cpu(*(uint16_t *)data));
 		break;
 	case BLOBMSG_TYPE_INT32:
 		sprintf(buf, "%d", (int32_t) be32_to_cpu(*(uint32_t *)data));
diff --git a/tests/cram/test_blobmsg.t b/tests/cram/test_blobmsg.t
index 9ab2acf6e2d8..84ec143d6ff1 100644
--- a/tests/cram/test_blobmsg.t
+++ b/tests/cram/test_blobmsg.t
@@ -33,7 +33,7 @@ check that blobmsg is producing expected results:
   \tworld : 2 (str) (esc)
   }
   
-  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
+  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
   
   [*] blobmsg from json:
   Message: Hello, world!
@@ -42,7 +42,7 @@ check that blobmsg is producing expected results:
   1 (i8)
   1 (i8)
   1 (i8)
-  32768 (i32)
+  -32768 (i32)
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
@@ -56,7 +56,7 @@ check that blobmsg is producing expected results:
   \tpoo : 1 (i8) (esc)
   \tmoo-min : 1 (i8) (esc)
   \tmoo-max : 1 (i8) (esc)
-  \tbar-min : 32768 (i32) (esc)
+  \tbar-min : -32768 (i32) (esc)
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (esc)
@@ -96,7 +96,7 @@ check that blobmsg is producing expected results:
   \tworld : 2 (str) (esc)
   }
   
-  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
+  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
   
   [*] blobmsg from json:
   Message: Hello, world!
@@ -105,7 +105,7 @@ check that blobmsg is producing expected results:
   1 (i8)
   1 (i8)
   1 (i8)
-  32768 (i32)
+  -32768 (i32)
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
@@ -119,7 +119,7 @@ check that blobmsg is producing expected results:
   \tpoo : 1 (i8) (esc)
   \tmoo-min : 1 (i8) (esc)
   \tmoo-max : 1 (i8) (esc)
-  \tbar-min : 32768 (i32) (esc)
+  \tbar-min : -32768 (i32) (esc)
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (esc)
@@ -159,7 +159,7 @@ check that blobmsg is producing expected results:
   \tworld : 2 (str) (esc)
   }
   
-  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
+  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
   
   [*] blobmsg from json:
   Message: Hello, world!
@@ -168,7 +168,7 @@ check that blobmsg is producing expected results:
   1 (i8)
   1 (i8)
   1 (i8)
-  32768 (i32)
+  -32768 (i32)
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
@@ -182,7 +182,7 @@ check that blobmsg is producing expected results:
   \tpoo : 1 (i8) (esc)
   \tmoo-min : 1 (i8) (esc)
   \tmoo-max : 1 (i8) (esc)
-  \tbar-min : 32768 (i32) (esc)
+  \tbar-min : -32768 (i32) (esc)
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (esc)
@@ -222,7 +222,7 @@ check that blobmsg is producing expected results:
   \tworld : 2 (str) (esc)
   }
   
-  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
+  [*] blobmsg to json: {"message":"Hello, world!","testdata":{"double":133.700000,"foo":false,"poo":true,"moo-min":true,"moo-max":true,"bar-min":-32768,"bar-max":32767,"baz-min":-2147483648,"baz-max":2147483647,"taz-min":-9223372036854775808,"taz-max":9223372036854775807,"world":"2"},"list":[false,true,true,true,-32768,32767,-2147483648,2147483647,-9223372036854775808,9223372036854775807,133.700000]}
   
   [*] blobmsg from json:
   Message: Hello, world!
@@ -231,7 +231,7 @@ check that blobmsg is producing expected results:
   1 (i8)
   1 (i8)
   1 (i8)
-  32768 (i32)
+  -32768 (i32)
   32767 (i32)
   -2147483648 (i32)
   2147483647 (i32)
@@ -245,7 +245,7 @@ check that blobmsg is producing expected results:
   \tpoo : 1 (i8) (esc)
   \tmoo-min : 1 (i8) (esc)
   \tmoo-max : 1 (i8) (esc)
-  \tbar-min : 32768 (i32) (esc)
+  \tbar-min : -32768 (i32) (esc)
   \tbar-max : 32767 (i32) (esc)
   \tbaz-min : -2147483648 (i32) (esc)
   \tbaz-max : 2147483647 (i32) (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