From: Petr Štetiar Date: Tue, 14 Jan 2020 08:05:02 +0000 (+0100) Subject: blobmsg_json: prefer snprintf usage X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=1ffa41535369f5bb67d1eb5bdcb306671ca1d2e4;p=oweals%2Flibubox.git 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 Signed-off-by: Petr Štetiar --- diff --git a/blobmsg_json.c b/blobmsg_json.c index aedc2da..dce81e9 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -208,7 +208,7 @@ static void blobmsg_format_string(struct strbuf *s, const char *str) buf[1] = escape; if (escape == 'u') { - sprintf(buf + 4, "%02x", (unsigned char) *p); + snprintf(buf + 4, sizeof(buf) - 4, "%02x", (unsigned char) *p); len = 6; } else { len = 2; @@ -225,7 +225,7 @@ static void blobmsg_format_json_list(struct strbuf *s, struct blob_attr *attr, i static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, bool without_name, bool head) { const char *data_str; - char buf[32]; + char buf[317]; void *data; int len; @@ -249,22 +249,22 @@ static void blobmsg_format_element(struct strbuf *s, struct blob_attr *attr, boo data_str = buf; switch(blob_id(attr)) { case BLOBMSG_TYPE_UNSPEC: - sprintf(buf, "null"); + snprintf(buf, sizeof(buf), "null"); break; case BLOBMSG_TYPE_BOOL: - sprintf(buf, "%s", *(uint8_t *)data ? "true" : "false"); + snprintf(buf, sizeof(buf), "%s", *(uint8_t *)data ? "true" : "false"); break; case BLOBMSG_TYPE_INT16: - sprintf(buf, "%d", (int16_t) be16_to_cpu(*(uint16_t *)data)); + snprintf(buf, sizeof(buf), "%" PRId16, (int16_t) be16_to_cpu(*(uint16_t *)data)); break; case BLOBMSG_TYPE_INT32: - sprintf(buf, "%d", (int32_t) be32_to_cpu(*(uint32_t *)data)); + snprintf(buf, sizeof(buf), "%" PRId32, (int32_t) be32_to_cpu(*(uint32_t *)data)); break; case BLOBMSG_TYPE_INT64: - sprintf(buf, "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data)); + snprintf(buf, sizeof(buf), "%" PRId64, (int64_t) be64_to_cpu(*(uint64_t *)data)); break; case BLOBMSG_TYPE_DOUBLE: - sprintf(buf, "%lf", blobmsg_get_double(attr)); + snprintf(buf, sizeof(buf), "%lf", blobmsg_get_double(attr)); break; case BLOBMSG_TYPE_STRING: blobmsg_format_string(s, data); diff --git a/tests/cram/test_blobmsg.t b/tests/cram/test_blobmsg.t index 74ad326..0c192c5 100644 --- a/tests/cram/test_blobmsg.t +++ b/tests/cram/test_blobmsg.t @@ -16,10 +16,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 100 (i8) (esc) \tmoo-min : -128 (i8) (esc) @@ -33,7 +35,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":{"dbl-min":0.000000,"dbl-max":179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000,"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,0.000000,179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000]} [*] blobmsg from json: Message: Hello, world! @@ -48,10 +50,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 1 (i8) (esc) \tmoo-min : 1 (i8) (esc) @@ -79,10 +83,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 100 (i8) (esc) \tmoo-min : -128 (i8) (esc) @@ -96,7 +102,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":{"dbl-min":0.000000,"dbl-max":179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000,"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,0.000000,179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000]} [*] blobmsg from json: Message: Hello, world! @@ -111,10 +117,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 1 (i8) (esc) \tmoo-min : 1 (i8) (esc) @@ -142,10 +150,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 100 (i8) (esc) \tmoo-min : -128 (i8) (esc) @@ -159,7 +169,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":{"dbl-min":0.000000,"dbl-max":179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000,"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,0.000000,179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000]} [*] blobmsg from json: Message: Hello, world! @@ -174,10 +184,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 1 (i8) (esc) \tmoo-min : 1 (i8) (esc) @@ -205,10 +217,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 100 (i8) (esc) \tmoo-min : -128 (i8) (esc) @@ -222,7 +236,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":{"dbl-min":0.000000,"dbl-max":179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000,"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,0.000000,179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000]} [*] blobmsg from json: Message: Hello, world! @@ -237,10 +251,12 @@ check that blobmsg is producing expected results: 2147483647 (i32) -9223372036854775808 (i64) 9223372036854775807 (i64) - 133.700000 (dbl) + 0.000000 (dbl) + 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) } Testdata: { - \tdouble : 133.700000 (dbl) (esc) + \tdbl-min : 0.000000 (dbl) (esc) + \tdbl-max : 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 (dbl) (esc) \tfoo : 0 (i8) (esc) \tpoo : 1 (i8) (esc) \tmoo-min : 1 (i8) (esc) diff --git a/tests/test-blobmsg.c b/tests/test-blobmsg.c index 58b0bc4..2224853 100644 --- a/tests/test-blobmsg.c +++ b/tests/test-blobmsg.c @@ -1,4 +1,6 @@ #include +#include +#include #include #include @@ -117,7 +119,8 @@ fill_message(struct blob_buf *buf) blobmsg_add_string(buf, "message", "Hello, world!"); tbl = blobmsg_open_table(buf, "testdata"); - blobmsg_add_double(buf, "double", 1.337e2); + blobmsg_add_double(buf, "dbl-min", DBL_MIN); + blobmsg_add_double(buf, "dbl-max", DBL_MAX); blobmsg_add_u8(buf, "foo", 0); blobmsg_add_u8(buf, "poo", 100); blobmsg_add_u8(buf, "moo-min", INT8_MIN); @@ -142,7 +145,8 @@ fill_message(struct blob_buf *buf) blobmsg_add_u32(buf, NULL, INT32_MAX); blobmsg_add_u64(buf, NULL, INT64_MIN); blobmsg_add_u64(buf, NULL, INT64_MAX); - blobmsg_add_double(buf, "double", 1.337e2); + blobmsg_add_double(buf, NULL, DBL_MIN); + blobmsg_add_double(buf, NULL, DBL_MAX); blobmsg_close_table(buf, tbl); }