Skip to content

eBPF-based security platform

treewide: measure up to a byte of a set with span_until()

Daniel Borkmann9dbf3d1f8612
diff --git a/testing/proofs/cbmc/proof.h b/testing/proofs/cbmc/proof.h
index 38154f8c..3719c90e 100644
--- a/testing/proofs/cbmc/proof.h
+++ b/testing/proofs/cbmc/proof.h
@@ -32,8 +32,8 @@
#undef strstr
/*
- * CBMC models neither strnlen(), memchr(), memmem(), strcspn(), strstr()
- * nor vsnprintf(), and a function without a body returns anything.
+ * CBMC models neither strnlen(), memchr(), memmem(), strstr() nor
+ * vsnprintf(), and a function without a body returns anything.
*/
size_t strnlen(const char *s, size_t max)
{
@@ -90,17 +90,6 @@ void *memmem(const void *hay, size_t hay_len, const void *needle,
return NULL;
}
-size_t strcspn(const char *s, const char *reject)
-{
- size_t n, i;
-
- for (n = 0; s[n]; n++)
- for (i = 0; reject[i]; i++)
- if (s[n] == reject[i])
- return n;
- return n;
-}
-
char *strstr(const char *s, const char *needle)
{
size_t n = strlen(needle);
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 69b2acc4..ea259f16 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -83,8 +83,8 @@ bpm-tar-octal --unwind 13 LEN=8,12
bpm-tar-header --unwind 9 --unwindset bpm_tar_header_ok.0:513,windowed_header.0:513 --max-field-sensitivity-array-size 512 LEN=0,116,448
bpm-archive-path --unwind 12 LEN=0-10
bpm-semver-valid --unwind 13
-bpm-semver-order --unwind 9 --unwindset prerelease_compare.0:2,memchr.0:2,memcmp.0:2,strcspn.1:2
-bpm-semver-transitive --unwind 9 --unwindset prerelease_compare.0:2,memchr.0:2,memcmp.0:2,strcspn.1:2
+bpm-semver-order --unwind 9 --unwindset prerelease_compare.0:2,memchr.0:2,memcmp.0:2,span_until.1:2
+bpm-semver-transitive --unwind 9 --unwindset prerelease_compare.0:2,memchr.0:2,memcmp.0:2,span_until.1:2
bpm-semver-prerelease --unwind 7
bpm-oci-challenge --unwind 18
bpm-oci-token --unwind 18
@@ -98,7 +98,7 @@ bpm-tree-reserve --unwind 1
bpm-tree-manifest --max-field-sensitivity-array-size 768 --unwind 80 --unwindset memcmp.0:770 LEN=0-2
bpm-source-config --unwind 24
bpm-oci-strings --unwind 9
-bpm-semver-model --slice-formula --unwind 1 --unwindset strlen.0:10,strcmp.0:10,strcspn.0:2,strcspn.1:4,digits.0:10,parse_base.0:10,parse_base.1:10,semver_parse.0:4,semver_identifiers.0:4,semver_identifiers.1:3,prerelease_compare.0:3,memchr.0:4,memcmp.0:4,bpm_semver_compare.0:4,strnlen.0:10,any_string.0:10,break_at.0:10,split_on.0:10,next_ident.0:4,numeral.0:8,numeric.0:8,ident_ok.0:4,identifiers.0:3,lex_bytes.0:10,memcmp_model.0:4,lex_idents.0:3,semver_model_compare.0:4 LEN=9
+bpm-semver-model --slice-formula --unwind 1 --unwindset strlen.0:10,strcmp.0:10,span_until.0:2,span_until.1:4,digits.0:10,parse_base.0:10,parse_base.1:10,semver_parse.0:4,semver_identifiers.0:4,semver_identifiers.1:3,prerelease_compare.0:3,memchr.0:4,memcmp.0:4,bpm_semver_compare.0:4,strnlen.0:10,any_string.0:10,break_at.0:10,split_on.0:10,next_ident.0:4,numeral.0:8,numeric.0:8,ident_ok.0:4,identifiers.0:3,lex_bytes.0:10,memcmp_model.0:4,lex_idents.0:3,semver_model_compare.0:4 LEN=9
chain-events --unwind 258 LEN=0-8,255-257
hex-model --unwind 20
paths-bpffs-name-model --unwind 20
@@ -114,7 +114,7 @@ similar-distance --max-field-sensitivity-array-size 65 --unwind 8 --unwindset me
similar-suggest --object-bits 10 --max-field-sensitivity-array-size 65 --unwind 8 --unwindset memcpy.0:14 LEN=0-26
similar-resolve --object-bits 10 --unwind 8
options-next --object-bits 12 --unwind 8 LEN=0-6
-options-long --object-bits 12 --unwind 8 --unwindset strcspn.1:67,strlen.0:67 LEN=64-66
+options-long --object-bits 12 --unwind 8 --unwindset span_until.1:67,strlen.0:67 LEN=64-66
options-table --object-bits 12 --unwind 8 --unwindset strcmp.0:12
config-evidence-event --unwind 1 --unwindset harness.0:33,same_event.0:33,hex_digits.0:65,room_for.0:10,on_event.0:4,hex_decode.0:33,strlen.0:66 LEN=0,1,15,16,128,4095,4096
config-evidence-parse --unwind 1 --unwindset config_evidence_parse.0:7,on_event.0:4,replay.0:4,hex_decode.0:33,strlen.0:66,strcmp.0:15,named.0:7,json_parse.0:7,json_parse.1:4,give_event.0:4,give_text.0:66,any_document.0:4,whole.0:4,in_order.0:4,hex_digits.0:65,spells.0:33,blank.0:33,check_as_said.0:4,check_as_said.1:33 --memory-leak-check --slice-formula LEN=0-2
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index d3f4b10e..c7b221ac 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -174,12 +174,12 @@ static size_t bpm_header(void *ptr, size_t size, size_t nmemb, void *ud)
} else if (str_skip_prefix_nocase(&v, "Content-Type:")) {
while (isspace((__u8)*v))
v++;
- v[strcspn(v, " ;\r\n")] = '\0';
+ v[span_until(v, strlen(v), " ;\r\n")] = '\0';
bpm_copy_string(r->content_type, sizeof(r->content_type), v);
} else if (str_skip_prefix_nocase(&v, "Location:")) {
while (isspace((__u8)*v))
v++;
- v[strcspn(v, "\r\n")] = '\0';
+ v[span_until(v, strlen(v), "\r\n")] = '\0';
bpm_copy_string(r->location, sizeof(r->location), v);
}
return n;
diff --git a/util/bpm_package.c b/util/bpm_package.c
index f1636a88..32e4f966 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -165,7 +165,7 @@ static bool semver_parse(const char *s, struct semver *v)
v->pre = NULL;
v->pre_len = 0;
if (*p == '-') {
- n = strcspn(p + 1, "+");
+ n = span_until(p + 1, strlen(p + 1), "+");
if (!semver_identifiers(p + 1, n, true))
return false;
v->pre = p + 1;
diff --git a/util/bpm_source_config.c b/util/bpm_source_config.c
index 5dda4474..b75a88e0 100644
--- a/util/bpm_source_config.c
+++ b/util/bpm_source_config.c
@@ -37,7 +37,7 @@ int bpm_parse_source_line(char *line,
val = eq;
while (isspace((__u8)*val))
val++;
- val[strcspn(val, "\r\n")] = '\0';
+ val[span_until(val, strlen(val), "\r\n")] = '\0';
if (!strcmp(key, "endpoint") && strscpy(endpoint, val, endpoint_sz) < 0)
return -E2BIG;
diff --git a/util/options.c b/util/options.c
index c666ba3b..62240cfe 100644
--- a/util/options.c
+++ b/util/options.c
@@ -95,7 +95,7 @@ static int spec_find(const struct opt_parser *p, const char *arg,
return (int)i;
return -1;
}
- len = strcspn(arg, "=");
+ len = span_until(arg, strlen(arg), "=");
if (arg[len])
*eq = arg + len + 1;
for (i = 0; i < p->n_specs; i++) {
@@ -109,10 +109,12 @@ static int spec_find(const struct opt_parser *p, const char *arg,
static int opt_unknown(const struct opt_parser *p, const char *arg)
{
- size_t i, len = arg[1] == '-' ? strcspn(arg, "=") : strlen(arg);
+ size_t i, len = strlen(arg);
const char *names[OPT_MAX];
char bad[SIMILAR_MAX_LEN + 1];
+ if (arg[1] == '-')
+ len = span_until(arg, len, "=");
pr_err("%s: unknown option '%.*s'", p->command, (int)len, arg);
if (len >= sizeof(bad))
return OPT_USAGE;
diff --git a/util/sysinfo.c b/util/sysinfo.c
index 7922ba63..ef7a7a8e 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -40,8 +40,7 @@ static void read_first_line(const char *path, char *buf, size_t cap)
buf[0] = '\0';
return;
}
- buf[n] = '\0';
- buf[strcspn(buf, "\r\n")] = '\0';
+ buf[span_until(buf, (size_t)n, "\r\n")] = '\0';
}
#define OS_RELEASE_SIZE_MAX (64u * 1024u)
@@ -266,7 +265,7 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
while (fgets(line, sizeof(line), f)) {
char *key, *value, *eq, *not_set, unset[] = "n";
- line[strcspn(line, "\r\n")] = '\0';
+ line[span_until(line, strlen(line), "\r\n")] = '\0';
key = trim_config_text(line);
if (!*key)
continue;