Am Freitag, dem 09.08.2024 um 01:10 +0200 schrieb Alejandro Colomar via Gcc:
> Hi,
>
> I've seen some bogus warning in GCC that suggests that some use of auto
> may cause undefined behavior (due to double evaluation).
>
> $ cat auto.c
> #include <stdio.h>
>
> int
> main(void)
> {
> int i = 3;
> int a[2 * i];
> int (*p)[2 * i];
>
> i = 1;
> p = &a;
> auto q = p + i++;
> }
> $ gcc -Wsequence-point -std=c23 auto.c
> auto.c: In function ‘main’:
> auto.c:12:23: warning: operation on ‘i’ may be undefined [-Wsequence-point]
> 12 | auto q = p + i++;
> | ~^~
>
>
> I originally found this problem here:
> <https://software.codidact.com/comments/thread/9880#comment-24855>
>
> And I suspect the warning is confusing auto with typeof(). If typeof()
> was used, this would indeed cause double evaluation, once before the =
> and once after the =, causing UB.
>
> Maybe it's due to how auto is implemented? I suspect it's doing
> internally:
>
> typeof(p + i++) q = p + i++;
>
> and that would be problematic. I can reproduce it with both gcc-13 and
> gcc-14.
>
Can you file a bug if there isn't one already?
BTW: Double evaluation is not only a problem for semantics, but also because
it can cause long compile times, cf. the 45 MB macro expansion example
in Linux...
Martin