summaryrefslogtreecommitdiff
path: root/util/libreboot-utils
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2026-03-27 03:09:41 +0000
committerLeah Rowe <leah@libreboot.org>2026-03-27 15:22:16 +0000
commit2c156d1aac37e10c41af6af5babe8810a4815897 (patch)
treeb1a594bc10411fcf66517e53b9056bd9f5ba5f35 /util/libreboot-utils
parent5d6344292a58aba54b321f6df2601630c26b62f9 (diff)
util/libreboot-utils: finish implementing hell
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/libreboot-utils')
-rw-r--r--util/libreboot-utils/include/common.h2
-rw-r--r--util/libreboot-utils/lib/file.c8
-rw-r--r--util/libreboot-utils/lib/io.c2
-rw-r--r--util/libreboot-utils/lib/mkhtemp.c4
-rw-r--r--util/libreboot-utils/lib/state.c6
-rw-r--r--util/libreboot-utils/lottery.c83
-rw-r--r--util/libreboot-utils/nvmutil.c2
7 files changed, 75 insertions, 32 deletions
diff --git a/util/libreboot-utils/include/common.h b/util/libreboot-utils/include/common.h
index 1bef0d50..77672846 100644
--- a/util/libreboot-utils/include/common.h
+++ b/util/libreboot-utils/include/common.h
@@ -543,7 +543,7 @@ int fs_rename_at(int olddirfd, const char *old,
int newdirfd, const char *new);
int fs_open(const char *path, int flags);
void close_no_err(int *fd);
-void free_if_null(char **buf);
+void free_and_set_null(char **buf);
int close_warn(int *fd, char *s);
struct filesystem *rootfs(void);
int fs_resolve_at(int dirfd, const char *path, int flags);
diff --git a/util/libreboot-utils/lib/file.c b/util/libreboot-utils/lib/file.c
index 5fdef7b3..3ca50889 100644
--- a/util/libreboot-utils/lib/file.c
+++ b/util/libreboot-utils/lib/file.c
@@ -153,7 +153,7 @@ fsync_dir(const char *path)
goto err_fsync_dir;
}
- free_if_null(&dirbuf);
+ free_and_set_null(&dirbuf);
errno = saved_errno;
return 0;
@@ -163,7 +163,7 @@ err_fsync_dir:
if (errno == saved_errno)
errno = EIO;
- free_if_null(&dirbuf);
+ free_and_set_null(&dirbuf);
close_no_err(&dirfd);
return -1;
@@ -581,7 +581,7 @@ try_err(int loop_err, int errval)
}
void
-free_if_null(char **buf)
+free_and_set_null(char **buf)
{
if (buf == NULL || *buf == NULL)
return;
@@ -902,7 +902,7 @@ fs_dirname_basename(const char *path,
*base = buf;
} else {
errno = EINVAL;
- free_if_null(&buf);
+ free_and_set_null(&buf);
return -1;
}
diff --git a/util/libreboot-utils/lib/io.c b/util/libreboot-utils/lib/io.c
index d05adbcc..1f2064a0 100644
--- a/util/libreboot-utils/lib/io.c
+++ b/util/libreboot-utils/lib/io.c
@@ -266,7 +266,7 @@ write_to_gbe_bin(void)
/* removed by rename
*/
- free_if_null(&f->tname);
+ free_and_set_null(&f->tname);
}
}
diff --git a/util/libreboot-utils/lib/mkhtemp.c b/util/libreboot-utils/lib/mkhtemp.c
index 0e0169e4..c913ce6c 100644
--- a/util/libreboot-utils/lib/mkhtemp.c
+++ b/util/libreboot-utils/lib/mkhtemp.c
@@ -184,7 +184,7 @@ err:
else
saved_errno = errno = EIO;
- free_if_null(&dest);
+ free_and_set_null(&dest);
close_no_err(&dirfd);
close_no_err(fd);
@@ -619,7 +619,7 @@ err:
close_no_err(fd);
success:
- free_if_null(&fname_copy);
+ free_and_set_null(&fname_copy);
return (*fd >= 0) ? *fd : -1;
}
diff --git a/util/libreboot-utils/lib/state.c b/util/libreboot-utils/lib/state.c
index 42d060b7..41c851fb 100644
--- a/util/libreboot-utils/lib/state.c
+++ b/util/libreboot-utils/lib/state.c
@@ -139,7 +139,7 @@ xstart(int argc, char *argv[])
if (us.f.tmpbase == NULL)
err_no_cleanup(0, errno, "strdup tmpbase");
- free_if_null(&tmpdir);
+ free_and_set_null(&tmpdir);
if (us.f.tname == NULL)
err_no_cleanup(0, errno, "x->f.tname null");
@@ -219,8 +219,8 @@ exit_cleanup(void)
close_err = 1;
close_no_err(&f->dirfd);
- free_if_null(&f->base);
- free_if_null(&f->tmpbase);
+ free_and_set_null(&f->base);
+ free_and_set_null(&f->tmpbase);
}
if (saved_errno)
diff --git a/util/libreboot-utils/lottery.c b/util/libreboot-utils/lottery.c
index 76548191..c66eb2b7 100644
--- a/util/libreboot-utils/lottery.c
+++ b/util/libreboot-utils/lottery.c
@@ -1,28 +1,71 @@
/* SPDX-License-Identifier: MIT ( >:3 )
- * Copyright (c) 2026 Leah Rowe <leah@libreboot.org> /| |\*/
-#include <stdio.h> /* / \ */
+ * Copyright (c) 2026 Leah Rowe <leah@libreboot.org> /| |\
+ / \ */
+#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> /* (^.>) - are u lucky? */
#include <string.h> /* \| /= */
#include "include/common.h" /* l \ */
+
#define len_hell(x, y) ((((x) - (y)) < BUFSIZ) ? ((x) - (y)) : BUFSIZ)
-static int rigged(char **s) {
- size_t size[3] = { rsize(SIZE_MAX), rsize(SIZE_MAX), 0 };
- if (!(size[0] && size[0]==size[1] && size[0]<=SIZE_MAX && s != NULL) ||
- ((size[0] & 1) && (*mkrstr(1) != *mkrstr(1))))
- return 1;
- if (!(size[0] &= ~(size_t)1)) /* (^:3) - it could be you! */
- return 0; /* /| |l */
- if ((*s = malloc(BUFSIZ)) == NULL) /* / \ */
- for (size[1] = 0; size[1] < size[0]; size[1] += BUFSIZ) {
- rset(*s, size[2] = len_hell(size[0], size[1]));
- if (!memcmp(*s, *s + (size[2] >> 1), size[2] >> 1))
- return 1;
- } else return 1;
- return 0; /* <---- winner! */
-} /* (^.^) - come again soon! */
-int main(int argc, char **argv) { /* /| |l */
- xpledgex("stdio", NULL); /* / \ */
- printf("%s", (argc = rigged(argv))? "You lose!\n" : "");
+
+static int rigged(void)
+{
+ size_t size[5] = { 0, 0, 0, SIZE_MAX, SIZE_MAX};
+ char *b1 = NULL, *b2 = NULL;
+ char *s = NULL;
+ size_t e = 0;
+
+ for (e = 0; e < SIZE_MAX; e++) {
+ size[3] = SIZE_MAX;
+ size[4] = SIZE_MAX;
+again:
+ if (!size[3]) {
+ if (!size[4])
+ if (e == SIZE_MAX - 1)
+ return 0; /* <--- WINNER!!! */
+ else
+ continue;
+ else --size[4];
+ } else --size[3];
+
+ free_and_set_null(&b1);
+ free_and_set_null(&b2);
+
+ size[0] = rsize(SIZE_MAX); /* \( ^o^)/ - then come play! */
+ size[1] = rsize(SIZE_MAX); /* | | */
+/* / \ */
+ if (!(size[0] && (size[0] == size[1]) && (size[0] <= SIZE_MAX)) ||
+ ((size[0] & 1) && (*(b1 = mkrstr(1)) != *(b2 = mkrstr(1)))))
+ goto out; /* \(^-^)/ - it could be you! */
+ if (!(size[0] &= ~(size_t)1)) /* \ / */
+ goto again; /* / \ */
+
+ if (s == NULL) {
+ if ((s = malloc(BUFSIZ)) == NULL)
+ goto out;
+ }
+
+ for (size[1] = 0; size[1] < size[0]; size[1] += BUFSIZ) {
+ rset(s, size[2] = len_hell(size[0], size[1]));
+ if (!memcmp(s, s + (size[2] >> 1), size[2] >> 1))
+ goto out;
+ }
+
+ goto again;
+ }
+out:
+ free_and_set_null(&b1);
+ free_and_set_null(&b2);
+ free_and_set_null(&s);
+
+ return 1;
+}
+/* \(^o^)/ - come again soon! */
+int main(int argc, char **argv) /* \ / */
+{ /* / \ */
+ xpledgex("stdio", NULL); /* */
+ printf("%s", (argc = rigged()) ? "You lose!\n" : "");
+
return argc;
}
diff --git a/util/libreboot-utils/nvmutil.c b/util/libreboot-utils/nvmutil.c
index 313390c0..0eed440c 100644
--- a/util/libreboot-utils/nvmutil.c
+++ b/util/libreboot-utils/nvmutil.c
@@ -132,7 +132,7 @@ main(int argc, char *argv[])
if (f->io_err_gbe_bin)
b0rk(EIO, "%s: error writing final file");
- free_if_null(&f->tname);
+ free_and_set_null(&f->tname);
return EXIT_SUCCESS;
}