All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] batctl: debug tables bugfixes
@ 2026-07-04 19:35 Sven Eckelmann
  2026-07-04 19:35 ` [PATCH 1/5] batctl: debug: avoid endless getopt loop for attached '-w' argument Sven Eckelmann
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Sven Eckelmann @ 2026-07-04 19:35 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Sven Eckelmann

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.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
Sven Eckelmann (5):
      batctl: debug: avoid endless getopt loop for attached '-w' argument
      batctl: debug: use strict interval/timeout parsing
      batctl: debug: reject trailing garbage for intervals
      batctl: debug: reject non-finite/negative interval/timeout values
      batctl: debug: don't return negative error codes from handle_debug_table

 debug.c      | 29 ++++++++++++++++-------------
 man/batctl.8 | 18 +++++++++---------
 2 files changed, 25 insertions(+), 22 deletions(-)
---
base-commit: e93995999e80513db80eba200ea682b5b15556af
change-id: 20260704-bugfixes-debug-949459f9bf75

Best regards,
--  
Sven Eckelmann <sven@narfation.org>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] batctl: debug: avoid endless getopt loop for attached '-w' argument
  2026-07-04 19:35 [PATCH 0/5] batctl: debug tables bugfixes Sven Eckelmann
@ 2026-07-04 19:35 ` Sven Eckelmann
  2026-07-04 19:36 ` [PATCH 2/5] batctl: debug: use strict interval/timeout parsing Sven Eckelmann
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Sven Eckelmann @ 2026-07-04 19:35 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Sven Eckelmann

The watch-mode "optinaparsing hack is not stable and breaks the assumptions
of getopt parsers. If something like "-w-" was specified as argument, then
the glibc and musl parsers would just cause an endless loop. And when no
argument was specified, an error "option requires an argument: w" was
always appearing.

It is better to use the optional parameter support which is directly
provided by glibc and musl:

  batctl meshif bat0 originators -w
  batctl meshif bat0 originators -w2.3

Fixes: e4a7b7733faf ("batctl: Add an optional interval for watch-mode")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 debug.c      | 12 ++++--------
 man/batctl.8 | 18 +++++++++---------
 2 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/debug.c b/debug.c
index 163a46b..bca81c8 100644
--- a/debug.c
+++ b/debug.c
@@ -26,7 +26,7 @@ static void debug_table_usage(struct state *state)
 	fprintf(stderr, " \t -h print this help\n");
 	fprintf(stderr, " \t -n don't replace mac addresses with bat-host names\n");
 	fprintf(stderr, " \t -H don't show the header\n");
-	fprintf(stderr, " \t -w [interval] watch mode - refresh the table continuously\n");
+	fprintf(stderr, " \t -w[interval] watch mode - refresh the table continuously\n");
 
 	if (debug_table->option_timeout_interval)
 		fprintf(stderr,
@@ -53,7 +53,8 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 	int optchar;
 	int err;
 
-	while ((optchar = getopt(argc, argv, "hnw:t:Humi:")) != -1) {
+	while ((optchar = getopt(argc, argv, "hnw::t:Humi:")) != -1) {
+		printf("%c\n", optchar);
 		switch (optchar) {
 		case 'h':
 			debug_table_usage(state);
@@ -63,10 +64,8 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			break;
 		case 'w':
 			read_opt |= CLR_CONT_READ;
-			if (optarg[0] == '-') {
-				optind--;
+			if (!optarg)
 				break;
-			}
 
 			if (!sscanf(optarg, "%f", &watch_interval)) {
 				fprintf(stderr,
@@ -130,9 +129,6 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			} else if (optopt == 'i') {
 				fprintf(stderr,
 					"Error - option '-i' needs an interface as argument\n");
-			} else if (optopt == 'w') {
-				read_opt |= CLR_CONT_READ;
-				break;
 			} else {
 				fprintf(stderr, "Error - unrecognised option: '-%c'\n", optopt);
 			}
diff --git a/man/batctl.8 b/man/batctl.8
index 54745da..c0f3aa0 100644
--- a/man/batctl.8
+++ b/man/batctl.8
@@ -377,27 +377,27 @@ is printed.
 The local and global translation tables also support the "\-u" and "\-m" option to only display unicast or multicast translation table announcements respectively.
 
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBbackbonetable\fP|\fBbbt\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBbackbonetable\fP|\fBbbt\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP\fIinterval\fP]
 (compile time option)
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBclaimtable\fP|\fBcl\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBclaimtable\fP|\fBcl\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]]
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBdat_cache\fP|\fBdc\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBdat_cache\fP|\fBdc\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]]
 (compile time option)
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBgateways\fP|\fBgwl\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBgateways\fP|\fBgwl\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]]
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBmcast_flags\fP|\fBmf\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBmcast_flags\fP|\fBmf\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]]
 (compile time option)
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBneighbors\fP|\fBn\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBneighbors\fP|\fBn\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]]
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBoriginators\fP|\fBo\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP] [\fB-t\fP \fItimeout_interval\fP] [\fB-i\fP \fIinterface\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBoriginators\fP|\fBo\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]] [\fB-t\fP \fItimeout_interval\fP] [\fB-i\fP \fIinterface\fP]
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBtransglobal\fP|\fBtg\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP] [\fB-u\fP] [\fB-m\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBtransglobal\fP|\fBtg\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]] [\fB-u\fP] [\fB-m\fP]
 (compile time option)
 .TP
-[\fBmeshif\fP \fInetdev\fP] \fBtranslocal\fP|\fBtl\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP \fIinterval\fP] [\fB-u\fP] [\fB-m\fP]
+[\fBmeshif\fP \fInetdev\fP] \fBtranslocal\fP|\fBtl\fP [\fB-n\fP] [\fB-H\fP] [\fB-w\fP[\fIinterval\fP]] [\fB-u\fP] [\fB-m\fP]
 
 .SH JSON QUERIES
 

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/5] batctl: debug: use strict interval/timeout parsing
  2026-07-04 19:35 [PATCH 0/5] batctl: debug tables bugfixes Sven Eckelmann
  2026-07-04 19:35 ` [PATCH 1/5] batctl: debug: avoid endless getopt loop for attached '-w' argument Sven Eckelmann
@ 2026-07-04 19:36 ` Sven Eckelmann
  2026-07-04 19:36 ` [PATCH 3/5] batctl: debug: reject trailing garbage for intervals Sven Eckelmann
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Sven Eckelmann @ 2026-07-04 19:36 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Sven Eckelmann

The sscanf() function does not only return 0 when the value could not be
parsed from the string. Also EOF is a potential return value. To make the
check more strict, just compare the returned value with the expected number
of scanned/parsed items.

Fixes: 302a41a73915 ("batctl: Add timeout filtering option for originators")
Fixes: e4a7b7733faf ("batctl: Add an optional interval for watch-mode")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 debug.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/debug.c b/debug.c
index bca81c8..ae13dce 100644
--- a/debug.c
+++ b/debug.c
@@ -67,7 +67,7 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			if (!optarg)
 				break;
 
-			if (!sscanf(optarg, "%f", &watch_interval)) {
+			if (sscanf(optarg, "%f", &watch_interval) != 1) {
 				fprintf(stderr,
 					"Error - provided argument of '-%c' is not a number\n",
 					optchar);
@@ -82,7 +82,7 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			}
 
 			read_opt |= NO_OLD_ORIGS;
-			if (!sscanf(optarg, "%f", &orig_timeout)) {
+			if (sscanf(optarg, "%f", &orig_timeout) != 1) {
 				fprintf(stderr,
 					"Error - provided argument of '-%c' is not a number\n",
 					optchar);

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/5] batctl: debug: reject trailing garbage for intervals
  2026-07-04 19:35 [PATCH 0/5] batctl: debug tables bugfixes Sven Eckelmann
  2026-07-04 19:35 ` [PATCH 1/5] batctl: debug: avoid endless getopt loop for attached '-w' argument Sven Eckelmann
  2026-07-04 19:36 ` [PATCH 2/5] batctl: debug: use strict interval/timeout parsing Sven Eckelmann
@ 2026-07-04 19:36 ` Sven Eckelmann
  2026-07-04 19:36 ` [PATCH 4/5] batctl: debug: reject non-finite/negative interval/timeout values Sven Eckelmann
  2026-07-04 19:36 ` [PATCH 5/5] batctl: debug: don't return negative error codes from handle_debug_table Sven Eckelmann
  4 siblings, 0 replies; 6+ messages in thread
From: Sven Eckelmann @ 2026-07-04 19:36 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Sven Eckelmann

 A "%f" sscanf conversion stops at the first
character it cannot consume and reports success for the leading numeric
part, so trailing garbag are still accepted silently.

Append a "%c" conversion so any trailing character makes sscanf() report
more than the one expected item.

Fixes: 302a41a73915 ("batctl: Add timeout filtering option for originators")
Fixes: e4a7b7733faf ("batctl: Add an optional interval for watch-mode")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 debug.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/debug.c b/debug.c
index ae13dce..1cceea6 100644
--- a/debug.c
+++ b/debug.c
@@ -51,6 +51,7 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 	float watch_interval = 1;
 	char *orig_iface = NULL;
 	int optchar;
+	char tmp;
 	int err;
 
 	while ((optchar = getopt(argc, argv, "hnw::t:Humi:")) != -1) {
@@ -67,7 +68,7 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			if (!optarg)
 				break;
 
-			if (sscanf(optarg, "%f", &watch_interval) != 1) {
+			if (sscanf(optarg, "%f%c", &watch_interval, &tmp) != 1) {
 				fprintf(stderr,
 					"Error - provided argument of '-%c' is not a number\n",
 					optchar);
@@ -82,7 +83,7 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			}
 
 			read_opt |= NO_OLD_ORIGS;
-			if (sscanf(optarg, "%f", &orig_timeout) != 1) {
+			if (sscanf(optarg, "%f%c", &orig_timeout, &tmp) != 1) {
 				fprintf(stderr,
 					"Error - provided argument of '-%c' is not a number\n",
 					optchar);

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/5] batctl: debug: reject non-finite/negative interval/timeout values
  2026-07-04 19:35 [PATCH 0/5] batctl: debug tables bugfixes Sven Eckelmann
                   ` (2 preceding siblings ...)
  2026-07-04 19:36 ` [PATCH 3/5] batctl: debug: reject trailing garbage for intervals Sven Eckelmann
@ 2026-07-04 19:36 ` Sven Eckelmann
  2026-07-04 19:36 ` [PATCH 5/5] batctl: debug: don't return negative error codes from handle_debug_table Sven Eckelmann
  4 siblings, 0 replies; 6+ messages in thread
From: Sven Eckelmann @ 2026-07-04 19:36 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Sven Eckelmann

A "%f" sscanf conversion accepts the textual floating point literals "nan",
"inf" and "infinity", and an out-of-range magnitude such as "1e9999" is
converted to infinity. There are calculations and comparisons performed
with the values. They must therefore be well defined non-negative and
finite numbers.

Fixes: 302a41a73915 ("batctl: Add timeout filtering option for originators")
Fixes: e4a7b7733faf ("batctl: Add an optional interval for watch-mode")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 debug.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/debug.c b/debug.c
index 1cceea6..22cbcb8 100644
--- a/debug.c
+++ b/debug.c
@@ -10,6 +10,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <math.h>
 
 #include "debug.h"
 #include "functions.h"
@@ -68,9 +69,10 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			if (!optarg)
 				break;
 
-			if (sscanf(optarg, "%f%c", &watch_interval, &tmp) != 1) {
+			if (sscanf(optarg, "%f%c", &watch_interval, &tmp) != 1 ||
+			    !isfinite(watch_interval) || watch_interval < 0) {
 				fprintf(stderr,
-					"Error - provided argument of '-%c' is not a number\n",
+					"Error - provided argument of '-%c' is not a positive number\n",
 					optchar);
 				return EXIT_FAILURE;
 			}
@@ -83,9 +85,10 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			}
 
 			read_opt |= NO_OLD_ORIGS;
-			if (sscanf(optarg, "%f%c", &orig_timeout, &tmp) != 1) {
+			if (sscanf(optarg, "%f%c", &orig_timeout, &tmp) != 1 ||
+			    !isfinite(orig_timeout) || orig_timeout < 0) {
 				fprintf(stderr,
-					"Error - provided argument of '-%c' is not a number\n",
+					"Error - provided argument of '-%c' is not a positive number\n",
 					optchar);
 				return EXIT_FAILURE;
 			}

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 5/5] batctl: debug: don't return negative error codes from handle_debug_table
  2026-07-04 19:35 [PATCH 0/5] batctl: debug tables bugfixes Sven Eckelmann
                   ` (3 preceding siblings ...)
  2026-07-04 19:36 ` [PATCH 4/5] batctl: debug: reject non-finite/negative interval/timeout values Sven Eckelmann
@ 2026-07-04 19:36 ` Sven Eckelmann
  4 siblings, 0 replies; 6+ messages in thread
From: Sven Eckelmann @ 2026-07-04 19:36 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Sven Eckelmann

handle_debug_table() returns the result of the netlink dump function
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.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
 debug.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/debug.c b/debug.c
index 22cbcb8..13a0a43 100644
--- a/debug.c
+++ b/debug.c
@@ -152,5 +152,8 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 
 	err = debug_table->netlink_fn(state, orig_iface, read_opt,
 				      orig_timeout, watch_interval);
-	return err;
+	if (err < 0)
+		return EXIT_FAILURE;
+
+	return EXIT_SUCCESS;
 }

-- 
2.47.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-07-04 19:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-04 19:35 [PATCH 0/5] batctl: debug tables bugfixes Sven Eckelmann
2026-07-04 19:35 ` [PATCH 1/5] batctl: debug: avoid endless getopt loop for attached '-w' argument Sven Eckelmann
2026-07-04 19:36 ` [PATCH 2/5] batctl: debug: use strict interval/timeout parsing Sven Eckelmann
2026-07-04 19:36 ` [PATCH 3/5] batctl: debug: reject trailing garbage for intervals Sven Eckelmann
2026-07-04 19:36 ` [PATCH 4/5] batctl: debug: reject non-finite/negative interval/timeout values Sven Eckelmann
2026-07-04 19:36 ` [PATCH 5/5] batctl: debug: don't return negative error codes from handle_debug_table Sven Eckelmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.