Re: issue with _Static_assert and __builtin()s

Ben Dooks <[email protected]> Thu, 8 Jan 2026 09:38:25 +0000
Newsgroups org.kernel.vger.linux-sparse
Organization Codethink Limited.
Message-ID <[email protected]>
On 08/01/2026 05:41, Dan Carpenter wrote:
> On Wed, Jan 07, 2026 at 07:41:45PM +0000, Ben Dooks wrote:
>> 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.
>>
> 
> Al has a fix for that.
> https://git.kernel.org/pub/scm/linux/kernel/git/viro/sparse.git/commit/?id=2634e39bf02697a18fece057208150362c985992

Ah thanks, that'll save me a few hours of prodding my code to try
getting it working.

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

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