Skip to content

eBPF-based security platform

docs: say what xstrndup() copies, and send bytes with a length elsewhere

Daniel Borkmann1256bc61387b
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.md
index 744c2a3d..bb926f7f 100644
--- a/.agents/skills/coding/SKILL.md
+++ b/.agents/skills/coding/SKILL.md
@@ -453,9 +453,12 @@ without a body.
a helper formatting display text returns the error, and status shows
`"?"`. Constant text is copied, not formatted, and a literal copied
into an array takes `strscpy_literal()`, which cannot fail.
-- `xstrdup()` measures with `strlen()`, so outside input gets
- `xstrndup(s, bound)`. `xfree(p, size)` and `xstrfree()` scrub before
- they free.
+- `xstrdup()` copies a string and `xstrndup(s, n)` at most its first
+ `n` bytes: a prefix, or a bounded copy of what already is a string.
+ Both end the copy at a NUL and say nothing, which is wrong for bytes
+ that come with a length: those become a string through
+ `span_to_str()`, into `len + 1` bytes from `xmalloc()`, which refuses
+ the NUL. `xfree(p, size)` and `xstrfree()` scrub before they free.
- `min_t()` and `max_t()` cast and can cut a value, so they are for
only what `min()` and `max()` refuse; a constant compared with an
unsigned value takes a `U` suffix.
diff --git a/util/malloc.h b/util/malloc.h
index 85e60e3e..e9c44ce8 100644
--- a/util/malloc.h
+++ b/util/malloc.h
@@ -34,9 +34,9 @@ void *xreallocarray(void *ptr, size_t old_nmemb, size_t new_nmemb,
size_t size);
/*
- * xstrdup() uses strlen, so it takes a trusted NUL-terminated string
- * alone; untrusted or unbounded input goes through xstrndup() with a
- * bound.
+ * xstrdup() copies a string, xstrndup() at most n bytes of one. A NUL
+ * ends either copy without an error, so bytes that come with a length
+ * become a string through span_to_str() instead.
*/
char *xstrdup(const char *s);
char *xstrndup(const char *s, size_t n);