batctl: genl_json bugfixes
cover batctl: genl_json bugfixes
I have now splitted the bugfixes for batctl in topic branches to make it a little bit easier to review. Similar to the "random bugfixes 2026-06-21" patchset, they are just various (mostly minor) problems which should be tackled at some point.
---
Sven Eckelmann (5):
batctl: genl_json: avoid negative chars in sanitize_string
batctl: genl_json: reject unknown options in handle_json_query
batctl: genl_json: don't return negative error codes to main
batctl: genl_json: escape non-printable characters as valid JSON
batctl: genl_json: drop leftover network coding log level
genl_json.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
---
base-commit: eb9597d4ca6db17c579f5ae9443c51b013e2fe65
change-id: 20260704-bugfixes-genl_json-6d36eb5d0c6d
Best regards,
Signature · 1 line
Sven Eckelmann <[email protected]>
PATCH 1/5 batctl: genl_json: avoid negative chars in sanitize_string
char is signed on common platforms. Passing a "negative" string byte to isprint() is undefined behavior when it is not EOF. The manpage for isprint is therefore requesting to provide the argument as unsigned char.
--- genl_json.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
genl_json.c +9 -7
@@ -33,16 +33,18 @@ struct nla_policy_json {static void sanitize_string (const char *str) { -while (*str) { -if (*str =='"' || *str =='\\' ) { +const unsigned char *c = (const unsigned char *)str; + +while (*c) { +if (*c =='"' || *c =='\\' ) {putchar ('\\' ); -putchar (*str); - }else if (!isprint (*str)) { -printf ("\\x%02x" , *str); +putchar (*c); + }else if (!isprint (*c)) { +printf ("\\x%02x" , *c); }else { -putchar (*str); +putchar (*c); } - str++; + c++; } }
Signature · 1 line
2.47.3
PATCH 2/5 batctl: genl_json: reject unknown options in handle_json_query
In reply to Sven EckelmannPrint the usage and return a failure for unknown options.
--- genl_json.c | 3 +++ 1 file changed, 3 insertions(+)
genl_json.c +3 -0
@@ -593,6 +593,9 @@ int handle_json_query(struct state *state, int argc, char **argv)case 'h' :json_query_usage (state);return EXIT_SUCCESS ; +default : +json_query_usage (state); +return EXIT_FAILURE ; } }
Signature · 1 line
2.47.3
PATCH 3/5 batctl: genl_json: don't return negative error codes to main
In reply to Sven Eckelmannhandle_json_query() returns the result of netlink_print_query_json() directly. This is a negative errno code on failure and main() passes it straight to exit(). The process then terminates with a mangled exit status like 161 (-95 & 0xff).
Limit the return codes to EXIT_SUCCESS and EXIT_FAILURE.
--- genl_json.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
genl_json.c +3 -1
@@ -600,6 +600,8 @@ int handle_json_query(struct state *state, int argc, char **argv) } err =netlink_print_query_json (state, json_query); +if (err <0 ) +return EXIT_FAILURE ; -return err; +return EXIT_SUCCESS ; }
Signature · 1 line
2.47.3
PATCH 4/5 batctl: genl_json: escape non-printable characters as valid JSON
In reply to Sven Eckelmannsanitize_string() emits non-printable bytes as \xNN inside JSON string values. JSON only allows the escape sequences \" \\ \/ \b \f \n \r \t and \uXXXX. The latter is the correct option for printing arbitrary character values.
--- genl_json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
genl_json.c +1 -1
@@ -40,7 +40,7 @@ static void sanitize_string(const char *str)putchar ('\\' );putchar (*c); }else if (!isprint (*c)) { -printf ("\\x%02x" , *c); +printf ("\\u%04x" , *c); }else {putchar (*c); }
Signature · 1 line
2.47.3
PATCH 5/5 batctl: genl_json: drop leftover network coding log level
In reply to Sven EckelmannSupport for the network coding feature was removed from batctl but missed in the genl_json loglevel output.
--- genl_json.c | 2 -- 1 file changed, 2 deletions(-)
genl_json.c +0 -2
@@ -219,8 +219,6 @@ static void nljson_print_loglevel(struct nlattr *attrs[], int idx) val & BIT(3) ? "true" : "false");printf ("\"dat\": %s," , val &BIT (4 ) ?"true" :"false" ); -printf ("\"nc\": %s," , - val &BIT (5 ) ?"true" :"false" );printf ("\"mcast\": %s," , val &BIT (6 ) ?"true" :"false" ); printf("\"tp\": %s,",
Signature · 1 line
2.47.3