eBPF-based security platform
include: add min() and max() to <shirudo/misc.h>
Daniel Borkmann653219ef8c05
diff --git a/builtin-status.c b/builtin-status.cindex 4c54e1ef..66ff9a00 100644--- a/builtin-status.c+++ b/builtin-status.c@@ -1590,7 +1590,7 @@ int cmd_status(int argc, const char **argv)struct timespec rt = {}, bt = {};__s64 boot_sec = 0;size_t last = 10;- size_t shown = ks.n < last ? ks.n : last;+ size_t shown = min(ks.n, last);if (clock_gettime(CLOCK_REALTIME, &rt) == 0 &&clock_gettime(CLOCK_BOOTTIME, &bt) == 0)diff --git a/include/shirudo/misc.h b/include/shirudo/misc.hindex da58fe20..1fd30d76 100644--- a/include/shirudo/misc.h+++ b/include/shirudo/misc.h@@ -2,8 +2,8 @@/** Copyright (c) 2026 Authors of shirudo*- * The kernel's fixed-width integer types, ARRAY_SIZE(), ptr_to_u64() and- * shirudo_trunc(), for userspace.+ * The kernel's fixed-width integer types, ARRAY_SIZE(), min() and max(),+ * ptr_to_u64() and shirudo_trunc(), for userspace.*/#ifndef SHIRUDO_MISC_H@@ -34,6 +34,25 @@ typedef unsigned long long __u64;typeof(&(a)[0])))#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + __must_be_array(a))+/*+ * Each argument is evaluated once, and arguments of two types refuse to+ * compile rather than compare after a silent conversion: min_t() and+ * max_t() convert both to the type they name. The temporaries take a+ * __COUNTER__ suffix so that a nested call shadows nothing.+ */+#define __minmax_op_min <+#define __minmax_op_max >+#define __minmax_n(op, x, y, n) ({ \+ __auto_type __x##n = (x); \+ __auto_type __y##n = (y); \+ (void)(&__x##n == &__y##n); \+ __x##n __minmax_op_##op __y##n ? __x##n : __y##n; })+#define __minmax(op, x, y, n) __minmax_n(op, x, y, n)+#define min(x, y) __minmax(min, x, y, __COUNTER__)+#define max(x, y) __minmax(max, x, y, __COUNTER__)+#define min_t(type, x, y) min((type)(x), (type)(y))+#define max_t(type, x, y) max((type)(x), (type)(y))+/* A pointer as the __u64 the bpf() ABI carries it in. */static inline __u64 ptr_to_u64(const void *ptr){diff --git a/testing/fuzz/fuzz-serializer.c b/testing/fuzz/fuzz-serializer.cindex 0b8acb03..8ec350e7 100644--- a/testing/fuzz/fuzz-serializer.c+++ b/testing/fuzz/fuzz-serializer.c@@ -44,7 +44,7 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)chunks = data;chunks_len = size;/* The page the reader asks for is the one a consistent serializer wrote. */- memcpy(&first, data, size < sizeof(first) ? size : sizeof(first));+ memcpy(&first, data, min(size, sizeof(first)));if (!read_serial_page(0, &info, first.page_index, first.cursor,first.invocation_id, &page))xfree(page.data, page.bytes);diff --git a/util/bpm_package.c b/util/bpm_package.cindex 8261d83d..67724aed 100644--- a/util/bpm_package.c+++ b/util/bpm_package.c@@ -139,7 +139,7 @@ static int prerelease_compare(const char *a, const char *b)} else if (na != nb) {c = na ? -1 : 1;} else {- c = strncmp(a, b, la < lb ? la : lb);+ c = strncmp(a, b, min(la, lb));if (!c && la != lb)c = la > lb ? 1 : -1;}diff --git a/util/bpm_tree.c b/util/bpm_tree.cindex 73c90b68..049a24c3 100644--- a/util/bpm_tree.c+++ b/util/bpm_tree.c@@ -55,7 +55,7 @@ static int tp_string(struct tree_parser *p, char *out, size_t cap)if (c == '"') {if (cap)- out[n < cap ? n : cap - 1] = '\0';+ out[min(n, cap - 1)] = '\0';return n < cap ? 0 : -ENOSPC;}if (c == '\\') {diff --git a/util/kmodules.c b/util/kmodules.cindex 8df7ecab..faad40a2 100644--- a/util/kmodules.c+++ b/util/kmodules.c@@ -453,7 +453,7 @@ void kmodules_print_recent(const struct shirudo_kmod_set *set,all[i].idx = i;}qsort(all, set->n, sizeof(*all), recent_cmp);- take = set->n < last_n ? set->n : last_n;+ take = min(set->n, last_n);for (i = 0; i < take; i++) {const struct shirudo_kmod *m = &set->mods[all[i].idx];char ts[32], size_s[16], age[24];diff --git a/util/malloc.c b/util/malloc.cindex 8614fd5e..1b83019c 100644--- a/util/malloc.c+++ b/util/malloc.c@@ -11,6 +11,8 @@#include <stdlib.h>#include <string.h>+#include <shirudo/misc.h>+/* Implements the wrappers; opt out of the poison pragma in malloc.h. */#define MALLOC_INTERNAL#include "malloc.h"@@ -52,7 +54,7 @@ void *xreallocarray(void *ptr, size_t old_nmemb, size_t new_nmemb,if (!out)return NULL; /* failure -- leave ptr untouched */if (ptr) {- copy = old_size < new_size ? old_size : new_size;+ copy = min(old_size, new_size);memcpy(out, ptr, copy);xfree(ptr, old_size); /* scrub old block before release */}diff --git a/util/similar.c b/util/similar.cindex 82fd5394..1766e331 100644--- a/util/similar.c+++ b/util/similar.c@@ -7,6 +7,8 @@#include <string.h>+#include <shirudo/misc.h>+#include "similar.h"static int edit_distance(const char *s, const char *t, int max)@@ -86,7 +88,7 @@ size_t suggest(const char *name,* right; discard the one that falls off the end so `out`* always holds the cap-best matches.*/- pos = n_out < cap ? n_out : cap;+ pos = min(n_out, cap);while (pos > 0 && out[pos - 1].dist > d) {if (pos < cap)out[pos] = out[pos - 1];diff --git a/util/upatch_decode.c b/util/upatch_decode.cindex a61d3115..1738a9f9 100644--- a/util/upatch_decode.c+++ b/util/upatch_decode.c@@ -153,7 +153,7 @@ static void *lua_limited_alloc(void *ud, void *ptr, size_t old_size,if (!next)return NULL;if (ptr) {- memcpy(next, ptr, old_size < new_size ? old_size : new_size);+ memcpy(next, ptr, min(old_size, new_size));xfree(ptr, old_size);}memory->used = base + new_size;diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex adbe9d27..63a7c550 100644--- a/util/upatch_rodata.c+++ b/util/upatch_rodata.c@@ -271,7 +271,7 @@ static int btf_rodata_btf(const struct shirudo_upatch *u,if (type_end < hdr.type_off || str_end < hdr.str_off ||layout_end < hdr.layout_off)continue;- end = hdr.hdr_len + (type_end > str_end ? type_end : str_end);+ end = hdr.hdr_len + max(type_end, str_end);if (layout_end > end - hdr.hdr_len)end = hdr.hdr_len + layout_end;if (end < hdr.hdr_len || end > u->hdr.data_len - i)