From 57960ab075b9e7a471c42ddea6a2abbc2994ec83 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sun, 12 Sep 2021 20:07:40 -0700 Subject: Q{Elf,Mach}Parser: simplify the return codes The multi-state return code was a legacy of how Arvid wrote the ELF parser code back in the day, the fact that it scanned for two different types of plugins in Qt 4 and that the metadata could exist in different places. None of that matters nowadays: who cares if the file is a corrupt binary, not a valid binary, does not have the right architecture, or has no suitable section? It's not a plugin, period. The Qt 4 plugin mechanism was removed for Qt 5.0 in commit 7443895857fdaee132c8efc643e471f02b3d0fa4 ("Remove support for Qt 4 style plugins"). Change-Id: I42eb903a916645db9900fffd16a442d800399b98 Reviewed-by: Lars Knoll Reviewed-by: Oswald Buddenhagen --- src/corelib/plugin/qlibrary.cpp | 56 ++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) (limited to 'src/corelib/plugin/qlibrary.cpp') diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp index 2c5940f8f2..cfc7479d37 100644 --- a/src/corelib/plugin/qlibrary.cpp +++ b/src/corelib/plugin/qlibrary.cpp @@ -254,8 +254,10 @@ static bool findPatternUnloaded(const QString &library, QLibraryPrivate *lib) constexpr qint64 MaxMemoryMapSize = Q_INT64_C(1) << (sizeof(qsizetype) > 4 ? 40 : 29); - qsizetype fdlen = qMin(file.size(), MaxMemoryMapSize); - const char *filedata = reinterpret_cast(file.map(0, fdlen)); + QLibraryScanResult r; + r.pos = 0; + r.length = qMin(file.size(), MaxMemoryMapSize); + const char *filedata = reinterpret_cast(file.map(0, r.length)); #ifdef Q_OS_UNIX if (filedata == nullptr) { @@ -274,65 +276,49 @@ static bool findPatternUnloaded(const QString &library, QLibraryPrivate *lib) // the side of doing a regular read into memory (up to 64 MB). data = file.read(64 * 1024 * 1024); filedata = data.constData(); - fdlen = data.size(); + r.length = data.size(); } #endif /* - ELF and Mach-O binaries with GCC have .qplugin sections. + ELF and Mach-O binaries with GCC have .qtmetadata sections. Find them. */ bool hasMetaData = false; - qsizetype pos = 0; char pattern[] = "qTMETADATA "; pattern[0] = 'Q'; // Ensure the pattern "QTMETADATA" is not found in this library should QPluginLoader ever encounter it. const ulong plen = ulong(qstrlen(pattern)); #if defined (Q_OF_ELF) - QElfParser::ScanResult r = QElfParser().parse(filedata, fdlen, library, lib, &pos, &fdlen); - if (r == QElfParser::Corrupt || r == QElfParser::NotElf) { - if (lib && qt_debug_component()) { - qWarning("QElfParser: %ls", qUtf16Printable(lib->errorString)); - } - return false; - } else if (r == QElfParser::QtMetaDataSection) { - qsizetype rel = qt_find_pattern(filedata + pos, fdlen, pattern, plen); - if (rel < 0) - pos = -1; - else - pos += rel; - hasMetaData = true; + r = QElfParser().parse(filedata, r.length, library, lib); + if (r.length == 0) { + if (lib && qt_debug_component()) + qWarning("QElfParser: %ls", qUtf16Printable(lib->errorString)); + return false; } #elif defined(Q_OF_MACH_O) { QString errorString; - int r = QMachOParser::parse(filedata, fdlen, library, &errorString, &pos, &fdlen); - if (r == QMachOParser::NotSuitable) { + r = QMachOParser::parse(filedata, r.length, library, &errorString); + if (r.length == 0) { if (qt_debug_component()) qWarning("QMachOParser: %ls", qUtf16Printable(errorString)); if (lib) lib->errorString = errorString; return false; } - // even if the metadata section was not found, the Mach-O parser will - // at least return the boundaries of the right architecture - qsizetype rel = qt_find_pattern(filedata + pos, fdlen, pattern, plen); - if (rel < 0) - pos = -1; - else - pos += rel; - hasMetaData = true; } -#else - pos = qt_find_pattern(filedata, fdlen, pattern, plen); - if (pos > 0) - hasMetaData = true; #endif // defined(Q_OF_ELF) && defined(Q_CC_GNU) + if (qsizetype rel = qt_find_pattern(filedata + r.pos, r.length, pattern, plen); + rel >= 0) { + r.pos += rel; + hasMetaData = true; + } bool ret = false; - if (pos >= 0 && hasMetaData) { - const char *data = filedata + pos; + if (r.pos >= 0 && hasMetaData) { + const char *data = filedata + r.pos; QString errMsg; - QJsonDocument doc = qJsonFromRawLibraryMetaData(data, fdlen, &errMsg); + QJsonDocument doc = qJsonFromRawLibraryMetaData(data, r.length, &errMsg); if (doc.isNull()) { qWarning("Found invalid metadata in lib %ls: %ls", qUtf16Printable(library), qUtf16Printable(errMsg)); -- cgit v1.2.3