public inbox for linux-sparse@vger.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@linaro.org>
To: linux-sparse@vger.kernel.org
Cc: Ricardo Ribalda <ribalda@chromium.org>,
	Kees Cook <kees@kernel.org>,
	linux-media@vger.kernel.org
Subject: [PATCH] builtin: implement __builtin_strlen() for constants
Date: Wed, 15 Oct 2025 16:08:13 +0300	[thread overview]
Message-ID: <aO-cvXncl7dbnP_J@stanley.mountain> (raw)

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.

Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Link: https://lore.kernel.org/all/CANiDSCsBAq3Yx4ybarUb_1NkQ-bvfXvWqb-DfqXatkiYJFZWiQ@mail.gmail.com/
---
 builtin.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/builtin.c b/builtin.c
index 3a29c3aec8a6..9e8fa5964b37 100644
--- a/builtin.c
+++ b/builtin.c
@@ -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 }},
-- 
2.51.0


             reply	other threads:[~2025-10-15 13:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-15 13:08 Dan Carpenter [this message]
2025-10-15 15:13 ` [PATCH] builtin: implement __builtin_strlen() for constants Linus Torvalds
2025-10-16  8:29   ` Dan Carpenter
2025-10-16 12:05     ` Chris Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aO-cvXncl7dbnP_J@stanley.mountain \
    --to=dan.carpenter@linaro.org \
    --cc=kees@kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=ribalda@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox