Skip to contentSkip to conversation

batctl: genl_json bugfixes

PATCH New Sven Eckelmann 1 participant 6 messages Jul 5

lore
Share & tools
lore
#0 lore raw

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]>
#1 lore raw

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.

Fixes ae1a3d3f0bb7 ("batctl: genl_json: Add generic JSON interface")
---
 genl_json.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
genl_json.c +9 -7
index 8aa97b4..ee83ab2 100644
@@ -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
#2 lore raw

PATCH 2/5 batctl: genl_json: reject unknown options in handle_json_query

In reply to Sven Eckelmann

Print the usage and return a failure for unknown options.

Fixes 57cc3c472a7d ("batctl: Introduce handler for JSON_* command types")
---
 genl_json.c | 3 +++
 1 file changed, 3 insertions(+)
genl_json.c +3 -0
index ee83ab2..356c5d7 100644
@@ -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
#3 lore raw

PATCH 3/5 batctl: genl_json: don't return negative error codes to main

In reply to Sven Eckelmann

handle_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.

Fixes 57cc3c472a7d ("batctl: Introduce handler for JSON_* command types")
---
 genl_json.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
genl_json.c +3 -1
index 356c5d7..917b255 100644
@@ -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
#4 lore raw

PATCH 4/5 batctl: genl_json: escape non-printable characters as valid JSON

In reply to Sven Eckelmann

sanitize_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.

Fixes ae1a3d3f0bb7 ("batctl: genl_json: Add generic JSON interface")
---
 genl_json.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
genl_json.c +1 -1
index 917b255..bb32cab 100644
@@ -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
#5 lore raw

PATCH 5/5 batctl: genl_json: drop leftover network coding log level

In reply to Sven Eckelmann

Support for the network coding feature was removed from batctl but missed in the genl_json loglevel output.

Fixes 882ab0d91468 ("batctl: remove support for network coding")
---
 genl_json.c | 2 --
 1 file changed, 2 deletions(-)
genl_json.c +0 -2
index bb32cab..c3ef83a 100644
@@ -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