[BUG] missing warning for pointer arithmetic out of bounds
Alejandro Colomar
alx.manpages@gmail.com
Tue Dec 13 19:24:47 GMT 2022
Hi Paul,
On 12/13/22 20:22, Paul Koning wrote:
>
>
>> On Dec 13, 2022, at 2:08 PM, Alejandro Colomar via Gcc <gcc@gcc.gnu.org> wrote:
>>
>> Hi!
>>
>> For the following program:
>>
>>
>> $ cat buf.c
>> #include <stdio.h>
>>
>> int main(void)
>> {
>> char *p, buf[5];
>>
>> p = buf + 6;
>> printf("%p\n", p);
>> }
>>
>>
>> There are no warnings in gcc, as I would expect:
>>
>> $ gcc -Wall -Wextra buf.c -O0
>>
>> Clang does warn, however:
>>
>> $ clang -Weverything -Wall -Wextra buf.c -O0
>> buf.c:8:17: warning: format specifies type 'void *' but the argument has type 'char *' [-Wformat-pedantic]
>> printf("%p\n", p);
>> ~~ ^
>> %s
>> buf.c:7:6: warning: the pointer incremented by 6 refers past the end of the array (that contains 5 elements) [-Warray-bounds-pointer-arithmetic]
>> p = buf + 6;
>> ^ ~
>
> I thought void * is a generic pointer that accepts any pointer argument. So a warning about char* being passed in seems to be flat out wrong.
>
>> buf.c:5:2: note: array 'buf' declared here
>> char *p, buf[5];
>> ^
>> 2 warnings generated.
>
> That was discussed just days ago: C says that a pointer one past the end of the array is legal. So here too it looks like Clang is wrong and GCC is right.
Look again:
char *p, buf[5];
p = buf + 6;
That's a pointer two-past-the-end; not one.
That's UB.
Cheers,
Alex
>
> paul
>
--
<http://www.alejandro-colomar.es/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20221213/acd2dde8/attachment.sig>
More information about the Gcc
mailing list