diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-03-01 11:44:28 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-03-04 05:50:21 +0000 |
commit | 8ba766c086d6ea980b5208b18af3b63a6fed7015 (patch) | |
tree | 4028276dd71c69af83bb02fa06f9f2df369508ee /src/corelib/text/qstring.cpp | |
parent | bbdbe26cadd98a9bada606029d2f0c20e73233b5 (diff) |
Don't access moved-from object
Clang's static analyzer tooling warns about this access-after-move [1].
While the comment above the function indicated that this was deliberate
and relying on a moved-from QString being valid, it is still bad
practice.
Since 'str' is empty in moved-from state if - and only if - it was a
non-const reference, make a compile-time check of the constness of type
T instead.
[1] https://testresults.qt.io/codechecker/daily_analyses/qtbase/dev/qtbase-dev-20210301-e14ccf0553/qstring.cpp_clang-tidy_b0545db57a2cc5dac67a56f76322ffd0.plist.html#reportHash=209ee3db0b17d21919326a1ad6635318
Change-Id: Iac1813b61b6a3c2ef4053b911a4043c5382f85e4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
(cherry picked from commit 49113c905d5868e6b76bb6b7b3e0a20b0c56a23a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/corelib/text/qstring.cpp')
-rw-r--r-- | src/corelib/text/qstring.cpp | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index da212ac291..fcdf687b99 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -6342,9 +6342,9 @@ namespace QUnicodeTables { \endlist In copy-convert mode, the local variable \c{s} is detached from the input - \a str. In the in-place convert mode, \a str is in moved-from state (which - this function requires to be a valid, empty string) and \c{s} contains the - only copy of the string, without reallocation (thus, \a it is still valid). + \a str. In the in-place convert mode, \a str is in moved-from state and + \c{s} contains the only copy of the string, without reallocation (thus, + \a it is still valid). There is one pathological case left: when the in-place conversion needs to reallocate memory to grow the buffer. In that case, we need to adjust the \a @@ -6355,7 +6355,7 @@ Q_NEVER_INLINE static QString detachAndConvertCase(T &str, QStringIterator it, QUnicodeTables::Case which) { Q_ASSERT(!str.isEmpty()); - QString s = std::move(str); // will copy if T is const QString + QString s = std::move(str); // will copy if T is const QString QChar *pp = s.begin() + it.index(); // will detach if necessary do { @@ -6374,9 +6374,8 @@ static QString detachAndConvertCase(T &str, QStringIterator it, QUnicodeTables:: s.replace(outpos, 1, reinterpret_cast<const QChar *>(folded.data()), folded.size()); pp = const_cast<QChar *>(s.constBegin()) + outpos + folded.size(); - // do we need to adjust the input iterator too? - // if it is pointing to s's data, str is empty - if (str.isEmpty()) + // Adjust the input iterator if we are performing an in-place conversion + if constexpr (!std::is_const<T>::value) it = QStringIterator(s.constBegin(), inpos + folded.size(), s.constEnd()); } } else { |