summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-24 11:45:23 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-25 23:09:09 +0000
commit049d8892eaa18d71d6edb10752418ad33305f310 (patch)
tree3cb104959f45385919d3fd79e83b739188502b56 /src/corelib/text
parentacbf9a858b6b389103b7f43f4f4892a142ec56c6 (diff)
QRegularExpression: do not assume QStringViews are NUL terminated
The convenience API used to look up the index of a named capturing group expects NUL terminated strings. Therefore, we can't just use it together with QStringViews, which may be not. Use the non-convenience API instead. Pick-to: 5.15 Change-Id: I25ca14de49b13ee1764525f8b19f2550c30c1afa Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qregularexpression.cpp21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp
index c52a80e676..a0b35f8142 100644
--- a/src/corelib/text/qregularexpression.cpp
+++ b/src/corelib/text/qregularexpression.cpp
@@ -1040,9 +1040,24 @@ int QRegularExpressionPrivate::captureIndexForName(QStringView name) const
if (!compiledPattern)
return -1;
- int index = pcre2_substring_number_from_name_16(compiledPattern, reinterpret_cast<PCRE2_SPTR16>(name.utf16()));
- if (index >= 0)
- return index;
+ // See the other usages of pcre2_pattern_info_16 for more details about this
+ PCRE2_SPTR16 *namedCapturingTable;
+ unsigned int namedCapturingTableEntryCount;
+ unsigned int namedCapturingTableEntrySize;
+
+ pcre2_pattern_info_16(compiledPattern, PCRE2_INFO_NAMETABLE, &namedCapturingTable);
+ pcre2_pattern_info_16(compiledPattern, PCRE2_INFO_NAMECOUNT, &namedCapturingTableEntryCount);
+ pcre2_pattern_info_16(compiledPattern, PCRE2_INFO_NAMEENTRYSIZE, &namedCapturingTableEntrySize);
+
+ for (unsigned int i = 0; i < namedCapturingTableEntryCount; ++i) {
+ const auto currentNamedCapturingTableRow =
+ reinterpret_cast<const char16_t *>(namedCapturingTable) + namedCapturingTableEntrySize * i;
+
+ if (name == (currentNamedCapturingTableRow + 1)) {
+ const int index = *currentNamedCapturingTableRow;
+ return index;
+ }
+ }
return -1;
}