From 78cdd9a64dc0cd666e5c8daafa7477c29641420d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 6 Jun 2023 16:33:21 +0200 Subject: QPixmapCache: deprecate replace() The replace() implementation overwrites the passed Key key with a new version, const_cast'ing away the const from the key passed by reference-to-const. This is UB if the Key was originally declared const. Deprecate the function. Also inline the const_cast, so compilers can readily detect the UB even if users don't enable deprecation warnings. Due to the severity of the issue (UB), immediate deprecation is warranted. There appear to be no in-tree user of the API outside of tst_qpixmapcache.cpp. [ChangeLog][Deprecation Notice][QtGui][QPixmapCache] The `replace(key, pixmap)` function has been deprecated, because passing a `const Key` to it results in undefined behavior. Use `remove(key, pixmap)` followed by `key = insert(pixmap)` instead. Pick-to: 6.6 Change-Id: Ic5060ce3271f2a1b6dc561da8716b452a2355d4c Reviewed-by: Volker Hilsheimer --- src/gui/image/qpixmapcache.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/gui/image/qpixmapcache.h') diff --git a/src/gui/image/qpixmapcache.h b/src/gui/image/qpixmapcache.h index 433890c68f..55aad6503f 100644 --- a/src/gui/image/qpixmapcache.h +++ b/src/gui/image/qpixmapcache.h @@ -42,13 +42,30 @@ public: static bool find(const Key &key, QPixmap *pixmap); static bool insert(const QString &key, const QPixmap &pixmap); static Key insert(const QPixmap &pixmap); +#if QT_DEPRECATED_SINCE(6, 6) + QT_DEPRECATED_VERSION_X_6_6("Use remove(key), followed by key = insert(pixmap).") + QT_GUI_INLINE_SINCE(6, 6) static bool replace(const Key &key, const QPixmap &pixmap); +#endif static void remove(const QString &key); static void remove(const Key &key); static void clear(); }; Q_DECLARE_SHARED(QPixmapCache::Key) +#if QT_DEPRECATED_SINCE(6, 6) +#if QT_GUI_INLINE_IMPL_SINCE(6, 6) +bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap) +{ + if (!key.isValid()) + return false; + remove(key); + const_cast(key) = insert(pixmap); + return key.isValid(); +} +#endif // QT_GUI_INLINE_IMPL_SINCE(6, 6) +#endif // QT_DEPRECATED_SINCE(6, 6) + QT_END_NAMESPACE #endif // QPIXMAPCACHE_H -- cgit v1.2.3