https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121593
Bug ID: 121593 Summary: Miscompiles small function to produce endless(?) loop Product: gcc Version: 15.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: marc.mutz at hotmail dot com Target Milestone: --- Created attachment 62143 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62143&action=edit Preprocessed source I'm refactoring a small piece of code into an inline function (template) and the test suite shows that the function no longer terminates (within the allocated 5min watchdog time; didn't try to see whether it eventually terminates or not). This is reproducible on Ubuntu 20.04 with self-compiled GCC 11.5 (also SLES with that GCC version on the Qt CI fails the same way), 13.4.1 (20250611), 14.3.1 (20250611) and 15.1.1 (20250611). Clang is fine, Valgrind is silent, and if I switch on asan/ubsan, the test passes, too. Ditto if I put some more complex code into the calling function, like a qDebug() (like printf) or even the increment of a `thread_local int count`. Another fix is to mark the extracted functions as __attribute__((optimize("O0"))) (O1 already fails). The extracted function is QUtf8Functions::nextCharacterFromUtf8(), the caller is `NextResult operator()(const char *&it, const char *end) const`. I'm attaching the preprocessed source for the old version (.inline.ii) as well as the diff to the preprocessed source with the port to the extracted function (.guarded.ii). Unfortunately, the test code is in a different TU (library, even), so I hope that looking at the assembly will already help. If you remove the __attribute__((optimize("O0"))) from nextCharacterFromUtf8(), then the test fails. With both versions of preprocessed source, the test passes. Tell me how I can help to narrow it down further (my assembly is very bad, though). Compile command for the preprocessed file are: ``` c++ -DBACKTRACE_HEADER=\"execinfo.h\" -DCore_EXPORTS -DELF_INTERPRETER=\"/lib64/ld-linux-x86-64.so.2\" -DQT_ASCII_CAST_WARNINGS -DQT_BUILDING_QT -DQT_BUILD_CORE_LIB -DQT_DEPRECATED_WARNINGS -DQT_EXPLICIT_QFILE_CONSTRUCTION_FROM_PATH -DQT_LEAN_HEADERS=1 -DQT_MOC_COMPAT -DQT_NO_CAST_TO_ASCII -DQT_NO_CONTEXTLESS_CONNECT -DQT_NO_DEBUG -DQT_NO_FOREACH -DQT_NO_JAVA_STYLE_ITERATORS -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT -DQT_NO_QASCONST -DQT_NO_QEXCHANGE -DQT_NO_QPAIR -DQT_NO_QSNPRINTF -DQT_NO_STD_FORMAT_SUPPORT -DQT_NO_USING_NAMESPACE -DQT_STRICT_QLIST_ITERATORS -DQT_TYPESAFE_FLAGS -DQT_USE_NODISCARD_FILE_OPEN -DQT_USE_QSTRINGBUILDER -D_GLIBCXX_ASSERTIONS -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -I/home/marc/Qt/qtbase-submit-build/src/corelib/Core_autogen/include -I/home/marc/Qt/qtbase-submit-build/include -I/home/marc/Qt/qtbase-submit-build/include/QtCore -I/home/marc/Qt/qtbase-submit/src/corelib -I/home/marc/Qt/qtbase-submit-build/src/corelib -I/home/marc/Qt/qtbase-submit-build/src/corelib/global -I/home/marc/Qt/qtbase-submit-build/src/corelib/kernel -I/home/marc/Qt/qtbase-submit/src/corelib/../3rdparty/tinycbor/src -I/home/marc/Qt/qtbase-submit-build/include/QtCore/6.11.0 -I/home/marc/Qt/qtbase-submit-build/include/QtCore/6.11.0/QtCore -I/home/marc/Qt/qtbase-submit/src/corelib/../3rdparty/forkfd -I/home/marc/Qt/qtbase-submit-build/src/corelib/.rcc -I/home/marc/Qt/qtbase-submit-build/mkspecs/linux-g++ -isystem /d/openssl/32-dev/include -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -g -DNDEBUG -O3 -std=gnu++17 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -Wall -Wextra -Wno-expansion-to-defined -U_FORTIFY_SOURCE -Wsuggest-override -fcf-protection=full -D_FORTIFY_SOURCE=2 -ftrivial-auto-var-init=pattern -fstack-protector-strong -fstack-clash-protection -fexceptions -MD -MT src/corelib/CMakeFiles/Core.dir/serialization/qxmlstream.cpp.o -MF src/corelib/CMakeFiles/Core.dir/serialization/qxmlstream.cpp.o.d -o qxmlstream.cpp.inlined.ii -E /home/marc/Qt/qtbase-submit/src/corelib/serialization/qxmlstream.cpp ```