From 37750aa8d158d44689e768ae3926dc842730dfcd Mon Sep 17 00:00:00 2001 From: Michael Weghorn Date: Fri, 10 Nov 2023 15:29:53 +0100 Subject: [PATCH] Don't reuse iterator var to avoid -D_GLIBCXX_DEBUG crash For a CXXFLAGS='-D_GLIBCXX_DEBUG' build, running the examples/corelib/ipc/sharedmemory/sharedmemory example and clicking on the "Load Image from File..." button would result in a crash: > usr/include/c++/13/debug/safe_iterator.h:492: > In function: > bool gnu_debug::operator!=(const > _Safe_iterator QSettingsKey, QVariant> >, std::debug::map std::less, std::allocator QVariant> > >, std::forward_iterator_tag>::_Self&, const > _Safe_iterator QSettingsKey, QVariant> >, std::debug::map std::less, std::allocator QVariant> > >, std::forward_iterator_tag>::_Self&) > > Error: attempt to compare a singular iterator to a > singular (value-initialized) iterator. > > Objects involved in the operation: > iterator "lhs" @ 0x7ffe8e811550 { > type = std::_Rb_tree_const_iterator > (constant iterator); > state = singular; > } > iterator "rhs" @ 0x7ffe8e811670 { > type = std::_Rb_tree_const_iterator > (constant iterator); > state = singular (value-initialized); > } > Aborted (core dumped) This may be a libstdc++ bug, but still avoid/work around the issue by just using two separate variables for the iterators here. While at it, simplify the code a bit and replace the use of const_cast and pointers with the use of const references. Many thanks to Giuseppe D'Angelo for the analysis of the underlying problem and reporting a bug for GCC/libstdc++ [1] ! [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112477 Fixes: QTBUG-119044 Pick-to: 6.5 Change-Id: I00a8cc35033cf3ab4ba1f071cccabdef8ef52f9c Reviewed-by: Giuseppe D'Angelo (cherry picked from commit 5db48d584e626a15bf436a56aa2165dfe9dfde1d) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qsettings.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index 6bd9a2c819b..d8d1419a517 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -1244,17 +1244,17 @@ QStringList QConfFileSettingsPrivate::children(const QString &prefix, ChildSpec else ensureSectionParsed(confFile, thePrefix); - auto j = const_cast( - &confFile->originalKeys)->lowerBound( thePrefix); - while (j != confFile->originalKeys.constEnd() && j.key().startsWith(thePrefix)) { - if (!confFile->removedKeys.contains(j.key())) - processChild(QStringView{j.key().originalCaseKey()}.sliced(startPos), spec, result); - ++j; + const auto &originalKeys = confFile->originalKeys; + auto i = originalKeys.lowerBound(thePrefix); + while (i != originalKeys.end() && i.key().startsWith(thePrefix)) { + if (!confFile->removedKeys.contains(i.key())) + processChild(QStringView{i.key().originalCaseKey()}.sliced(startPos), spec, result); + ++i; } - j = const_cast( - &confFile->addedKeys)->lowerBound(thePrefix); - while (j != confFile->addedKeys.constEnd() && j.key().startsWith(thePrefix)) { + const auto &addedKeys = confFile->addedKeys; + auto j = addedKeys.lowerBound(thePrefix); + while (j != addedKeys.end() && j.key().startsWith(thePrefix)) { processChild(QStringView{j.key().originalCaseKey()}.sliced(startPos), spec, result); ++j; }