[PATCH] cmd/mem.c: fix undefined behavior in mem cmp

Quentin Schulz quentin.schulz at cherry.de
Mon Sep 30 10:38:35 CEST 2024


Hi Rasmus,

On 9/27/24 8:56 PM, Rasmus Villemoes wrote:
> Quentin Schulz <foss+uboot at 0leil.net> writes:
> 
>> From: Quentin Schulz <quentin.schulz at cherry.de>
>>
>> My linter complains that "When using void pointers in calculations, the
>> behaviour is undefined".
>>
>> GCC does say that "In GNU C, addition and subtraction operations are
>> supported on pointers to void"[1] but this hints at this only being
>> supported in the GNU flavor of C. And I assume U-Boot may want to be
>> compiled with clang/llvm?
>>
>> Let's fix that warning by casting the void pointer to a u8 pointer since
>> the size variable unit is byte.
>>
>> [1] https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html
>>
> 
> No, let's please not. Try enabling -Wpointer-arith and see how much
> churn that would require all over the tree (doing it in this one place
> would be pointless), and all the casts would make the code much much
> harder to read.
> 

That alone is not a justification.

> We do rely on lots of gcc extensions, and Clang has documented that it
> "aims to support a broad range of GCC extensions". Arithmetic on void is
> one of them, and that's not going to go away.
> 

But this very well could/is.

1) is there a way to tell Clang that we want to follow this GNU 
extension for that case? Is that the default? I can see there are 
warnings for something seemingly related in llvm 
(gnu-null-pointer-arithmetic, gnu-pointer-arith, ...?).

2) do we document somewhere the GNU C extensions we use on the whole tree?

3) is anyone aware of a way to silence some specific warnings at the 
project level so that clang takes care of it so that linters use those 
settings so we avoid people sending more commits for that "issue"?

Cheers,
Quentin


More information about the U-Boot mailing list