Skip to contentSkip to conversation

builtin: implement __builtin_strlen() for constants

PATCH In review Dan Carpenter 3 participants 4 messages Oct 15, 2025 – Oct 16, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 builtin: implement __builtin_strlen() for constants

People are adding compile time asserts to check whether strings are the expected length. In GCC and Clang strlen("foo") is expanded at compile time so this works, but in Sparse it triggers a "bad constant expression" warning. Implement expand_strlen() to handle string literals.

---
 builtin.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
builtin.c +23 -1
index 3a29c3aec8a6..9e8fa5964b37 100644
@@ -596,6 +596,28 @@ static struct symbol_op object_size_op = {
 	.expand = expand_object_size,
 };
 
+static int expand_strlen(struct expression *expr, int cost)
+{
+	struct expression *arg = first_expression(expr->args);
+
+	if (!arg)
+		return UNSAFE;
+	if (arg->type == EXPR_SYMBOL)
+		arg = arg->symbol->initializer;
+	if (!arg || arg->type != EXPR_STRING || !arg->string->length)
+		return UNSAFE;
+
+	expr->flags |= CEF_SET_ICE;
+	expr->type = EXPR_VALUE;
+	expr->value = arg->string->length - 1;
+	expr->taint = 0;
+	return 0;
+}
+
+static struct symbol_op strlen_op = {
+	.expand = expand_strlen,
+};
+
 /*
  * Builtin functions
  */
@@ -775,7 +797,7 @@ static const struct builtin_fn builtins_common[] = {
 	{ "__builtin_strcpy", &string_ctype, 0, { &string_ctype, &const_string_ctype }},
 	{ "__builtin_strcspn", size_t_ctype, 0, { &const_string_ctype, &const_string_ctype }},
 	{ "__builtin_strdup", &string_ctype, 0, { &const_string_ctype }},
-	{ "__builtin_strlen", size_t_ctype, 0, { &const_string_ctype }},
+	{ "__builtin_strlen", size_t_ctype, 0, { &const_string_ctype }, .op = &strlen_op},
 	{ "__builtin_strncasecmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
 	{ "__builtin_strncat", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
 	{ "__builtin_strncmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
Signature · 1 line
2.51.0
#1 lore raw
Quoted 6 lines · Dan Carpenter

People are adding compile time asserts to check whether strings are the expected length. In GCC and Clang strlen("foo") is expanded at compile time so this works, but in Sparse it triggers a "bad constant expression" warning. Implement expand_strlen() to handle string literals.

Ack.

Except it's not quite right.

Try this:

    int i(void)
    {
        return __builtin_strlen("hello\0hi");
    }

and you'll see that it returns 8, even though the correct string length is 5.

So you should add a

    #include <string.h>

at the top, and do something like

- expr->value = arg->string->length - 1;
+ expr->value = strlen(arg->string->data);

in there instead, because constant strings can have embedded NUL characters.

Linus

#2 lore raw
Quoted 9 lines · Linus Torvalds

So you should add a

    #include <string.h>

at the top, and do something like

(quoted diff) +1 -1
-       expr->value = arg->string->length - 1;
+       expr->value = strlen(arg->string->data);
Quoted 3 lines

in there instead, because constant strings can have embedded NUL characters.

Linus

Ah. Thanks.

regards,
dan carpenter

maintainer
#3 lore raw

Thanks,

Will apply with Linus' fix up.

Chris

Quoted 20 lines · Dan Carpenter
Quoted 9 lines · Linus Torvalds

So you should add a

    #include <string.h>

at the top, and do something like

(quoted diff) +1 -1
-       expr->value = arg->string->length - 1;
+       expr->value = strlen(arg->string->data);
Quoted 3 lines

in there instead, because constant strings can have embedded NUL characters.

Linus

Ah. Thanks.

regards,
dan carpenter