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.
Have a lovely night!
Alex
--
<https://www.alejandro-colomar.es/>
signature.asc
Description: PGP signature
