public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* [BUG] missing warning for pointer arithmetic out of bounds
@ 2022-12-13 19:08 Alejandro Colomar
  2022-12-13 19:15 ` Alejandro Colomar
  2022-12-13 19:22 ` Paul Koning
  0 siblings, 2 replies; 8+ messages in thread
From: Alejandro Colomar @ 2022-12-13 19:08 UTC (permalink / raw)
  To: gcc


[-- Attachment #1.1: Type: text/plain, Size: 946 bytes --]

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;
             ^     ~
     buf.c:5:2: note: array 'buf' declared here
         char *p, buf[5];
         ^
     2 warnings generated.

Cheers,

Alex


-- 
<http://www.alejandro-colomar.es/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-12-18 12:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-13 19:08 [BUG] missing warning for pointer arithmetic out of bounds Alejandro Colomar
2022-12-13 19:15 ` Alejandro Colomar
2022-12-13 19:18   ` Andrew Pinski
2022-12-18 12:48     ` Optimization levels for getting all warnings (was: [BUG] missing warning for pointer arithmetic out of bounds) Alejandro Colomar
2022-12-13 19:22   ` [BUG] missing warning for pointer arithmetic out of bounds David Malcolm
2022-12-13 19:22 ` Paul Koning
2022-12-13 19:24   ` Alejandro Colomar
2022-12-13 20:45   ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).