QSettings: Prevent assert when passing empty keys.

[ChangeLog][Important behavior changes][QSettings]
QSettings::value() now returns an invalid QVariant
when passing an empty key. The code path ran into
an assert, which was only noticeable in debug
builds.

Task-number: QTBUG-41812
Change-Id: I5cc32be3aa267a132e9d6639ecd6cb0bbafc15b0
Reviewed-by: Stéphane Fabry, Cutesoft <stephane.fabry@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Friedemann Kleint 2014-10-09 09:43:23 +02:00
parent 1b8c5f450b
commit 0368b24a7f
2 changed files with 19 additions and 0 deletions

View File

@ -3121,6 +3121,10 @@ bool QSettings::isWritable() const
void QSettings::setValue(const QString &key, const QVariant &value)
{
Q_D(QSettings);
if (key.isEmpty()) {
qWarning("QSettings::setValue: Empty key passed");
return;
}
QString k = d->actualKey(key);
d->set(k, value);
d->requestUpdate();
@ -3253,6 +3257,10 @@ bool QSettings::event(QEvent *event)
QVariant QSettings::value(const QString &key, const QVariant &defaultValue) const
{
Q_D(const QSettings);
if (key.isEmpty()) {
qWarning("QSettings::value: Empty key passed");
return QVariant();
}
QVariant result = defaultValue;
QString k = d->actualKey(key);
d->get(k, &result);

View File

@ -118,6 +118,7 @@ private slots:
void testUpdateRequestEvent();
void testThreadSafety();
void testEmptyData();
void testEmptyKey();
void testResourceFiles();
void testRegistryShortRootNames();
void trailingWhitespace();
@ -2025,6 +2026,16 @@ void tst_QSettings::testEmptyData()
QFile::remove(filename);
}
void tst_QSettings::testEmptyKey()
{
QSettings settings;
QTest::ignoreMessage(QtWarningMsg, "QSettings::value: Empty key passed");
const QVariant value = settings.value(QString());
QCOMPARE(value, QVariant());
QTest::ignoreMessage(QtWarningMsg, "QSettings::setValue: Empty key passed");
settings.setValue(QString(), value);
}
void tst_QSettings::testResourceFiles()
{
QSettings settings(":/resourcefile.ini", QSettings::IniFormat);