https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120735
Bug ID: 120735
Summary: -Warray-bounds error via std::vector::data after
unsigned int overflow potential
Product: gcc
Version: 15.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: drahflow at gmx dot de
Target Milestone: ---
Created attachment 61672
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=61672&action=edit
Reproducer
Consider https://godbolt.org/z/7hne4hfva
unsigned char f(unsigned int n) {
unsigned int dataSize = 4;
//assert(dataSize + n > dataSize); /* works with this assert enabled */
dataSize += n;
//assert(dataSize > 7); /* works with this assert enabled */
vector<unsigned char> buf;
buf.resize(dataSize);
unsigned char *ptr = buf.data(); /* works with a raw array (cf. godbolt)
*/
ptr += 1; /* works without this calculation */
memcpy(ptr, "abc", 3);
return *ptr;
}
it results in
<source>:17:11: error: 'void* memcpy(void*, const void*, size_t)' offset [0, 2]
is out of the bounds [0, 0] [-Werror=array-bounds=]
17 | memcpy(ptr, "abc", 3);
This error appears for 11.1 up to trunk.