diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-04-21 06:39:50 +0100 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-04-21 06:39:50 +0100 |
| commit | e097eb5483b00631ef49353a22a73c2c48939d95 (patch) | |
| tree | b0446d36f711dd1b439f67571b38b13e17663f01 | |
| parent | 7faf014a84e89f442f1ce35f5d0305074e77b85d (diff) | |
lbutils: don't use stack memory for path strings
Signed-off-by: Leah Rowe <leah@libreboot.org>
| -rw-r--r-- | util/libreboot-utils/include/common.h | 2 | ||||
| -rw-r--r-- | util/libreboot-utils/lib/file.c | 23 | ||||
| -rw-r--r-- | util/libreboot-utils/lib/mkhtemp.c | 6 | ||||
| -rw-r--r-- | util/libreboot-utils/lib/string.c | 21 |
4 files changed, 43 insertions, 9 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h index a16bdce0..940c4364 100644 --- a/util/libreboot-utils/include/common.h +++ b/util/libreboot-utils/include/common.h @@ -366,6 +366,8 @@ int scmp(const char *a, const char *b, size_t maxlen, int *rval); int ccmp(const char *a, const char *b, size_t i, int *rval); +int dup_pair(char **dir, const char *d, + char **base, const char *b); char *sdup(const char *s, size_t n, char **dest); char *scatn(ssize_t sc, const char **sv, diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c index efc23ba9..0385ebbb 100644 --- a/util/libreboot-utils/lib/file.c +++ b/util/libreboot-utils/lib/file.c @@ -521,6 +521,8 @@ fs_dirname_basename(const char *path, char *buf = NULL; char *slash; size_t len; + const char *d = NULL; + const char *b = NULL; errno = 0; if (if_err(path == NULL || dir == NULL || base == NULL, EFAULT)) @@ -539,22 +541,27 @@ fs_dirname_basename(const char *path, if (slash) { *slash = '\0'; - *dir = buf; - *base = slash + 1; + d = buf; + b = slash + 1; - if (**dir == '\0') { - (*dir)[0] = '/'; - (*dir)[1] = '\0'; - } + if (*d == '\0') + d = "/"; } else if (allow_relative) { - sdup(".", PATH_MAX, dir); - *base = buf; + d = "."; + b = buf; } else { free_and_set_null(&buf); goto err; } + if (dup_pair(dir, d, base, b) < 0) { + free_and_set_null(&buf); + goto err; + } + + free_and_set_null(&buf); + reset_caller_errno(0); return 0; err: diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c index da44a7e2..d394ae73 100644 --- a/util/libreboot-utils/lib/mkhtemp.c +++ b/util/libreboot-utils/lib/mkhtemp.c @@ -195,7 +195,11 @@ env_tmpdir(int bypass_all_sticky_checks, char **tmpdir, bypass_all_sticky_checks)) goto err; - rval = t; + rval = NULL; + if (t != NULL) { + if (sdup(t, PATH_MAX, &rval) == NULL) + goto err; + } goto out; } diff --git a/util/libreboot-utils/lib/string.c b/util/libreboot-utils/lib/string.c index 99864b82..7388cf35 100644 --- a/util/libreboot-utils/lib/string.c +++ b/util/libreboot-utils/lib/string.c @@ -270,6 +270,27 @@ out: return *rval; } +int +dup_pair(char **dir, const char *d, + char **base, const char *b) +{ + char *dtmp = NULL; + char *btmp = NULL; + + if (d && sdup(d, PATH_MAX, &dtmp) == NULL) + return -1; + + if (b && sdup(b, PATH_MAX, &btmp) == NULL) { + free(dtmp); + return -1; + } + + *dir = dtmp; + *base = btmp; + + return 0; +} + /* strict word-based strdup */ char * sdup(const char *s, |
