public inbox for linux-sparse@vger.kernel.org
 help / color / mirror / Atom feed
* issue with _Static_assert and __builtin()s
@ 2026-01-07 19:41 Ben Dooks
  2026-01-08  5:41 ` Dan Carpenter
  0 siblings, 1 reply; 6+ messages in thread
From: Ben Dooks @ 2026-01-07 19:41 UTC (permalink / raw)
  To: linux-sparse

[-- Attachment #1: Type: text/plain, Size: 1036 bytes --]

So I am looking at why the Linux kernel's check for nul characters
in strings is causing errors out of sparse.

EG:
drivers/md/dm.c:3813:1: error: bad constant expression
drivers/md/dm.c:3814:1: error: bad constant expression
drivers/md/dm.c:3816:1: error: bad constant expression
drivers/md/dm.c:3817:1: error: bad constant expression

I've tracked it down to the sizeof(str) - 1 == __builtin_strlen(str)
failing to be a good constant expression...

This is an example of the assert which isn't working:

_Static_assert(sizeof("moo") - 1 == __builtin_strlen("moo"), "nul!");

This does at least get past w/o warnings
_Static_assert(__builtin_types_compatible_p(int, int), "doh!");

I've had a go at updating builtin.c to deal with __builtin_strlen()
for a string constant (attached) but that's just changing the
output to "error: bad integer constant expression" so not sure
what I've missed here.


-- 
Ben Dooks				http://www.codethink.co.uk/
Senior Engineer				Codethink - Providing Genius

https://www.codethink.co.uk/privacy.html

[-- Attachment #2: initial-strlen.patch --]
[-- Type: text/x-patch, Size: 1768 bytes --]

diff --git a/builtin.c b/builtin.c
index e4751445..d7e56b34 100644
--- a/builtin.c
+++ b/builtin.c
@@ -596,6 +596,49 @@ static struct symbol_op object_size_op = {
 	.expand = expand_object_size,
 };
 
+#include <string.h>
+static int expand_strlen(struct expression *expr, int cost)
+{
+	struct expression *init, *arg = first_expression(expr->args);
+	unsigned long val = 0;
+
+	if (!arg)
+		return UNSAFE;	// ? ok
+
+	switch (arg->type) {
+	case EXPR_STRING:
+		//todo//
+		break;
+	case EXPR_SYMBOL:
+		if (arg->symbol->ident)
+			goto not_literal;
+
+		init = arg->symbol->initializer;
+		if (!init || init->type != EXPR_STRING)
+			goto not_literal;
+
+		val = strlen(init->string->data);
+		break;
+	default:
+		goto not_literal;
+		break;
+	}
+
+	expr->type = EXPR_VALUE;
+	expr->flags |= CEF_SET_ICE;
+	expr->value = val;
+	expr->taint = 0;
+	return 0;
+
+not_literal:
+	return UNSAFE;
+}
+
+
+static struct symbol_op strlen_op = {
+	.expand = expand_strlen,
+};
+
 /*
  * Builtin functions
  */
@@ -775,7 +818,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, 1, { &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 }},

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-02-10 15:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-07 19:41 issue with _Static_assert and __builtin()s Ben Dooks
2026-01-08  5:41 ` Dan Carpenter
2026-01-08  9:38   ` Ben Dooks
2026-02-09 16:25   ` Jeff Johnson
2026-02-10 14:25     ` Ben Dooks
2026-02-10 14:40       ` Jeff Johnson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox