summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@digia.com>2014-10-09 09:43:23 +0200
committerFriedemann Kleint <Friedemann.Kleint@digia.com>2014-10-17 14:55:45 +0200
commit0368b24a7f53c945b582097cc8ad22a053856d22 (patch)
treebca43e19e6cab7d18f3e016b9b4ada923daf8d31
parent1b8c5f450ba7dc3be04149020ec788a8ca03e47f (diff)
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>
-rw-r--r--src/corelib/io/qsettings.cpp8
-rw-r--r--tests/auto/corelib/io/qsettings/tst_qsettings.cpp11
2 files changed, 19 insertions, 0 deletions
diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp
index fd35ae33dc..d896da176a 100644
--- a/src/corelib/io/qsettings.cpp
+++ b/src/corelib/io/qsettings.cpp
@@ -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);
diff --git a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp
index 77a60997a6..3e68e4859f 100644
--- a/tests/auto/corelib/io/qsettings/tst_qsettings.cpp
+++ b/tests/auto/corelib/io/qsettings/tst_qsettings.cpp
@@ -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);