Krolyxon
Commit fbe186e

Branches

Refs →

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`.
Author
NRK
Date
2022-06-16 19:33 +0600
Committer
Gitea · 2022-09-05 03:52 +0200
Parent
ba39006
Commit
fbe186e79d2083a7bbd1588b317cd5782bc5ae73

+6 −4

2 files changed

  1. M options.c +5 −3
  2. M util.c +1 −1
M options.c View
@@ -98,9 +98,6 @@ void parse_options(int argc, char **argv)
9898 static opt_t _options;
9999
100100 options = &_options;
101− progname = strrchr(argv[0], '/');
102− progname = progname ? progname + 1 : argv[0];
103−
104101 _options.from_stdin = false;
105102 _options.to_stdout = false;
106103 _options.using_null = false;
@@ -125,6 +122,11 @@ void parse_options(int argc, char **argv)
125122 _options.clean_cache = false;
126123 _options.private_mode = false;
127124
125+ if (argc > 0) {
126+ s = strrchr(argv[0], '/');
127+ progname = s != NULL && s[1] != '\0' ? s + 1 : argv[0];
128+ }
129+
128130 optparse_init(&op, argv);
129131 while ((opt = optparse_long(&op, longopts, NULL)) != -1) {
130132 for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */
M util.c View
@@ -28,7 +28,7 @@
2828 #include <sys/stat.h>
2929 #include <unistd.h>
3030
31−const char *progname;
31+const char *progname = "nsxiv";
3232
3333 void* emalloc(size_t size)
3434 {