From 83ac6bfa0fb2fc89195cf68f608cd2db82209729 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:22:59 +0100 Subject: [PATCH] Fix crash when calling estimateSize() (f'up to #12139) (#5628) --- lib/checkother.cpp | 3 ++- test/cfg/qt.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 51f276c65..32ab74b9c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2887,7 +2887,8 @@ void CheckOther::checkRedundantCopy() if (fScope && fScope->bodyEnd && Token::Match(fScope->bodyEnd->tokAt(-3), "return %var% ;")) { const Token* varTok = fScope->bodyEnd->tokAt(-2); if (varTok->variable() && !varTok->variable()->isGlobal() && - (!varTok->variable()->type() || estimateSize(varTok->variable()->type(), mSettings, symbolDatabase) > 2 * mSettings->platform.sizeof_pointer)) + (!varTok->variable()->type() || !varTok->variable()->type()->classScope || + estimateSize(varTok->variable()->type(), mSettings, symbolDatabase) > 2 * mSettings->platform.sizeof_pointer)) redundantCopyError(startTok, startTok->str()); } } diff --git a/test/cfg/qt.cpp b/test/cfg/qt.cpp index 73650f09c..63ccf3b1d 100644 --- a/test/cfg/qt.cpp +++ b/test/cfg/qt.cpp @@ -580,3 +580,16 @@ namespace { Q_PROPERTY(QHash hash READ hash WRITE setHash) }; } + +struct SEstimateSize { + inline const QString& get() const { return m; } + QString m; +}; + +class QString; + +void dontCrashEstimateSize(const SEstimateSize& s) { + // cppcheck-suppress redundantCopyLocalConst + QString q = s.get(); + if (!q.isNull()) {} +}