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

Sven Eckelmann <[email protected]> Sun, 21 Jun 2026 16:24:10 +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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/main.c b/main.c
index 79ed4ef..94db4a1 100644
--- a/main.c
+++ b/main.c
@@ -132,7 +132,7 @@ 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'))
+	if (line_ptr && strlen(line_ptr) > 0 && line_ptr[strlen(line_ptr) - 1] == '\n')
 		line_ptr[strlen(line_ptr) - 1] = '\0';
 
 	if (ret == EXIT_SUCCESS)

-- 
2.47.3