[PATCH] bzip2recover: Check argc >= 1 && argv[0] != NULL

Naveed Khan naveed@digiscrypt.com
Sun Jun 21 21:09:41 GMT 2026


main() initialises progName by copying argv[0] with strncpy before argc
is examined. When the program is started with an empty argument vector
(argc == 0 and argv[0] == NULL), for example through
execve(path, (char*[]){ NULL }, envp), this dereferences a NULL pointer
and bzip2recover crashes with SIGSEGV before any argument checking
happens.

Guard the use of argv[0] and fall back to a hard coded "bzip2recover"
string, mirroring the fix already applied to bzip2.c in commit
af79253677ad ("bzip2.c: Check argc >= 1 && argv[0] != NULL").
---
diff --git a/bzip2recover.c b/bzip2recover.c
index 28d4a05..8c79a90 100644
--- a/bzip2recover.c
+++ b/bzip2recover.c
@@ -311,7 +311,10 @@ Int32 main ( Int32 argc, Char** argv )
    UInt32      buffHi, buffLo, blockCRC;
    Char*       p;
 
-   strncpy ( progName, argv[0], BZ_MAX_FILENAME-1);
+   if (argc >= 1 && argv[0] != NULL)
+      strncpy ( progName, argv[0], BZ_MAX_FILENAME-1);
+   else
+      strncpy ( progName, "bzip2recover", BZ_MAX_FILENAME-1);
    progName[BZ_MAX_FILENAME-1]='\0';
    inFileName[0] = outFileName[0] = 0;
 
-- 
2.52.0


More information about the Bzip2-devel mailing list