From 74e7f5ccc94733fca936df4ff979f895227bfef0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 3 Aug 2022 17:45:29 +0200 Subject: [PATCH 1/2] Fix incorrect use of strncpy() in Scintilla DMIS lexer This strncpy() was copying a buffer to itself, justifiably resulting in a -Wrestrict from gcc-12 when using -O2. --- src/stc/scintilla/lexers/LexDMIS.cxx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/stc/scintilla/lexers/LexDMIS.cxx b/src/stc/scintilla/lexers/LexDMIS.cxx index 7eeecca0e5..0493a69cab 100644 --- a/src/stc/scintilla/lexers/LexDMIS.cxx +++ b/src/stc/scintilla/lexers/LexDMIS.cxx @@ -244,10 +244,10 @@ void SCI_METHOD LexerDMIS::Lex(Sci_PositionU startPos, Sci_Position lengthDoc, i case SCE_DMIS_KEYWORD: if (!setDMISWord.Contains(scCTX.ch)) { - char tmpStr[MAX_STR_LEN]; - memset(tmpStr, 0, MAX_STR_LEN*sizeof(char)); - scCTX.GetCurrent(tmpStr, (MAX_STR_LEN-1)); - strncpy(tmpStr, this->UpperCase(tmpStr), (MAX_STR_LEN-1)); + char tmpBuf[MAX_STR_LEN]; + memset(tmpBuf, 0, MAX_STR_LEN*sizeof(char)); + scCTX.GetCurrent(tmpBuf, (MAX_STR_LEN-1)); + char* const tmpStr = this->UpperCase(tmpBuf); if (this->m_minorWords.InList(tmpStr)) { scCTX.ChangeState(SCE_DMIS_MINORWORD); From 87836e0b976cc28076c249d3d8a89f6ba04e1649 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 3 Aug 2022 17:59:24 +0200 Subject: [PATCH 2/2] Avoid bogus -Wuse-after-free in gcc 12 optimized builds This seems to be https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104215 or at least similar to it, i.e. a known false positive that we unfortunately have to work around in this ugly way, by inserting a compiler-specific barrier. If we need such workarounds in other places, we should define a wxCOMPILE_BARRIER macro or something similar, but for now it seems to be only needed here. --- include/wx/vector.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/wx/vector.h b/include/wx/vector.h index 7b10147b6c..03fbb64519 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -519,6 +519,15 @@ public: const size_t idx = it - begin(); const size_t after = end() - it; + // Unfortunately gcc 12 still complains about use-after-free even + // though our code is correct because it actually optimizes it to be + // wrong, with -O2 or higher, by moving the assignment above below the + // call to reserve() below, so use this hack to avoid the warning with + // it by preventing it from rearranging the code. +#if wxCHECK_GCC_VERSION(12, 1) + __asm__ __volatile__("":::"memory"); +#endif + reserve(size() + count); // the place where the new element is going to be inserted