diff --git a/src/corelib/kernel/qwinregistry.cpp b/src/corelib/kernel/qwinregistry.cpp index fe0cd62bdd2..dc5252f9d19 100644 --- a/src/corelib/kernel/qwinregistry.cpp +++ b/src/corelib/kernel/qwinregistry.cpp @@ -38,13 +38,14 @@ void QWinRegistryKey::close() QVariant QWinRegistryKey::value(QStringView subKey) const { + // NOTE: Empty value name is allowed in Windows registry, it means the default + // or unnamed value of a key, you can read/write/delete such value normally. + if (!isValid()) return {}; - if (subKey.isEmpty()) - subKey = u"Default"; - - auto subKeyC = reinterpret_cast(subKey.utf16()); + // Use nullptr when we need to access the default value. + const auto subKeyC = subKey.isEmpty() ? nullptr : reinterpret_cast(subKey.utf16()); // Get the size and type of the value. DWORD dataType = REG_NONE; diff --git a/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp b/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp index 81f75a7beaa..897f638dc91 100644 --- a/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp +++ b/tests/auto/corelib/kernel/qwinregistrykey/tst_qwinregistrykey.cpp @@ -20,7 +20,7 @@ static const QPair TEST_DWORD = qMakePair(u"dword", 123); static const QPair TEST_QWORD = qMakePair(u"qword", 456); static const QPair TEST_BINARY = qMakePair(u"binary", "binary\0"_ba); static const QPair TEST_NOT_EXIST = qMakePair(u"not_exist", QVariant()); -static const QPair TEST_DEFAULT = qMakePair(u"Default", u"default"_s); +static const QPair TEST_DEFAULT = qMakePair(u"", u"default"_s); [[nodiscard]] static inline bool write(const HKEY key, const QStringView name, const QVariant &value) { @@ -216,12 +216,6 @@ void tst_qwinregistrykey::qwinregistrykey() QCOMPARE(value.value_or(QString()), TEST_DEFAULT.second); } - { - const auto value = registry.value(L""); - QVERIFY(value.has_value()); - QCOMPARE(value.value_or(QString()), TEST_DEFAULT.second); - } - { const QString value = registry.stringValue(TEST_STRING.first); QVERIFY(!value.isEmpty());