From 5c20f9aa5c31d30a2dccfc617e93c6d6ac0fc96c Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 30 Jan 2025 12:09:01 +0100 Subject: [PATCH] QPainterStateGuard: also check for nullptr painter in save(), restore() The ctor refuses to be called with a nullptr painter, but a moved-from QPainterStateGuard object _will_ have a nullptr painter. An assert gives a slightly nicer error message than "nullptr deref". Extract Method verifyPainter() to not litter the executable with string literals for every one of the these functions (some compilers don't use the leeway extended to them by the standard to fold duplicate string literals). Found in API-review. Amends ec3a5f4994a2bafc65fa8e01fb0861219580f622. Pick-to: 6.9 Change-Id: I562e6a0ff5a4c8ce86418b998ed78c501441b61a Reviewed-by: Giuseppe D'Angelo Reviewed-by: Axel Spoerl Reviewed-by: Juha Vuolle --- src/gui/painting/qpainterstateguard.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/gui/painting/qpainterstateguard.h b/src/gui/painting/qpainterstateguard.h index b6b5c039775..529c2bce5a0 100644 --- a/src/gui/painting/qpainterstateguard.h +++ b/src/gui/painting/qpainterstateguard.h @@ -33,7 +33,7 @@ public: explicit QPainterStateGuard(QPainter *painter, InitialState state = InitialState::Save) : m_painter(painter) { - Q_ASSERT(painter); + verifyPainter(); if (state == InitialState::Save) save(); } @@ -46,18 +46,25 @@ public: void save() { + verifyPainter(); m_painter->save(); ++m_level; } void restore() { + verifyPainter(); Q_ASSERT(m_level > 0); --m_level; m_painter->restore(); } private: + void verifyPainter() + { + Q_ASSERT(m_painter); + } + QPainter *m_painter; int m_level = 0; };