Fix GCC12 warning about using a pointer after realloc()

qmalloc.cpp:92:18: warning: pointer may be used after ?void* realloc(void*, size_t)? [-Wuse-after-free]
   92 |         qptrdiff oldoffset = static_cast<char *>(oldptr) - static_cast<char *>(actualptr);
      |                  ^~~~~~~~~

Pick-to: 6.2 6.3
Change-Id: I74249c52dc02478ba93cfffd16d230165b6f030f
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Thiago Macieira 2022-02-09 10:01:26 -08:00
parent 52a83658c3
commit 6586b030d1
1 changed files with 2 additions and 2 deletions

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -80,6 +80,7 @@ void *qReallocAligned(void *oldptr, size_t newsize, size_t oldsize, size_t align
// However, we need to store the actual pointer, so we need to allocate actually size +
// alignment anyway.
qptrdiff oldoffset = oldptr ? static_cast<char *>(oldptr) - static_cast<char *>(actualptr) : 0;
void *real = realloc(actualptr, newsize + alignment);
if (!real)
return nullptr;
@ -89,7 +90,6 @@ void *qReallocAligned(void *oldptr, size_t newsize, size_t oldsize, size_t align
void **faked_ptr = reinterpret_cast<void **>(faked);
if (oldptr) {
qptrdiff oldoffset = static_cast<char *>(oldptr) - static_cast<char *>(actualptr);
qptrdiff newoffset = reinterpret_cast<char *>(faked_ptr) - static_cast<char *>(real);
if (oldoffset != newoffset)
memmove(faked_ptr, static_cast<char *>(real) + oldoffset, qMin(oldsize, newsize));