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

Sven Eckelmann <[email protected]> Sun, 05 Jul 2026 14:55:08 +0200
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
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 <[email protected]>
---
 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