summaryrefslogtreecommitdiffstats
path: root/src/network/kernel
diff options
context:
space:
mode:
authorDimitrios Apostolou <jimis@qt.io>2020-05-19 19:47:10 +0200
committerDimitrios Apostolou <jimis@qt.io>2020-05-28 10:48:58 +0200
commitbc58e0dc7a378d96472441757250586b71868e62 (patch)
treef713c485fe845f174982c240286190e98db9c89b /src/network/kernel
parentd4d08e009d595ccd308d4390797b031f4296cfee (diff)
Address Coverity defect about buffer overrun
Coverity warned that chunk could be >= tldChunkCount (2), and tldData[chunk] (array of length 2) would be accessed out of bounds. This can not happen, but it was unclear. Clarify logic with comments and asserts, that Coverity will hopefully understand now. Change-Id: I2a38c685cfcbc69ed123918e8cbed360b20b1035 Coverity-Id: 178254 Pick-to: 5.15 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/network/kernel')
-rw-r--r--src/network/kernel/qtldurl.cpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/network/kernel/qtldurl.cpp b/src/network/kernel/qtldurl.cpp
index cab10a8b1c..c4bc312229 100644
--- a/src/network/kernel/qtldurl.cpp
+++ b/src/network/kernel/qtldurl.cpp
@@ -58,6 +58,8 @@ enum TLDMatchType {
ExceptionMatch,
};
+// Scan the auto-generated table of TLDs for an entry. For more details
+// see comments in file: util/corelib/qurl-generateTLDs/main.cpp
static bool containsTLDEntry(QStringView entry, TLDMatchType match)
{
const QStringView matchSymbols[] = {
@@ -66,18 +68,36 @@ static bool containsTLDEntry(QStringView entry, TLDMatchType match)
u"!",
};
const auto symbol = matchSymbols[match];
- int index = qt_hash(entry, qt_hash(symbol)) % tldCount;
+ const int index = qt_hash(entry, qt_hash(symbol)) % tldCount;
// select the right chunk from the big table
short chunk = 0;
uint chunkIndex = tldIndices[index], offset = 0;
- while (chunk < tldChunkCount && tldIndices[index] >= tldChunks[chunk]) {
+
+ // The offset in the big string, of the group that our entry hashes into.
+ const auto tldGroupOffset = tldIndices[index];
+
+ // It should always be inside all chunks' total size.
+ Q_ASSERT(tldGroupOffset < tldChunks[tldChunkCount - 1]);
+ // All offsets are stored in non-decreasing order.
+ // This check is within bounds as tldIndices has length tldCount+1.
+ Q_ASSERT(tldGroupOffset <= tldIndices[index + 1]);
+ // The last extra entry in tldIndices
+ // should be equal to the total of all chunks' lengths.
+ Q_ASSERT(tldIndices[tldCount] == tldChunks[tldChunkCount - 1]);
+
+ // Find which chunk contains the tldGroupOffset
+ while (tldGroupOffset >= tldChunks[chunk]) {
chunkIndex -= tldChunks[chunk];
offset += tldChunks[chunk];
chunk++;
+
+ // We can not go above the number of chunks we have, since all our
+ // indices are less than the total chunks' size (see asserts above).
+ Q_ASSERT(chunk < tldChunkCount);
}
- // check all the entries from the given index
+ // check all the entries from the given offset
while (chunkIndex < tldIndices[index+1] - offset) {
const auto utf8 = tldData[chunk] + chunkIndex;
if ((symbol.isEmpty() || QLatin1Char(*utf8) == symbol) && entry == QString::fromUtf8(utf8 + symbol.size()))