Go to file
Giuseppe D'Angelo 6f852935e0 QString: fix lifetime issues with QRegularExpression APIs
QString has several functions taking a QRegularExpression: indexOf(),
contains(), and so on. Some of those have an out-argument of type
QRegularExpressionMatch, to report the details of the match (if any).
For instance:

  QRegularExpression re(...);
  QRegularExpressionMatch match;

  if (string.contains(re, &match))
    use(match);

The code used to route the implementation of these functions through
QStringView (which has the very same functions). This however opens
up a lifetime problem with temporary strings:

  if (getString().contains(re, &match))
    use(match); // match is dangling

Here `match` is dangling because it is referencing data into the
destroyed temporary -- nothing is keeping the string alive. This is
against the rules we've decided for Qt, and it's also asymmetric with
the corresponding code that uses QRegularExpression directly instead:

  match = re.match(getString());
  if (match.hasMatch())
    use(match); // not dangling

... although we've documented not to do this. (In light of the decision
we've made w.r.t. temporaries, the documentation is wrong anyways.)
Here QRE takes a copy of the string and stores it in the match object,
thus keeping it alive.

Hence, extend the implementation of the QString functions to keep a
(shallow) copy of the string. To keep the code shared as much as
possible with QStringView, in theory one could have a function taking a
std::variant<QString, QStringView> and that uses the currently active
member. However I've found that std::variant here creates some abysmal
codegen, so instead I went for a simpler approach -- pass a QStringView
and an optional pointer to a QString. Use the latter if it's loaded.

QStringView has some inline code that calls into exported functions, so
I can't change the signature of them without breaking BC; I'm instead
adding new unexported functions and a Qt 7 note to unify them.

Change-Id: I7c65885a84069d0fbb902dcc96ddff543ca84562
Fixes: QTBUG-103940
Pick-to: 6.2 6.3 6.4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2022-07-10 23:40:24 +02:00
.github/workflows Repair github action workflow 2021-11-10 18:57:33 +01:00
bin Minor fixes for qt-configure-module 2022-06-27 17:55:19 +02:00
cmake CMake: Don't create duplicate plugin targets in shared builds 2022-07-08 15:07:38 +02:00
coin Adjust documentation build dir for out of source builds 2022-06-27 11:05:23 +00:00
config.tests x86/RDSEED: Work around QNX compiler missing the rdseed intrinsic 2022-07-06 03:20:38 -07:00
dist Add Qt 6.0.0 changes file 2020-11-16 10:02:08 +02:00
doc Doc: Fix online macros for commercial template 2022-06-21 22:08:34 +00:00
examples CMake: Let qt6_add_plugin accept sources 2022-06-29 18:32:05 +02:00
lib Purge all fonts 2015-08-18 19:59:14 +00:00
libexec Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
LICENSES Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
mkspecs Disable bitcode for iOS 2022-07-07 17:02:37 +02:00
qmake Doc: Clarify qmake's c++latest CONFIG value 2022-06-29 18:32:05 +02:00
src QString: fix lifetime issues with QRegularExpression APIs 2022-07-10 23:40:24 +02:00
tests QString: fix lifetime issues with QRegularExpression APIs 2022-07-10 23:40:24 +02:00
util CMake: Rewrite double-conversion find module 2022-07-02 00:11:12 +02:00
.cmake.conf Bump version to 6.5.0 2022-06-06 13:23:59 +03:00
.gitattributes Give batch files CRLF line endings 2020-11-04 15:02:29 +00:00
.gitignore Assume qhelpgenerator in libexec instead of bin 2021-11-08 19:27:32 +01:00
.lgtm.yml Skip LGTM analysis for the bootstrap library and tools 2020-07-16 01:04:34 +02:00
.tag Update the git-archive export options 2012-09-07 15:39:31 +02:00
CMakeLists.txt Use REALPATH based check for symbolic links on Windows 2022-01-07 14:31:38 +01:00
conanfile.py Conan: Do not force 'qt_host_path' usage in cross-build context 2022-06-22 18:18:42 +03:00
config_help.txt CMake: update the x86 intrinsic checks 2022-06-28 03:28:42 +00:00
configure Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
configure.bat Use SPDX license identifiers 2022-05-16 16:37:38 +02:00
configure.cmake rcc: teach qmake not to use zstd when not available 2022-07-06 19:41:31 +03:00
dependencies.yaml Re-add dependencies.yaml now that qt5.git wip/qt6 builds fine 2019-09-18 13:19:31 +02:00
qt_cmdline.cmake C++23/c++2b support 2022-06-08 19:43:34 +02:00
sync.profile Remove deprecated qgl.h from sync.profile 2022-06-11 00:42:13 +02:00