Skip to content

Commit

Permalink
QWinRegistryKey: fix assert when querying default value
Browse files Browse the repository at this point in the history
I wrongly assumed we can't query a value with an empty name ""
during the previous refactor commit, however, in Windows registry,
an empty name for a value means the default value of a key, we can
read and write it through the "Default" name.
Remove the wrong assert to fix the crash when we are trying to query
a default value of a key.

Add a new test case to test this kind of scenarios.

Amends commit 40523b6

Fixes: QTBUG-107794
Change-Id: Idacbcb86df4435a8c1ca1c19121599390ae8f3d3
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
  • Loading branch information
wangwenx190 committed Oct 21, 2022
1 parent 466a03e commit 738e05a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/corelib/kernel/qwinregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ void QWinRegistryKey::close()

QVariant QWinRegistryKey::value(QStringView subKey) const
{
Q_ASSERT(!subKey.isEmpty());

if (!isValid())
return {};

if (subKey.isEmpty())
subKey = u"Default";

auto subKeyC = reinterpret_cast<const wchar_t *>(subKey.utf16());

// Get the size and type of the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static const QPair<QStringView, quint32> TEST_DWORD = qMakePair(u"dword", 123);
static const QPair<QStringView, quint64> TEST_QWORD = qMakePair(u"qword", 456);
static const QPair<QStringView, QByteArray> TEST_BINARY = qMakePair(u"binary", "binary\0"_ba);
static const QPair<QStringView, QVariant> TEST_NOT_EXIST = qMakePair(u"not_exist", QVariant());
static const QPair<QStringView, QVariant> TEST_DEFAULT = qMakePair(u"Default", u"default"_s);

[[nodiscard]] static inline bool write(const HKEY key, const QStringView name, const QVariant &value)
{
Expand Down Expand Up @@ -124,6 +125,8 @@ void tst_qwinregistrykey::initTestCase()
return;
if (!write(key, TEST_BINARY.first, TEST_BINARY.second))
return;
if (!write(key, TEST_DEFAULT.first, TEST_DEFAULT.second))
return;
m_available = true;
}

Expand All @@ -141,9 +144,10 @@ void tst_qwinregistrykey::cleanupTestCase()
RegDeleteValueW(key, C_STR(TEST_DWORD.first));
RegDeleteValueW(key, C_STR(TEST_QWORD.first));
RegDeleteValueW(key, C_STR(TEST_BINARY.first));
RegDeleteValueW(key, C_STR(TEST_DEFAULT.first));
#undef C_STR
RegCloseKey(key);
RegDeleteKeyW(HKEY_CURRENT_USER, TEST_KEY);
RegCloseKey(key);
}

void tst_qwinregistrykey::qwinregistrykey()
Expand Down Expand Up @@ -206,6 +210,18 @@ void tst_qwinregistrykey::qwinregistrykey()
QCOMPARE(value.value_or(QVariant()), QVariant());
}

{
const auto value = registry.value<QString>(TEST_DEFAULT.first);
QVERIFY(value.has_value());
QCOMPARE(value.value_or(QString()), TEST_DEFAULT.second);
}

{
const auto value = registry.value<QString>(L"");
QVERIFY(value.has_value());
QCOMPARE(value.value_or(QString()), TEST_DEFAULT.second);
}

{
const QString value = registry.stringValue(TEST_STRING.first);
QVERIFY(!value.isEmpty());
Expand Down

0 comments on commit 738e05a

Please sign in to comment.