From 12b826375706155eb61bb66673433878a3578718 Mon Sep 17 00:00:00 2001 From: Alexander Golubev Date: Fri, 29 Mar 2024 18:14:14 +0300 Subject: [PATCH] TQTextCodec: avoid dangling pointer to current locale's codec This fixes crashes cause by dangling pointer returned from TQTextCodec::codecForLocale(). That might happen when tqWarning() and such are called during final cleanup. Bug: https://mirror.git.trinitydesktop.org/gitea/TDE/tqt3/issues/142 Signed-off-by: Alexander Golubev --- src/codecs/qtextcodec.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/codecs/qtextcodec.cpp b/src/codecs/qtextcodec.cpp index 1c6df695..2e26320a 100644 --- a/src/codecs/qtextcodec.cpp +++ b/src/codecs/qtextcodec.cpp @@ -90,12 +90,14 @@ static TQValueList *all = 0; static bool destroying_is_ok; // starts out as 0 +static bool codecs_were_cleansed = 0; static TQTextCodec * localeMapper = 0; class TQTextCodecCleanup { public: ~TQTextCodecCleanup() { TQTextCodec::deleteAllCodecs(); + codecs_were_cleansed = TRUE; } }; static TQTextCodecCleanup qtextcodec_cleanup; @@ -140,6 +142,8 @@ void TQTextCodec::deleteAllCodecs() ball->clear(); delete ball; + localeMapper = 0; + destroying_is_ok = FALSE; } @@ -821,7 +825,12 @@ TQTextCodec* TQTextCodec::codecForLocale() if ( localeMapper ) return localeMapper; - setup(); + // codecForLocale() is used by TQString::locale8Bit(), which is used by tqWarning() (and such) + // which should be fine to call during final cleanup and in such case we don't want neither to + // recreate the codecs again nor complains about codecs being already destroyed. + if ( !codecs_were_cleansed ) { + setup(); + } return localeMapper; } @@ -2906,6 +2915,8 @@ static void realSetup() #if defined(QT_CHECK_STATE) if ( destroying_is_ok ) tqWarning( "TQTextCodec: creating new codec during codec cleanup!" ); + if ( codecs_were_cleansed ) + tqWarning( "TQTextCodec: trying to setup codecs after they already been cleansed!" ); #endif all = new TQValueList;