From feb5dbc02e8df45fd1bf6258535ac8f1b74003ba Mon Sep 17 00:00:00 2001 From: Bastien BERNARD Date: Mon, 20 Apr 2026 11:00:50 +0200 Subject: [PATCH] libubox: add boolean type support Create a distinguished BLOBMSG_TYPE_BOOL in order to differenciate int8 and boolean types (both still use BLOB_ATTR_INT8 as storage format). --- blobmsg.c | 1 + blobmsg.h | 9 +++++++- blobmsg_json.c | 2 +- tests/fuzz/test-fuzz.c | 1 + tests/test-blobmsg-types.c | 44 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/blobmsg.c b/blobmsg.c index d87d607..b031ed1 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -17,6 +17,7 @@ static const int blob_type[__BLOBMSG_TYPE_LAST] = { [BLOBMSG_TYPE_INT8] = BLOB_ATTR_INT8, + [BLOBMSG_TYPE_BOOL] = BLOB_ATTR_INT8, [BLOBMSG_TYPE_INT16] = BLOB_ATTR_INT16, [BLOBMSG_TYPE_INT32] = BLOB_ATTR_INT32, [BLOBMSG_TYPE_INT64] = BLOB_ATTR_INT64, diff --git a/blobmsg.h b/blobmsg.h index a34c4f7..ba45f60 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -31,7 +31,7 @@ enum blobmsg_type { BLOBMSG_TYPE_INT32, BLOBMSG_TYPE_INT16, BLOBMSG_TYPE_INT8, - BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8, + BLOBMSG_TYPE_BOOL, BLOBMSG_TYPE_DOUBLE, __BLOBMSG_TYPE_LAST, BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - 1, @@ -231,6 +231,13 @@ blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val) return blobmsg_add_field(buf, BLOBMSG_TYPE_INT64, name, &val, 8); } +static inline int +blobmsg_add_bool(struct blob_buf *buf, const char *name, bool val) +{ + uint8_t v = val ? 1 : 0; + return blobmsg_add_field(buf, BLOBMSG_TYPE_BOOL, name, &v, 1); +} + static inline int blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string) { diff --git a/blobmsg_json.c b/blobmsg_json.c index 31eec09..b974254 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -60,7 +60,7 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object blobmsg_add_string(b, name, json_object_get_string(obj)); break; case json_type_boolean: - blobmsg_add_u8(b, name, json_object_get_boolean(obj)); + blobmsg_add_bool(b, name, json_object_get_boolean(obj)); break; case json_type_int: { int64_t i64 = json_object_get_int64(obj); diff --git a/tests/fuzz/test-fuzz.c b/tests/fuzz/test-fuzz.c index 026a3fd..3aa34f2 100644 --- a/tests/fuzz/test-fuzz.c +++ b/tests/fuzz/test-fuzz.c @@ -26,6 +26,7 @@ static void fuzz_blobmsg_parse(const uint8_t *data, size_t size) BLOBMSG_TYPE_INT32, BLOBMSG_TYPE_INT16, BLOBMSG_TYPE_INT8, + BLOBMSG_TYPE_BOOL, BLOBMSG_TYPE_DOUBLE, BLOBMSG_TYPE_TROUBLE, }; diff --git a/tests/test-blobmsg-types.c b/tests/test-blobmsg-types.c index 4d77a27..d3e47b9 100644 --- a/tests/test-blobmsg-types.c +++ b/tests/test-blobmsg-types.c @@ -19,6 +19,8 @@ enum { FOO_INT8_MIN, FOO_DOUBLE_MAX, FOO_DOUBLE_MIN, + FOO_BOOL_TRUE, + FOO_BOOL_FALSE, __FOO_MAX }; @@ -67,6 +69,14 @@ static const struct blobmsg_policy pol[] = { .name = "double_min", .type = BLOBMSG_TYPE_DOUBLE, }, + [FOO_BOOL_TRUE] = { + .name = "bool_true", + .type = BLOBMSG_TYPE_BOOL, + }, + [FOO_BOOL_FALSE] = { + .name = "bool_false", + .type = BLOBMSG_TYPE_BOOL, + }, }; static const struct blobmsg_policy pol_json[] = { @@ -114,6 +124,14 @@ static const struct blobmsg_policy pol_json[] = { .name = "double_min", .type = BLOBMSG_TYPE_DOUBLE, }, + [FOO_BOOL_TRUE] = { + .name = "bool_true", + .type = BLOBMSG_TYPE_BOOL, + }, + [FOO_BOOL_FALSE] = { + .name = "bool_false", + .type = BLOBMSG_TYPE_BOOL, + }, }; #ifndef ARRAY_SIZE @@ -150,6 +168,10 @@ static void dump_message(struct blob_buf *buf) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN])); + if (tb[FOO_BOOL_TRUE]) + fprintf(stderr, "bool_true: %s\n", blobmsg_get_bool(tb[FOO_BOOL_TRUE]) ? "true" : "false"); + if (tb[FOO_BOOL_FALSE]) + fprintf(stderr, "bool_false: %s\n", blobmsg_get_bool(tb[FOO_BOOL_FALSE]) ? "true" : "false"); } static void dump_message_cast_u64(struct blob_buf *buf) @@ -182,6 +204,10 @@ static void dump_message_cast_u64(struct blob_buf *buf) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN])); + if (tb[FOO_BOOL_TRUE]) + fprintf(stderr, "bool_true: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_TRUE])); + if (tb[FOO_BOOL_FALSE]) + fprintf(stderr, "bool_false: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_FALSE])); } static void dump_message_cast_s64(struct blob_buf *buf) @@ -214,6 +240,10 @@ static void dump_message_cast_s64(struct blob_buf *buf) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN])); + if (tb[FOO_BOOL_TRUE]) + fprintf(stderr, "bool_true: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_TRUE])); + if (tb[FOO_BOOL_FALSE]) + fprintf(stderr, "bool_false: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_FALSE])); } static void dump_message_json(struct blob_buf *buf) @@ -250,6 +280,10 @@ static void dump_message_json(struct blob_buf *buf) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN])); + if (tb[FOO_BOOL_TRUE]) + fprintf(stderr, "bool_true: %s\n", blobmsg_get_bool(tb[FOO_BOOL_TRUE]) ? "true" : "false"); + if (tb[FOO_BOOL_FALSE]) + fprintf(stderr, "bool_false: %s\n", blobmsg_get_bool(tb[FOO_BOOL_FALSE]) ? "true" : "false"); } static void dump_message_cast_u64_json(struct blob_buf *buf) @@ -282,6 +316,10 @@ static void dump_message_cast_u64_json(struct blob_buf *buf) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN])); + if (tb[FOO_BOOL_TRUE]) + fprintf(stderr, "bool_true: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_TRUE])); + if (tb[FOO_BOOL_FALSE]) + fprintf(stderr, "bool_false: %" PRIu64 "\n", blobmsg_cast_u64(tb[FOO_BOOL_FALSE])); } static void dump_message_cast_s64_json(struct blob_buf *buf) @@ -314,6 +352,10 @@ static void dump_message_cast_s64_json(struct blob_buf *buf) fprintf(stderr, "double_max: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MAX])); if (tb[FOO_DOUBLE_MIN]) fprintf(stderr, "double_min: %e\n", blobmsg_get_double(tb[FOO_DOUBLE_MIN])); + if (tb[FOO_BOOL_TRUE]) + fprintf(stderr, "bool_true: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_TRUE])); + if (tb[FOO_BOOL_FALSE]) + fprintf(stderr, "bool_false: %" PRId64 "\n", blobmsg_cast_s64(tb[FOO_BOOL_FALSE])); } static void @@ -330,6 +372,8 @@ fill_message(struct blob_buf *buf) blobmsg_add_u8(buf, "int8_min", INT8_MIN); blobmsg_add_double(buf, "double_max", DBL_MAX); blobmsg_add_double(buf, "double_min", DBL_MIN); + blobmsg_add_bool(buf, "bool_true", true); + blobmsg_add_bool(buf, "bool_false", false); } int main(int argc, char **argv)