[PATCH] bzip2recover: Check argc >= 1 && argv[0] != NULL
Mark Wielaard
mark@klomp.org
Mon Jun 22 09:53:26 GMT 2026
Hi Naveed,
On Mon, Jun 22, 2026 at 02:39:41AM +0530, Naveed Khan via Bzip2-devel wrote:
> 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").
Yeah, as mentioned in
https://sourceware.org/bugzilla/show_bug.cgi?id=33046#c5 it is a
somewhat obscure way of invoking a program. But we can easily work
around it, as you done here.
Pushed.
Thanks,
Mark
> ---
> 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