llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

-Wstringop-overread not implemented in Clang

Open ldionne opened this issue 3 years ago • 3 comments

GCC warns on the following code:

#include <cstring>

void f(char* dst){
    std::memcpy(dst, "abc", 5);
}
<source>: In function 'void f(char*)':
<source>:4:16: warning: 'void* memcpy(void*, const void*, size_t)' reading 5 bytes from a region of size 4 [-Wstringop-overread]
    4 |     std::memcpy(dst, "abc", 5);
      |     ~~~~~~~~~~~^~~~~~~~~~~~~~~
Compiler returned: 0

Clang doesn't warn. I tried with -Wall -Wextra. It seems like it should be fairly easy to warn on this with Clang, perhaps we should implement that diagnostic?

https://godbolt.org/z/qP736q5je

ldionne avatar Nov 15 '23 23:11 ldionne

Confirmed.

CC @AaronBallman

shafik avatar Nov 16 '23 05:11 shafik

GCC implements a whole lot of those diagnostics in their middle/back-end and they're based on optimizer behavior, so we're not likely to pick up all of GCC's diagnostic behavior if we did implement this one. That said, so long as we're going through the builtin function, it seems reasonable to catch static cases like this when we can. Anything more complicated than static cases should be handled by the static analyzer though because they'll require dataflow analysis.

AaronBallman avatar Nov 16 '23 12:11 AaronBallman

Given PR #183004 has been merged, I propose this issue either be closed or marked as a duplicate of #83728. Although I should note the PR only handles the static cases.

jpjepko avatar Jun 23 '26 18:06 jpjepko