diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-05-23 23:33:54 +0300 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-06-06 01:23:01 +0300 |
commit | 845b5d63bf3e4f224676d6f7499a46ff686874fe (patch) | |
tree | 039647a8e4b9d86cd165fa36eee5f360f25b8e3d | |
parent | 9cb08c4c0de1685551c18e3b7958a00afa2a4c6a (diff) |
Moc: assert size of registered strings list is within INT_MAX range
Assert generator.strings.size() < INT_MAX after all strings have been
registered.
Parts of the public API, e.g. QMetaMethod::methodIndex and similar
functions return int, and other parts of the code expect int values, at
least for Qt6 this can't be changed, so use qsizetype internally and
assert the values fit in an int.
Change-Id: Ib226e9c19a578bbeaeb9bb767d756a9569fe57b3
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/tools/moc/generator.cpp | 4 | ||||
-rw-r--r-- | src/tools/moc/generator.h | 2 | ||||
-rw-r--r-- | src/tools/moc/moc.cpp | 6 |
3 files changed, 10 insertions, 2 deletions
diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index 097d002142..625e1e6733 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -122,7 +122,7 @@ void Generator::strreg(const QByteArray &s) int Generator::stridx(const QByteArray &s) { - int i = strings.indexOf(s); + int i = int(strings.indexOf(s)); Q_ASSERT_X(i != -1, Q_FUNC_INFO, "We forgot to register some strings"); return i; } @@ -841,7 +841,7 @@ void Generator::generateProperties() int notifyId = p.notifyId; if (p.notifyId < -1) { // signal is in parent class - const int indexInStrings = strings.indexOf(p.notify); + const int indexInStrings = int(strings.indexOf(p.notify)); notifyId = indexInStrings | IsUnresolvedSignal; } fprintf(out, ", 0x%.8x, uint(%d), %d,\n", flags, notifyId, p.revision); diff --git a/src/tools/moc/generator.h b/src/tools/moc/generator.h index fa651f04a0..ac29557769 100644 --- a/src/tools/moc/generator.h +++ b/src/tools/moc/generator.h @@ -20,6 +20,8 @@ public: const QHash<QByteArray, QByteArray> &knownGadgets, FILE *outfile = nullptr, bool requireCompleteTypes = false); void generateCode(); + qsizetype registeredStringsCount() { return strings.size(); }; + private: bool registerableMetaType(const QByteArray &propertyType); void registerClassInfoStrings(); diff --git a/src/tools/moc/moc.cpp b/src/tools/moc/moc.cpp index 4a9e3463ac..d04f2d079e 100644 --- a/src/tools/moc/moc.cpp +++ b/src/tools/moc/moc.cpp @@ -1138,6 +1138,12 @@ void Moc::generate(FILE *out, FILE *jsonOutput) for (int i = 0; i < classList.size(); ++i) { Generator generator(&classList[i], metaTypes, knownQObjectClasses, knownGadgets, out, requireCompleteTypes); generator.generateCode(); + + // generator.generateCode() should have already registered all strings + if (Q_UNLIKELY(generator.registeredStringsCount() >= std::numeric_limits<int>::max())) { + error("internal limit exceeded: number of parsed strings is too big."); + exit(EXIT_FAILURE); + } } fputs("", out); |