eBPF-based security platform
util: add strsprintf()
Daniel Borkmann60b9758af67e
diff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 8aa2665c..10d7ca4f 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -2,7 +2,7 @@/** Copyright (c) 2026 Authors of shirudo*- * Bounded number parsing and string copies, from util/string.c.+ * Bounded number parsing, string copies and formatting, from util/string.c.*/#ifndef SHIRUDO_STRING_H@@ -35,4 +35,11 @@ int parse_s64(const char *s, size_t len, __s64 min, __s64 max, __s64 *out);*/ssize_t strscpy(char *dst, const char *src, size_t size);+/*+ * snprintf() into the size bytes at buf: the length written, or -E2BIG,+ * with buf left empty, when the output did not fit.+ */+int strsprintf(char *buf, size_t size, const char *fmt, ...)+ __attribute__((format(printf, 3, 4), warn_unused_result));+#endif /* SHIRUDO_STRING_H */diff --git a/testing/helper/test-string.c b/testing/helper/test-string.cindex 073ffd0f..a4a8f73b 100644--- a/testing/helper/test-string.c+++ b/testing/helper/test-string.c@@ -9,6 +9,7 @@* test-string u64|x64 <max> <text> [<len>]* test-string s64 <min> <max> <text> [<len>]* test-string strscpy <size> <text>+ * test-string strsprintf <size> <text>*/#include <stdint.h>@@ -94,6 +95,21 @@ int main(int argc, char **argv)xfree(buf, max ? max : 1);return 0;}- fprintf(stderr, "usage: test-string digits|u64|x64|s64|strscpy ...\n");+ if (!strcmp(op, "strsprintf") && argc == 4) {+ int n;++ if (bound_u64(argv[2], &max))+ return 2;+ buf = xmalloc(max ? max : 1);+ if (!buf)+ return 2;+ buf[0] = 'x';+ n = strsprintf(buf, max, "<%s>", argv[3]);+ printf("rc=%d buf=%s\n", n, max ? buf : "-");+ xfree(buf, max ? max : 1);+ return 0;+ }+ fprintf(stderr,+ "usage: test-string digits|u64|x64|s64|strscpy|strsprintf ...\n");return 2;}diff --git a/testing/t0049-string.sh b/testing/t0049-string.shindex 733fb0a5..ae147088 100755--- a/testing/t0049-string.sh+++ b/testing/t0049-string.sh@@ -3,8 +3,9 @@# Copyright (c) 2026 Authors of shirudo## t0049 -- util/string.c: numbers parsed from a bounded, unterminated-# span, digits only, within the bounds the caller gives, and strings-# copied into a buffer that is always terminated and says when it cut.+# span, digits only, within the bounds the caller gives, strings copied+# into a buffer that is always terminated and says when it cut, and+# output formatted whole or not at all.cd "$(dirname "$0")". ./test-lib.sh@@ -92,4 +93,15 @@ test_expect_success 'a string that does not fit is cut and says so' './$H strscpy 0 hello | grep -qx "rc=-7 dst=-"'+test_expect_success 'formatted output that fits is written whole' '+ ./$H strsprintf 8 hello | grep -qx "rc=7 buf=<hello>" &&+ ./$H strsprintf 64 "" | grep -qx "rc=2 buf=<>"+'++test_expect_success 'formatted output that does not fit leaves nothing' '+ ./$H strsprintf 7 hello | grep -qx "rc=-7 buf=" &&+ ./$H strsprintf 1 hello | grep -qx "rc=-7 buf=" &&+ ./$H strsprintf 0 hello | grep -qx "rc=-7 buf=-"+'+test_donediff --git a/util/string.c b/util/string.cindex 80d1a62b..a56c15fc 100644--- a/util/string.c+++ b/util/string.c@@ -2,12 +2,14 @@/** Copyright (c) 2026 Authors of shirudo*- * Bounded number parsing and string copies.+ * Bounded number parsing, string copies and formatting.*/#include <errno.h>+#include <stdarg.h>#include <stdbool.h>#include <stdint.h>+#include <stdio.h>#include <string.h>#include <shirudo/string.h>@@ -105,3 +107,18 @@ ssize_t strscpy(char *dst, const char *src, size_t size)memcpy(dst, src, len + 1);return (ssize_t)len;}++int strsprintf(char *buf, size_t size, const char *fmt, ...)+{+ va_list ap;+ int n;++ va_start(ap, fmt);+ n = vsnprintf(buf, size, fmt, ap);+ va_end(ap);+ if (n >= 0 && (size_t)n < size)+ return n;+ if (size)+ buf[0] = '\0';+ return -E2BIG;+}