Skip to contentSkip to conversation

batctl: version bugfixes

PATCH New Sven Eckelmann 1 participant 4 messages Jul 5

lore
Share & tools
lore
#0 lore raw

cover batctl: version 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 (3):
batctl: only mark file read successful on read line
batctl: version: avoid use of uninitialized read buffer
batctl: version: don't strip newline for empty buffer

 functions.c |  5 ++---
 main.c      | 11 +++++++----
 2 files changed, 9 insertions(+), 7 deletions(-)

---
base-commit: eb9597d4ca6db17c579f5ae9443c51b013e2fe65
change-id: 20260704-bugfixes-version-c3d2d1567bbe

Best regards,

Signature · 1 line
Sven Eckelmann <[email protected]>
#1 lore raw

PATCH 1/3 batctl: only mark file read successful on read line

The line_ptr is shared globally. It can happen that another function like parse_hosts_file() allocated the buffer successfully. But the next getline() in read_file() fails - but keeps the line_ptr valid. In this case, the function would return a success - even when the buffer contains stale data.

Instead only set the return value to EXIT_SUCCESS when a single line could be read.

Fixes deb324e65044 ("batctl: buffer based reading replaced by line-by-line reading")
---
 functions.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
functions.c +2 -3
index 00dbd3d..349569d 100644
@@ -147,6 +147,8 @@ int read_file(const char *full_path, int read_opt)
 	}
 
 	while (getline(&line_ptr, &len, fp) != -1) {
+		res = EXIT_SUCCESS;
+
 		/* the buffer will be handled elsewhere */
 		if (read_opt & USE_READ_BUFF)
 			break;
@@ -154,9 +156,6 @@ int read_file(const char *full_path, int read_opt)
 		printf("%s", line_ptr);
 	}
 
-	if (line_ptr)
-		res = EXIT_SUCCESS;
-
 	fclose(fp);
 	return res;
 }
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/3 batctl: version: avoid use of uninitialized read buffer

In reply to Sven Eckelmann

version() strips the trailing newline from line_ptr before checking whether read_file() actually succeeded. If the read_file() returned an error, it could be that line_ptr was allocated buyt not yet initialized. It could therefore not contain any \0 delimiter - making the strlen read outside the buffer. The write of the \0 could therefore also be outside the buffer.

Only attempt to access the buffer when a success was indicated.

Fixes dbc4a8c8e585 ("batctl: version also prints the kernel module version if available")
---
 main.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
main.c +5 -4
index 79ed4ef..e625291 100644
@@ -132,13 +132,14 @@ static void version(void)
 	printf("batctl %s [batman-adv: ", SOURCE_VERSION);
 
 	ret = read_file(module_ver_path, USE_READ_BUFF | SILENCE_ERRORS);
-	if ((line_ptr) && (line_ptr[strlen(line_ptr) - 1] == '\n'))
-		line_ptr[strlen(line_ptr) - 1] = '\0';
+	if (ret == EXIT_SUCCESS) {
+		if (line_ptr[strlen(line_ptr) - 1] == '\n')
+			line_ptr[strlen(line_ptr) - 1] = '\0';
 
-	if (ret == EXIT_SUCCESS)
 		printf("%s]\n", line_ptr);
-	else
+	} else {
 		printf("module version unknown]\n");
+	}
 
 	free(line_ptr);
 	exit(EXIT_SUCCESS);
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/3 batctl: version: don't strip newline for empty buffer

In reply to Sven Eckelmann

When read_file() would return an empty buffer, then version() must not strip the last byte. Otherwise it would try to access 1 byte before the start of the buffer.

Fixes dbc4a8c8e585 ("batctl: version also prints the kernel module version if available")
---
 main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
main.c +4 -2
index e625291..0cf99d7 100644
@@ -133,8 +133,10 @@ static void version(void)
 
 	ret = read_file(module_ver_path, USE_READ_BUFF | SILENCE_ERRORS);
 	if (ret == EXIT_SUCCESS) {
-		if (line_ptr[strlen(line_ptr) - 1] == '\n')
-			line_ptr[strlen(line_ptr) - 1] = '\0';
+		size_t line_len = strlen(line_ptr);
+
+		if (line_len > 0 && line_ptr[line_len - 1] == '\n')
+			line_ptr[line_len - 1] = '\0';
 
 		printf("%s]\n", line_ptr);
 	} else {
Signature · 1 line
2.47.3