don't assume positive argc

handle a rare, but possible case of argc being 0, in which case argv[0]
would be null.

note that both POSIX and ISO C standard allow argc to be 0 and in
practice this can be triggered via calling `exec(3)` family of functions
with NULL as the first `argv`.
This commit is contained in:
NRK 2022-06-16 19:33:32 +06:00 committed by Gitea
parent ba39006574
commit fbe186e79d
2 changed files with 6 additions and 4 deletions

View file

@ -98,9 +98,6 @@ void parse_options(int argc, char **argv)
static opt_t _options; static opt_t _options;
options = &_options; options = &_options;
progname = strrchr(argv[0], '/');
progname = progname ? progname + 1 : argv[0];
_options.from_stdin = false; _options.from_stdin = false;
_options.to_stdout = false; _options.to_stdout = false;
_options.using_null = false; _options.using_null = false;
@ -125,6 +122,11 @@ void parse_options(int argc, char **argv)
_options.clean_cache = false; _options.clean_cache = false;
_options.private_mode = false; _options.private_mode = false;
if (argc > 0) {
s = strrchr(argv[0], '/');
progname = s != NULL && s[1] != '\0' ? s + 1 : argv[0];
}
optparse_init(&op, argv); optparse_init(&op, argv);
while ((opt = optparse_long(&op, longopts, NULL)) != -1) { while ((opt = optparse_long(&op, longopts, NULL)) != -1) {
for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */ for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */

2
util.c
View file

@ -28,7 +28,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
const char *progname; const char *progname = "nsxiv";
void* emalloc(size_t size) void* emalloc(size_t size)
{ {