* [PATCH 0/3] batctl: version bugfixes
@ 2026-07-05 12:55 Sven Eckelmann
2026-07-05 12:55 ` [PATCH 1/3] batctl: only mark file read successful on read line Sven Eckelmann
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Sven Eckelmann @ 2026-07-05 12:55 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 (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,
--
Sven Eckelmann <sven@narfation.org>
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 1/3] batctl: only mark file read successful on read line
2026-07-05 12:55 [PATCH 0/3] batctl: version bugfixes Sven Eckelmann
@ 2026-07-05 12:55 ` Sven Eckelmann
2026-07-05 12:55 ` [PATCH 2/3] batctl: version: avoid use of uninitialized read buffer Sven Eckelmann
2026-07-05 12:55 ` [PATCH 3/3] batctl: version: don't strip newline for empty buffer Sven Eckelmann
2 siblings, 0 replies; 4+ messages in thread
From: Sven Eckelmann @ 2026-07-05 12:55 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: Sven Eckelmann
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")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
functions.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/functions.c b/functions.c
index 00dbd3d..349569d 100644
--- a/functions.c
+++ b/functions.c
@@ -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;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/3] batctl: version: avoid use of uninitialized read buffer
2026-07-05 12:55 [PATCH 0/3] batctl: version bugfixes Sven Eckelmann
2026-07-05 12:55 ` [PATCH 1/3] batctl: only mark file read successful on read line Sven Eckelmann
@ 2026-07-05 12:55 ` Sven Eckelmann
2026-07-05 12:55 ` [PATCH 3/3] batctl: version: don't strip newline for empty buffer Sven Eckelmann
2 siblings, 0 replies; 4+ messages in thread
From: Sven Eckelmann @ 2026-07-05 12:55 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: 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")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
main.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/main.c b/main.c
index 79ed4ef..e625291 100644
--- a/main.c
+++ b/main.c
@@ -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);
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 3/3] batctl: version: don't strip newline for empty buffer
2026-07-05 12:55 [PATCH 0/3] batctl: version bugfixes Sven Eckelmann
2026-07-05 12:55 ` [PATCH 1/3] batctl: only mark file read successful on read line Sven Eckelmann
2026-07-05 12:55 ` [PATCH 2/3] batctl: version: avoid use of uninitialized read buffer Sven Eckelmann
@ 2026-07-05 12:55 ` Sven Eckelmann
2 siblings, 0 replies; 4+ messages in thread
From: Sven Eckelmann @ 2026-07-05 12:55 UTC (permalink / raw)
To: b.a.t.m.a.n; +Cc: 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")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
main.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/main.c b/main.c
index e625291..0cf99d7 100644
--- a/main.c
+++ b/main.c
@@ -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 {
--
2.47.3
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-05 12:58 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-05 12:55 [PATCH 0/3] batctl: version bugfixes Sven Eckelmann
2026-07-05 12:55 ` [PATCH 1/3] batctl: only mark file read successful on read line Sven Eckelmann
2026-07-05 12:55 ` [PATCH 2/3] batctl: version: avoid use of uninitialized read buffer Sven Eckelmann
2026-07-05 12:55 ` [PATCH 3/3] batctl: version: don't strip newline for empty buffer Sven Eckelmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox