[Bug tree-optimization/105078] Maybe wrong *** buffer overflow detected ***: terminated with -D_FORTIFY_SOURCE
marxin at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Mar 28 14:06:33 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105078
--- Comment #4 from Martin Liška <marxin at gcc dot gnu.org> ---
Note the libQt6 version of the function looking approximately like this:
#include <cstdlib>
#include <cstdint>
#include <unistd.h>
struct QArrayData {
int size;
__attribute__((malloc))
static void *allocate(QArrayData **pdata, size_t size, size_t
alignment) {
size_t headerSize = sizeof(QArrayData);
headerSize += (alignment - alignof(QArrayData));
*pdata = static_cast<QArrayData *>(::malloc(headerSize +
size));
(*pdata)->size = size;
return reinterpret_cast<void *>(uintptr_t(*pdata) +
headerSize);
}
};
template <class T>
struct QTypedArrayData : QArrayData {
class AlignmentDummy { QArrayData header; T data; };
static QTypedArrayData *allocate(size_t size) {
QArrayData *d;
QArrayData::allocate(&d, size, alignof(AlignmentDummy));
return static_cast<QTypedArrayData *>(d);
}
static T *dataStart(QArrayData *data, size_t alignment) {
void *start = reinterpret_cast<void *>((uintptr_t(data) +
sizeof(QArrayData) + alignment - 1) & ~(alignment - 1));
return static_cast<T *>(start);
}
};
int main()
{
int size = 256;
auto *data = QTypedArrayData<char>::allocate(size);
return readlink("asdf", data->dataStart(data,
alignof(QTypedArrayData<char>::AlignmentDummy)), data->size - 1);
}
More information about the Gcc-bugs
mailing list