summaryrefslogtreecommitdiff
path: root/util/libreboot-utils/lib/mkhtemp.c
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-30 05:13:31 +0100
committerLeah Rowe <leah@libreboot.org>2026-03-30 06:25:52 +0100
commitda20b75beac750bf936c9c959f18bf4dce4bdf11 (patch)
tree4a663207cdd150abca2e87c101fa58dd8b58272c /util/libreboot-utils/lib/mkhtemp.c
parentb96708bd3abc3cca7894b96a22caf6291b0748b0 (diff)
libreboot-utils: more flexible string usage
i previously used error status and set return values indirectly. i still do that, but where possible, i also now return the real value. this is because these string functions can no longer return with error status; on error, they all abort. this forces the program maintainer to keep their code reliable, and removes the need to check the error status after using syscalls, because these libc wrappers mitigate that and make use of libc for you, including errors. this is part of a general effort to promote safe use of the C programming language, especially in libreboot! Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils/lib/mkhtemp.c')
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c35
1 files changed, 13 insertions, 22 deletions
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index 2ef26d67..0560da47 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -120,8 +120,6 @@ new_tmp_common(int *fd, char **path, int type,
if (tmpdir == NULL)
goto err;
- if (slen(tmpdir, maxlen, &dirlen) < 0)
- goto err;
if (*tmpdir == '\0')
goto err;
if (*tmpdir != '/')
@@ -132,15 +130,12 @@ new_tmp_common(int *fd, char **path, int type,
else
templatestr = "tmp.XXXXXXXXXX";
- if (slen(templatestr, maxlen, &templatestr_len) < 0)
- goto err;
-
/* may as well calculate in advance */
- destlen = dirlen + 1 + templatestr_len;
+ destlen = slen(tmpdir, maxlen, &dirlen) + 1
+ + slen(templatestr, maxlen, &templatestr_len);
/* full path: */
- if (scatn(3, (const char *[]) { tmpdir, "/", templatestr },
- maxlen, &dest) < 0)
- goto err;
+ dest = scatn(3, (const char *[]) { tmpdir, "/", templatestr },
+ maxlen, &dest);
fname = dest + dirlen + 1;
@@ -312,12 +307,10 @@ same_dir(const char *a, const char *b)
/* optimisation: if both dirs
are the same, we don't need
- to check anything. sehr schnell:
+ to check anything. sehr schnell!
*/
- if (scmp(a, b, maxlen, &rval_scmp) < 0)
- goto err_same_dir;
/* bonus: scmp checks null for us */
- if (rval_scmp == 0)
+ if (!scmp(a, b, maxlen, &rval_scmp))
goto success_same_dir;
fd_a = fs_open(a, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
@@ -550,19 +543,17 @@ mkhtemp(int *fd,
if (if_err(fd == NULL || template == NULL || fname == NULL ||
st_dir_initial == NULL, EFAULT) ||
if_err(*fd >= 0, EEXIST) ||
- if_err(dirfd < 0, EBADF)
- ||
- if_err_sys(slen(template, max_len, &template_len) < 0) ||
- if_err(template_len >= max_len, EMSGSIZE)
- ||
- if_err_sys(slen(fname, max_len, &fname_len) < 0) ||
- if_err(fname == NULL, EINVAL) ||
- if_err(strrchr(fname, '/') != NULL, EINVAL))
+ if_err(dirfd < 0, EBADF))
return -1;
- for (end = template + template_len; /* count X */
+ /* count X */
+ for (end = template + slen(template, max_len, &template_len);
end > template && *--end == 'X'; xc++);
+ fname_len = slen(fname, max_len, &fname_len);
+ if (if_err(strrchr(fname, '/') != NULL, EINVAL))
+ return -1;
+
if (if_err(xc < 3 || xc > template_len, EINVAL) ||
if_err(fname_len > template_len, EOVERFLOW))
return -1;