-Wstringop-overread not implemented in Clang
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
Confirmed.
CC @AaronBallman
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.
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.