diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-29 08:09:52 +0100 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-29 08:20:09 +0100 |
| commit | 6e4839d3566bbabd30d0d15b4edb820d4d4b042f (patch) | |
| tree | 8f277c6cf03cbc27ce04d71120783c8b601d8a65 /util/libreboot-utils/lib | |
| parent | 909b321f3a834dbcccf884bc8206a3fe7f1af149 (diff) | |
libreboot-utils: simplify lbgetprogname
make it more reliable; it can't segfault
now, under any circumstance. not even
once.
the problem arised when lbsetname was not
called in a program, before calling the
function: lbgetprogname. a segfault would
occur, due to it being NULL.
not every os/libc has getprogname, so i have
my own implementation.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib')
| -rw-r--r-- | util/libreboot-utils/lib/string.c | 40 |
1 files changed, 15 insertions, 25 deletions
diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c index d5e2de85..6a372dcf 100644 --- a/util/libreboot-utils/lib/string.c +++ b/util/libreboot-utils/lib/string.c @@ -359,22 +359,16 @@ no_op(void) const char * lbgetprogname(void) { - static char *rval = NULL; - static char *p; - static int setname = 0; - - if (!setname) { - if ((rval = lbsetprogname(NULL)) == NULL) - return NULL; - - p = strrchr(rval, '/'); - if (p) - rval = p + 1; - - setname = 1; - } - - return rval; + char *name = lbsetprogname(NULL); + char *p = NULL; + if (name) + p = strrchr(name, '/'); + if (p) + return p + 1; + else if (name) + return name; + else + return "libreboot-utils"; } /* singleton. if string not null, @@ -385,17 +379,13 @@ lbgetprogname(void) char * lbsetprogname(char *argv0) { - static int setname = 0; static char *progname = NULL; - size_t len; - - if (!setname) { - if (if_err(argv0 == NULL || *argv0 == '\0', EFAULT) || - slen(argv0, 4096, &len) < 0) - return NULL; + static int set = 0; - memcpy(smalloc(&progname, len + 1), argv0, len + 1); - setname = 1; + if (!set) { + if (argv0 == NULL || sdup(argv0, 4096, &progname) < 0) + return "libreboot-utils"; + set = 1; } return progname; |
