diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2022-06-18 12:09:52 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-06-22 13:03:17 -0700 |
commit | dda9c9e2bc4fd2efe9e3fb0e451a8c3512f9a4d2 (patch) | |
tree | d4521e507c36c0f8096e6915dd354eed8f119d91 /src/tools/moc | |
parent | c83a87eca7a435a6569e0185733982150093ee3a (diff) |
moc: fix use of escape sequence of more than one character
We had the code to calculate the length, but were improperly using it
only for the offset, not the length of the string or its containing
array. That resulted in the generated moc output containing:
QT_MOC_LITERAL(111, 5), // "\xffz"
QT_MOC_LITERAL(114, 5), // "\0012"
QT_MOC_LITERAL(117, 23), // "slotWithAReallyLongName"
The two strings are described as occupying 5 bytes (length 4 + null
terminator), which is incorrect. The offset was correct: 114 - 111 = 3
and 117 - 114 = 3. The new output is:
QT_MOC_LITERAL(111, 2), // "\xffz"
QT_MOC_LITERAL(114, 2), // "\0012"
QT_MOC_LITERAL(117, 23), // "slotWithAReallyLongName"
The effect of the array size calculation would only be felt if moc
decided it needed a second string array (for strings over 65535 bytes),
which would cause the offsets in the second array to be all wrong. There
was no such test until now.
Drive-by fixing of the newline, indentation, and the stale comment
referring to QByteArrayData (Qt 5).
Pick-to: 6.2 6.3 6.4
Change-Id: Id0fb9ab0089845ee8843fffd16f9cd01b3e0709a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/tools/moc')
-rw-r--r-- | src/tools/moc/generator.cpp | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index d03d8513db..b0a3ff37a4 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -93,6 +93,19 @@ static inline int lengthOfEscapeSequence(const QByteArray &s, int i) return i - startPos; } +static inline uint lengthOfEscapedString(const QByteArray &str) +{ + int extra = 0; + for (int j = 0; j < str.length(); ++j) { + if (str.at(j) == '\\') { + int cnt = lengthOfEscapeSequence(str, j) - 1; + extra += cnt; + j += cnt; + } + } + return str.length() - extra; +} + void Generator::strreg(const QByteArray &s) { if (!strings.contains(s)) @@ -225,7 +238,7 @@ void Generator::generateCode() int stringDataLength = 0; int stringDataCounter = 0; for (int i = 0; i < strings.size(); ++i) { - int thisLength = strings.at(i).length() + 1; + int thisLength = lengthOfEscapedString(strings.at(i)) + 1; stringDataLength += thisLength; if (stringDataLength / constCharArraySizeLimit) { // save previous stringdata and start computing the next one. @@ -238,35 +251,26 @@ void Generator::generateCode() } fprintf(out, "};\n"); - // Macro that expands into a QByteArrayData. The offset member is - // calculated from 1) the offset of the actual characters in the - // stringdata.stringdata member, and 2) the stringdata.data index of the - // QByteArrayData being defined. This calculation relies on the - // QByteArrayData::data() implementation returning simply "this + offset". + // Macro that simplifies the string data listing. The offset is calculated + // from the top of the stringdata object (i.e., past the uints). fprintf(out, "#define QT_MOC_LITERAL(ofs, len) \\\n" " uint(offsetof(qt_meta_stringdata_%s_t, stringdata0) + ofs), len \n", qualifiedClassNameIdentifier.constData()); fprintf(out, "static const qt_meta_stringdata_%s_t qt_meta_stringdata_%s = {\n", qualifiedClassNameIdentifier.constData(), qualifiedClassNameIdentifier.constData()); - fprintf(out, " {\n"); + fprintf(out, " {"); { int idx = 0; for (int i = 0; i < strings.size(); ++i) { const QByteArray &str = strings.at(i); - fprintf(out, "QT_MOC_LITERAL(%d, %d)", idx, int(str.length())); - if (i != strings.size() - 1) - fputc(',', out); const QByteArray comment = str.length() > 32 ? str.left(29) + "..." : str; - fprintf(out, " // \"%s\"\n", comment.size() ? comment.constData() : ""); - idx += str.length() + 1; - for (int j = 0; j < str.length(); ++j) { - if (str.at(j) == '\\') { - int cnt = lengthOfEscapeSequence(str, j) - 1; - idx -= cnt; - j += cnt; - } - } + const char *comma = (i != strings.size() - 1 ? "," : " "); + int len = lengthOfEscapedString(str); + fprintf(out, "\n QT_MOC_LITERAL(%d, %d)%s // \"%s\"", idx, len, comma, + comment.constData()); + + idx += len + 1; } fprintf(out, "\n },\n"); } |