nsxivfbe186e
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`.
+6 −4
2 files changed
| @@ -98,9 +98,6 @@ void parse_options(int argc, char **argv) | |||
| 98 | 98 | static opt_t _options; | |
| 99 | 99 | ||
| 100 | 100 | options = &_options; | |
| 101 | − | progname = strrchr(argv[0], '/'); | |
| 102 | − | progname = progname ? progname + 1 : argv[0]; | |
| 103 | − | ||
| 104 | 101 | _options.from_stdin = false; | |
| 105 | 102 | _options.to_stdout = false; | |
| 106 | 103 | _options.using_null = false; | |
| @@ -125,6 +122,11 @@ void parse_options(int argc, char **argv) | |||
| 125 | 122 | _options.clean_cache = false; | |
| 126 | 123 | _options.private_mode = false; | |
| 127 | 124 | ||
| 125 | + | if (argc > 0) { | |
| 126 | + | s = strrchr(argv[0], '/'); | |
| 127 | + | progname = s != NULL && s[1] != '\0' ? s + 1 : argv[0]; | |
| 128 | + | } | |
| 129 | + | ||
| 128 | 130 | optparse_init(&op, argv); | |
| 129 | 131 | while ((opt = optparse_long(&op, longopts, NULL)) != -1) { | |
| 130 | 132 | for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */ | |
| @@ -28,7 +28,7 @@ | |||
| 28 | 28 | #include <sys/stat.h> | |
| 29 | 29 | #include <unistd.h> | |
| 30 | 30 | ||
| 31 | − | const char *progname; | |
| 31 | + | const char *progname = "nsxiv"; | |
| 32 | 32 | ||
| 33 | 33 | void* emalloc(size_t size) | |
| 34 | 34 | { | |