diff --git a/src/libs/languageutils/componentversion.cpp b/src/libs/languageutils/componentversion.cpp index f041740d3b2..edfa2c552bb 100644 --- a/src/libs/languageutils/componentversion.cpp +++ b/src/libs/languageutils/componentversion.cpp @@ -6,8 +6,6 @@ #include #include -#include - namespace LanguageUtils { // QTC_TEMP @@ -42,8 +40,8 @@ QString ComponentVersion::toString() const void ComponentVersion::addToHash(QCryptographicHash &hash) const { - hash.addData(reinterpret_cast(&_major), sizeof(_major)); - hash.addData(reinterpret_cast(&_minor), sizeof(_minor)); + hash.addData(QByteArrayView(reinterpret_cast(&_major), sizeof(_major))); + hash.addData(QByteArrayView(reinterpret_cast(&_minor), sizeof(_minor))); } } // namespace LanguageUtils diff --git a/src/libs/languageutils/fakemetaobject.cpp b/src/libs/languageutils/fakemetaobject.cpp index 4c553cdc7b5..da7877c1b63 100644 --- a/src/libs/languageutils/fakemetaobject.cpp +++ b/src/libs/languageutils/fakemetaobject.cpp @@ -6,6 +6,21 @@ using namespace LanguageUtils; +static QByteArrayView asData(const void *mem, size_t len) +{ + return QByteArrayView(reinterpret_cast(mem), len); +} + +static void addData(QCryptographicHash &hash, int x) +{ + hash.addData(asData(&x, sizeof(int))); +} + +static void addData(QCryptographicHash &hash, const QString &x) +{ + hash.addData(asData(x.constData(), x.size() * sizeof(QChar))); +} + FakeMetaEnum::FakeMetaEnum() {} @@ -39,15 +54,12 @@ bool FakeMetaEnum::hasKey(const QString &key) const void FakeMetaEnum::addToHash(QCryptographicHash &hash) const { - int len = m_name.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_name.constData()), len * sizeof(QChar)); - len = m_keys.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); + addData(hash, m_name.size()); + addData(hash, m_name); + addData(hash, m_keys.size()); for (const QString &key : std::as_const(m_keys)) { - len = key.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(key.constData()), len * sizeof(QChar)); + addData(hash, key.size()); + addData(hash, key); } } @@ -121,29 +133,23 @@ void FakeMetaMethod::setRevision(int r) void FakeMetaMethod::addToHash(QCryptographicHash &hash) const { - int len = m_name.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_name.constData()), len * sizeof(QChar)); - hash.addData(reinterpret_cast(&m_methodAccess), sizeof(m_methodAccess)); - hash.addData(reinterpret_cast(&m_methodTy), sizeof(m_methodTy)); - hash.addData(reinterpret_cast(&m_revision), sizeof(m_revision)); - len = m_paramNames.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); + addData(hash, m_name.size()); + addData(hash, m_name); + addData(hash, m_methodAccess); + addData(hash, m_methodTy); + addData(hash, m_revision); + addData(hash, m_paramNames.size()); for (const QString &pName : std::as_const(m_paramNames)) { - len = pName.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(pName.constData()), len * sizeof(QChar)); + addData(hash, pName.size()); + addData(hash, pName); } - len = m_paramTypes.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); + addData(hash, m_paramTypes.size()); for (const QString &pType : std::as_const(m_paramTypes)) { - len = pType.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(pType.constData()), len * sizeof(QChar)); + addData(hash, pType.size()); + addData(hash, pType); } - len = m_returnType.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_returnType.constData()), len * sizeof(QChar)); + addData(hash, m_returnType.size()); + addData(hash, m_returnType); } QString FakeMetaMethod::describe(int baseIndent) const @@ -213,17 +219,15 @@ int FakeMetaProperty::revision() const void FakeMetaProperty::addToHash(QCryptographicHash &hash) const { - int len = m_propertyName.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_propertyName.constData()), len * sizeof(QChar)); - hash.addData(reinterpret_cast(&m_revision), sizeof(m_revision)); + addData(hash, m_propertyName.size()); + addData(hash, m_propertyName); + addData(hash, m_revision); int flags = (m_isList ? (1 << 0) : 0) + (m_isPointer ? (1 << 1) : 0) + (m_isWritable ? (1 << 2) : 0); - hash.addData(reinterpret_cast(&flags), sizeof(flags)); - len = m_type.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_type.constData()), len * sizeof(QChar)); + addData(hash, flags); + addData(hash, m_type.size()); + addData(hash, m_type); } QString FakeMetaProperty::describe(int baseIndent) const @@ -361,34 +365,34 @@ QByteArray FakeMetaObject::calculateFingerprint() const { QCryptographicHash hash(QCryptographicHash::Sha1); int len = m_className.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_className.constData()), len * sizeof(QChar)); + addData(hash, len); + addData(hash, m_className); len = m_attachedTypeName.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_attachedTypeName.constData()), len * sizeof(QChar)); + addData(hash, len); + addData(hash, m_attachedTypeName); len = m_defaultPropertyName.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_defaultPropertyName.constData()), len * sizeof(QChar)); + addData(hash, len); + addData(hash, m_defaultPropertyName); len = m_enumNameToIndex.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); + addData(hash, len); { QStringList keys(m_enumNameToIndex.keys()); keys.sort(); for (const QString &key : std::as_const(keys)) { len = key.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(key.constData()), len * sizeof(QChar)); + addData(hash, len); + addData(hash, key); int value = m_enumNameToIndex.value(key); - hash.addData(reinterpret_cast(&value), sizeof(value)); // avoid? this adds order dependency to fingerprint... + addData(hash, value); m_enums.at(value).addToHash(hash); } } len = m_exports.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); + addData(hash, len); for (const Export &e : std::as_const(m_exports)) e.addToHash(hash); // normalize order? len = m_exports.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); + addData(hash, len); for (const FakeMetaMethod &m : std::as_const(m_methods)) m.addToHash(hash); // normalize order? { @@ -396,16 +400,16 @@ QByteArray FakeMetaObject::calculateFingerprint() const keys.sort(); for (const QString &key : std::as_const(keys)) { len = key.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(key.constData()), len * sizeof(QChar)); + addData(hash, len); + addData(hash, key); int value = m_propNameToIdx.value(key); - hash.addData(reinterpret_cast(&value), sizeof(value)); // avoid? this adds order dependency to fingerprint... + addData(hash, value); m_props.at(value).addToHash(hash); } } len = m_superName.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(m_superName.constData()), len * sizeof(QChar)); + addData(hash, len); + addData(hash, m_superName); QByteArray res = hash.result(); res.append('F'); @@ -540,14 +544,12 @@ bool FakeMetaObject::Export::isValid() const void FakeMetaObject::Export::addToHash(QCryptographicHash &hash) const { - int len = package.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(package.constData()), len * sizeof(QChar)); - len = type.size(); - hash.addData(reinterpret_cast(&len), sizeof(len)); - hash.addData(reinterpret_cast(type.constData()), len * sizeof(QChar)); + addData(hash, package.size()); + addData(hash, package); + addData(hash, type.size()); + addData(hash, type); version.addToHash(hash); - hash.addData(reinterpret_cast(&metaObjectRevision), sizeof(metaObjectRevision)); + addData(hash, metaObjectRevision); } QString FakeMetaObject::Export::describe(int baseIndent) const diff --git a/src/libs/qmljs/qmljsimportdependencies.cpp b/src/libs/qmljs/qmljsimportdependencies.cpp index f1c4181a830..8e725fa521f 100644 --- a/src/libs/qmljs/qmljsimportdependencies.cpp +++ b/src/libs/qmljs/qmljsimportdependencies.cpp @@ -185,14 +185,14 @@ ImportKey::ImportKey(ImportType::Enum type, const QString &path, int majorVersio void ImportKey::addToHash(QCryptographicHash &hash) const { - hash.addData(reinterpret_cast(&type), sizeof(type)); - hash.addData(reinterpret_cast(&majorVersion), sizeof(majorVersion)); - hash.addData(reinterpret_cast(&minorVersion), sizeof(minorVersion)); + hash.addData(QByteArrayView(reinterpret_cast(&type), sizeof(type))); + hash.addData(QByteArrayView(reinterpret_cast(&majorVersion), sizeof(majorVersion))); + hash.addData(QByteArrayView(reinterpret_cast(&minorVersion), sizeof(minorVersion))); for (const QString &s : splitPath) { - hash.addData("/", 1); - hash.addData(reinterpret_cast(s.constData()), sizeof(QChar) * s.size()); + hash.addData("/"); + hash.addData(QByteArrayView(reinterpret_cast(s.constData()), sizeof(QChar) * s.size())); } - hash.addData("/", 1); + hash.addData("/"); } ImportKey ImportKey::flatKey() const { @@ -547,11 +547,11 @@ QByteArray DependencyInfo::calculateFingerprint(const ImportDependencies &deps) QStringList coreImports = Utils::toList(allCoreImports); coreImports.sort(); for (const QString &importId : std::as_const(coreImports)) { - hash.addData(reinterpret_cast(importId.constData()), importId.size() * sizeof(QChar)); + hash.addData(QByteArrayView(reinterpret_cast(importId.constData()), importId.size() * sizeof(QChar))); QByteArray coreImportFingerprint = deps.coreImport(importId).fingerprint; hash.addData(coreImportFingerprint); } - hash.addData("/", 1); + hash.addData("/"); QList imports = Utils::toList(allImports); std::sort(imports.begin(), imports.end()); for (const ImportKey &k : std::as_const(imports))