From 6586b030d163a4a63328cb83a25dd21411dcbb9e Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 9 Feb 2022 10:01:26 -0800 Subject: [PATCH] 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(oldptr) - static_cast(actualptr); | ^~~~~~~~~ Pick-to: 6.2 6.3 Change-Id: I74249c52dc02478ba93cfffd16d230165b6f030f Reviewed-by: Marc Mutz --- src/corelib/global/qmalloc.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/corelib/global/qmalloc.cpp b/src/corelib/global/qmalloc.cpp index aeba9ed19b..3be1bf53d4 100644 --- a/src/corelib/global/qmalloc.cpp +++ b/src/corelib/global/qmalloc.cpp @@ -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(oldptr) - static_cast(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(faked); if (oldptr) { - qptrdiff oldoffset = static_cast(oldptr) - static_cast(actualptr); qptrdiff newoffset = reinterpret_cast(faked_ptr) - static_cast(real); if (oldoffset != newoffset) memmove(faked_ptr, static_cast(real) + oldoffset, qMin(oldsize, newsize));