eBPF-based security platform
string: walk the tokens of a buffer without writing to it
Daniel Borkmann0a4736f59010
diff --git a/.agents/skills/coding/SKILL.md b/.agents/skills/coding/SKILL.mdindex a132ba1f..f4fa95a0 100644--- a/.agents/skills/coding/SKILL.md+++ b/.agents/skills/coding/SKILL.md@@ -393,6 +393,7 @@ already, or should. The basics:| Copy or format a string | `strscpy()`, `strsprintf()`, `vstrsprintf()`, and check `-E2BIG` || A upatch's pin directory or a pin in it | `upatch_pin_path()` (`util/paths.h`) || A string's prefix, or a span that must be one string | `str_has_prefix()` to test a literal prefix and `str_skip_prefix()` to step past it, `str_after_prefix()` for the rest after a prefix known only at run time, each with a `_nocase` form for a letter in either case; `span_equals()` for a span of known length against a literal, `span_equals_str()` against a string, `span_has_prefix_nocase()` for a span's start (`<shirudo/string.h>`); `strncmp()` and `strncasecmp()`, whose length a caller counts by hand, are poisoned |+| The lines of a buffer, the words of a line, the entries of a list | `for_each_token()` (`<shirudo/string.h>`), which gives each token as pointer and length and writes nothing; a caller that needs a terminated token ends it in a buffer of its own || A file name's extension | `name_has_suffix()` (`<shirudo/string.h>`), `upatch_select()` for a scandir of upatches || Text from outside on a terminal | `text_sanitize()`; UTF-8 with `utf8_width()` || Bytes from outside as a string | `span_to_str()` (`<shirudo/string.h>`), which takes their length and refuses a NUL among them; `read_text_line()` for a line of a settings file (`util/io.h`) |diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex c54e9dcc..08b514eb 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -82,6 +82,13 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.in either case for the ``_nocase`` forms, and the empty prefixstarts every string; a span equals a string of its length andbytes alone; nothing past a span's length is read+ * - ``string-tokens``+ - ``token_next()``, ``for_each_token()``+ - Lengths 0 to 4, one or two delimiters, every cursor+ - A token lies inside the span, holds no delimiter and has one or+ the span's end on either side; the cursor ends past it and past+ one delimiter; a walk gives the bytes that are no delimiter in+ order, each once, and no empty token* - ``hex``- ``hex_encode()``, ``hex_alloc()``, ``hex_decode()``,``hex_decode_max()``diff --git a/include/shirudo/string.h b/include/shirudo/string.hindex 17bc9638..7e648d0c 100644--- a/include/shirudo/string.h+++ b/include/shirudo/string.h@@ -3,9 +3,9 @@* Copyright (c) 2026 Authors of shirudo** Bounded number parsing, string copies and formatting, file name- * suffixes, DNS names, UTF-8 characters and text for a terminal, from- * util/string.c, the compares of a prefix and of a span, and the scrub- * and compare for secrets.+ * suffixes, tokens, DNS names, UTF-8 characters and text for a terminal,+ * from util/string.c, the compares of a prefix and of a span, and the+ * scrub and compare for secrets.*/#ifndef SHIRUDO_STRING_H@@ -147,6 +147,33 @@ static inline bool span_equals_str(const char *s, size_t len, const char *str)!strncasecmp(s, "" prefix, sizeof(prefix) - 1)) ? \sizeof(prefix) - 1 : 0)+/*+ * The next token of the len bytes at s, from *pos on: a run of bytes that+ * are none of delims, after any that are. Its length, with its offset in+ * *start and *pos past it and past the delimiter that ended it, or 0 when+ * no token is left. Nothing is written, and a NUL is a byte as any other.+ */+size_t token_next(const char *s, size_t len, const char *delims, size_t *pos,+ size_t *start);++/*+ * Run the body once for each token of the len bytes at s, tok at its+ * first byte, a pointer of the type of s, and tok_len its length. No+ * token is empty: delimiters in a row count as one. A body that owns the+ * buffer may end the token with a NUL where a byte follows it, the+ * delimiter or a string's terminator.+ */+#define __for_each_token(tok, tok_len, s, len, delims, n) \+ for (__auto_type __text##n = &(s)[0]; __text##n; __text##n = NULL) \+ for (size_t __pos##n = 0, __start##n, __size##n = (len); \+ ((tok_len) = token_next(__text##n, __size##n, delims, \+ &__pos##n, &__start##n)) && \+ ((tok) = __text##n + __start##n, true);)+#define __for_each_token_n(tok, tok_len, s, len, delims, n) \+ __for_each_token(tok, tok_len, s, len, delims, n)+#define for_each_token(tok, tok_len, s, len, delims) \+ __for_each_token_n(tok, tok_len, s, len, delims, __COUNTER__)+/** A DNS host name: labels of letters, digits and inner hyphens, 1 to 63* bytes each and 253 in all. A name whose labels are all decimal or 0xdiff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.listindex 96840920..12cb46b2 100644--- a/testing/proofs/cbmc/proofs.list+++ b/testing/proofs/cbmc/proofs.list@@ -6,6 +6,7 @@ string-copy --unwind 10string-span --unwind 9string-suffix --unwind 8string-prefix --unwind 8+string-tokens --unwind 8 LEN=0-4string-sanitize --unwind 8 LEN=0-6hex --unwind 12io-lines --unwind 10diff --git a/testing/proofs/cbmc/string-tokens.c b/testing/proofs/cbmc/string-tokens.cnew file mode 100644index 00000000..a39bd0c7--- /dev/null+++ b/testing/proofs/cbmc/string-tokens.c@@ -0,0 +1,83 @@+// SPDX-License-Identifier: BSD-2-Clause+/*+ * Copyright (c) 2026 Authors of shirudo+ *+ * token_next() and for_each_token() of util/string.c on every span of LEN+ * bytes, every one or two delimiters and every cursor: a token lies+ * inside the span, holds no delimiter and has one or the span's end on+ * either side, the cursor ends past it, and a walk gives the bytes that+ * are no delimiter in order, each once, reading the span's bytes alone.+ */++#include <stdbool.h>++#include "proof.h"+#include "../../../util/string.c"++static char delims[3];++static bool delimits(char c)+{+ return c == delims[0] || (delims[1] && c == delims[1]);+}++static void any_delims(void)+{+ char first, second;++ __CPROVER_assume(first);+ delims[0] = first;+ delims[1] = second;+}++static void one_token(const char *s)+{+ size_t pos, start, was, n, i;++ was = pos;+ n = token_next(s, LEN, delims, &pos, &start);+ __CPROVER_assert(start <= LEN && n <= LEN - start,+ "a token inside the span");+ __CPROVER_assert(pos <= LEN && pos >= start + n && pos - start - n <= 1,+ "the cursor past the token and one delimiter");+ for (i = 0; i < LEN; i++) {+ if (i >= was && i < start)+ __CPROVER_assert(delimits(s[i]),+ "only delimiters before the token");+ if (i >= start && i < start + n)+ __CPROVER_assert(!delimits(s[i]),+ "no delimiter in the token");+ if (i == start + n)+ __CPROVER_assert(delimits(s[i]) && pos == i + 1,+ "a delimiter ends it, and is skipped");+ }+ __CPROVER_assert(n || start == LEN, "no token only at the span's end");+ __CPROVER_assert(was > LEN || start >= was,+ "a token at or after the cursor");+}++static void whole_walk(const char *s)+{+ size_t n, i, seen = 0, want = 0, next = 0;+ const char *tok;++ for_each_token(tok, n, s, LEN, delims) {+ __CPROVER_assert(n && tok >= s + next &&+ n <= LEN - (size_t)(tok - s),+ "a token of a byte or more, past the last");+ next = (size_t)(tok - s) + n;+ seen += n;+ }+ for (i = 0; i < LEN; i++)+ want += !delimits(s[i]);+ __CPROVER_assert(seen == want, "every byte that is no delimiter");+}++void harness(void)+{+ const char *s = any_bytes(LEN);++ any_delims();+ one_token(s);+ whole_walk(s);+}diff --git a/testing/unit/string.c b/testing/unit/string.cindex df7360fe..41c5845e 100644--- a/testing/unit/string.c+++ b/testing/unit/string.c@@ -663,6 +663,116 @@ static void span_prefix_reads_no_more_than_the_span(void)xfree(exact, 12);}+/* The tokens of the len bytes at text, each in brackets. */+static const char *tokens_of(const char *text, size_t len, const char *delims)+{+ static char out[64];+ char *buf = span(text, len);+ const char *tok;+ size_t n, at = 0;++ for_each_token(tok, n, buf, len, delims) {+ ASSERT_TRUE(tok >= buf && n <= len &&+ (size_t)(tok - buf) <= len - n);+ ASSERT_TRUE(at + n + 3 <= sizeof(out));+ out[at++] = '[';+ memcpy(out + at, tok, n);+ at += n;+ out[at++] = ']';+ }+ out[at] = '\0';+ xfree(buf, len ? len : 1);+ return out;+}++static void tokens_are_the_runs_between_delimiters(void)+{+ EXPECT_STREQ(tokens_of("a,bc,d", 6, ","), "[a][bc][d]");+ EXPECT_STREQ(tokens_of("abc", 3, ","), "[abc]");+ EXPECT_STREQ(tokens_of("a b\tc\n", 6, " \t\n"), "[a][b][c]");+ EXPECT_STREQ(tokens_of("/var//lib/", 10, "/"), "[var][lib]");+}++static void no_token_is_empty(void)+{+ EXPECT_STREQ(tokens_of("", 0, ","), "");+ EXPECT_STREQ(tokens_of(",", 1, ","), "");+ EXPECT_STREQ(tokens_of(",,,", 3, ","), "");+ EXPECT_STREQ(tokens_of(",,a,,", 5, ","), "[a]");+ EXPECT_STREQ(tokens_of("\n\nx\n\ny\n", 7, "\n"), "[x][y]");+}++static void tokens_stop_at_the_length_not_at_a_nul(void)+{+ const char *tok;+ size_t n, count = 0;++ EXPECT_STREQ(tokens_of("a,b,c", 3, ","), "[a][b]");+ EXPECT_STREQ(tokens_of("a,b,c", 4, ","), "[a][b]");+ for_each_token(tok, n, "a\0b\nc", 5, "\n") {+ if (!count++) {+ EXPECT_UEQ(n, 3);+ EXPECT_MEMEQ(tok, "a\0b", 3);+ } else {+ EXPECT_UEQ(n, 1);+ EXPECT_TRUE(*tok == 'c');+ }+ }+ EXPECT_UEQ(count, 2);+ EXPECT_STREQ(tokens_of("abc", 3, ""), "[abc]");+}++static void token_may_be_ended_in_its_own_buffer(void)+{+ char words[] = "-O2 -g\t-Wall", *tok, *seen[4];+ size_t n, count = 0;++ for_each_token(tok, n, words, strlen(words), " \t") {+ tok[n] = '\0';+ ASSERT_TRUE(count < ARRAY_SIZE(seen));+ seen[count++] = tok;+ }+ ASSERT_EQ(count, 3);+ EXPECT_STREQ(seen[0], "-O2");+ EXPECT_STREQ(seen[1], "-g");+ EXPECT_STREQ(seen[2], "-Wall");+}++static void token_walks_nest(void)+{+ const char *text = "a=1,b=2\nc=3\n", *line, *pair;+ size_t line_len, pair_len, pairs = 0, lines = 0;++ for_each_token(line, line_len, text, strlen(text), "\n") {+ lines++;+ for_each_token(pair, pair_len, line, line_len, ",") {+ EXPECT_UEQ(pair_len, 3);+ EXPECT_TRUE(pair[1] == '=');+ pairs++;+ }+ }+ EXPECT_UEQ(lines, 2);+ EXPECT_UEQ(pairs, 3);+}++static void token_next_takes_any_cursor(void)+{+ size_t pos = 2, start = 9;++ EXPECT_UEQ(token_next("ab,cd", 5, ",", &pos, &start), 2);+ EXPECT_UEQ(start, 3);+ EXPECT_UEQ(pos, 5);+ EXPECT_UEQ(token_next("ab,cd", 5, ",", &pos, &start), 0);+ EXPECT_UEQ(pos, 5);+ pos = 9;+ EXPECT_UEQ(token_next("ab,cd", 5, ",", &pos, &start), 0);+ EXPECT_UEQ(pos, 5);+ pos = 0;+ EXPECT_UEQ(token_next("ab,cd", 5, ",", &pos, &start), 2);+ EXPECT_UEQ(start, 0);+ EXPECT_UEQ(pos, 3);+}+static const struct unit_case cases[] = {UNIT_CASE(digits_counts_leading_digits),UNIT_CASE(decimal_fills_its_span),@@ -699,6 +809,12 @@ static const struct unit_case cases[] = {UNIT_CASE(str_after_prefix_nocase_takes_either_case),UNIT_CASE(span_equals_str_wants_the_whole_string),UNIT_CASE(span_prefix_reads_no_more_than_the_span),+ UNIT_CASE(tokens_are_the_runs_between_delimiters),+ UNIT_CASE(no_token_is_empty),+ UNIT_CASE(tokens_stop_at_the_length_not_at_a_nul),+ UNIT_CASE(token_may_be_ended_in_its_own_buffer),+ UNIT_CASE(token_walks_nest),+ UNIT_CASE(token_next_takes_any_cursor),{}};diff --git a/util/string.c b/util/string.cindex 8ecf5916..cc8d4313 100644--- a/util/string.c+++ b/util/string.c@@ -2,7 +2,8 @@/** Copyright (c) 2026 Authors of shirudo*- * Bounded number parsing, string copies and formatting, DNS names.+ * Bounded number parsing, string copies and formatting, tokens, DNS+ * names, UTF-8 characters and text for a terminal.*/#include <errno.h>@@ -123,6 +124,28 @@ int span_to_str(char *dst, size_t cap, const void *src, size_t len)return 0;}+static bool among(char c, const char *set)+{+ for (; *set; set++)+ if (c == *set)+ return true;+ return false;+}++size_t token_next(const char *s, size_t len, const char *delims, size_t *pos,+ size_t *start)+{+ size_t at = min(*pos, len), n = 0;++ while (at < len && among(s[at], delims))+ at++;+ while (n < len - at && !among(s[at + n], delims))+ n++;+ *start = at;+ *pos = at + n + (n < len - at);+ return n;+}+static bool numeric_dns_label(const char *label, size_t len){size_t i = 0;