eBPF-based security platform
util: add strscpy()
Daniel Borkmann1839c18d96d5
diff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 3b9df441..8aa2665c 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -2,13 +2,14 @@/** Copyright (c) 2026 Authors of shirudo*- * Bounded number parsing, from util/string.c.+ * Bounded number parsing and string copies, from util/string.c.*/#ifndef SHIRUDO_STRING_H#define SHIRUDO_STRING_H#include <stddef.h>+#include <sys/types.h>#include <shirudo/misc.h>@@ -27,4 +28,11 @@ int parse_u64(const char *s, size_t len, __u64 max, __u64 *out);int parse_x64(const char *s, size_t len, __u64 max, __u64 *out);int parse_s64(const char *s, size_t len, __s64 min, __s64 max, __s64 *out);+/*+ * Copy the string src into the size bytes at dst, terminated whenever+ * size is not 0: its length, or -E2BIG when it did not fit and dst holds+ * as much of it as did.+ */+ssize_t strscpy(char *dst, const char *src, size_t size);+#endif /* SHIRUDO_STRING_H */diff --git a/testing/helper/test-string.c b/testing/helper/test-string.cindex 787546a1..073ffd0f 100644--- a/testing/helper/test-string.c+++ b/testing/helper/test-string.c@@ -8,6 +8,7 @@* test-string digits <text>* test-string u64|x64 <max> <text> [<len>]* test-string s64 <min> <max> <text> [<len>]+ * test-string strscpy <size> <text>*/#include <stdint.h>@@ -80,6 +81,19 @@ int main(int argc, char **argv)xfree(buf, len ? len : 1);return 0;}- fprintf(stderr, "usage: test-string digits|u64|x64|s64 ...\n");+ if (!strcmp(op, "strscpy") && argc == 4) {+ ssize_t n;++ if (bound_u64(argv[2], &max))+ return 2;+ buf = xmalloc(max ? max : 1);+ if (!buf)+ return 2;+ n = strscpy(buf, argv[3], max);+ printf("rc=%zd dst=%s\n", n, max ? buf : "-");+ xfree(buf, max ? max : 1);+ return 0;+ }+ fprintf(stderr, "usage: test-string digits|u64|x64|s64|strscpy ...\n");return 2;}diff --git a/testing/t0049-string.sh b/testing/t0049-string.shindex f6593018..733fb0a5 100755--- a/testing/t0049-string.sh+++ b/testing/t0049-string.sh@@ -3,7 +3,8 @@# 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.+# span, digits only, within the bounds the caller gives, and strings+# copied into a buffer that is always terminated and says when it cut.cd "$(dirname "$0")". ./test-lib.sh@@ -13,7 +14,7 @@ U64_MAX=18446744073709551615S64_MIN=-9223372036854775808S64_MAX=9223372036854775807-# EINVAL=22, ERANGE=34 on Linux.+# E2BIG=7, EINVAL=22, ERANGE=34 on Linux.test_expect_success 'leading digits are counted' './$H digits 123abc | grep -qx "n=3" &&@@ -79,4 +80,16 @@ test_expect_success 'a signed number keeps to its bounds and form' 'done'+test_expect_success 'a string that fits is copied whole' '+ ./$H strscpy 6 hello | grep -qx "rc=5 dst=hello" &&+ ./$H strscpy 64 hello | grep -qx "rc=5 dst=hello" &&+ ./$H strscpy 1 "" | grep -qx "rc=0 dst="+'++test_expect_success 'a string that does not fit is cut and says so' '+ ./$H strscpy 5 hello | grep -qx "rc=-7 dst=hell" &&+ ./$H strscpy 1 hello | grep -qx "rc=-7 dst=" &&+ ./$H strscpy 0 hello | grep -qx "rc=-7 dst=-"+'+test_donediff --git a/util/string.c b/util/string.cindex 664861f4..80d1a62b 100644--- a/util/string.c+++ b/util/string.c@@ -2,12 +2,13 @@/** Copyright (c) 2026 Authors of shirudo*- * Bounded number parsing.+ * Bounded number parsing and string copies.*/#include <errno.h>#include <stdbool.h>#include <stdint.h>+#include <string.h>#include <shirudo/string.h>@@ -88,3 +89,19 @@ int parse_s64(const char *s, size_t len, __s64 min, __s64 max, __s64 *out)*out = r;return 0;}++ssize_t strscpy(char *dst, const char *src, size_t size)+{+ size_t len;++ if (!size)+ return -E2BIG;+ len = strnlen(src, size);+ if (len == size) {+ memcpy(dst, src, size - 1);+ dst[size - 1] = '\0';+ return -E2BIG;+ }+ memcpy(dst, src, len + 1);+ return (ssize_t)len;+}