more fixes for QTBUG-17868
authorCharles Yin <charles.yin@nokia.com>
Tue, 6 Sep 2011 03:54:46 +0000 (13:54 +1000)
committerQt by Nokia <qt-info@nokia.com>
Tue, 6 Sep 2011 08:31:39 +0000 (10:31 +0200)
QDeclarativePropertyCache bypasses the previous fix, need to check the
property name when property cache try to create property.

Change-Id: Ibccc02df568a8ded3626bb914150ba9ee87d238f
Task-number:QTBUG-17868
Reviewed-on: http://codereview.qt.nokia.com/4230
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
src/declarative/util/qdeclarativepropertymap.cpp
tests/auto/declarative/qdeclarativepropertymap/tst_qdeclarativepropertymap.cpp

index c924267..fc009a8 100644 (file)
@@ -58,7 +58,7 @@ public:
 protected:
     virtual void propertyWritten(int index);
     virtual void propertyCreated(int, QMetaPropertyBuilder &);
-
+    virtual int createProperty(const char *, const char *);
 private:
     QDeclarativePropertyMap *map;
     QDeclarativePropertyMapPrivate *priv;
@@ -71,8 +71,19 @@ public:
     QDeclarativePropertyMapMetaObject *mo;
     QStringList keys;
     void emitChanged(const QString &key, const QVariant &value);
+    bool validKeyName(const QString& name);
 };
 
+bool QDeclarativePropertyMapPrivate::validKeyName(const QString& name)
+{
+    //The following strings shouldn't be used as property names
+    return  name != QLatin1String("keys")
+         && name != QLatin1String("valueChanged")
+         && name != QLatin1String("QObject")
+         && name != QLatin1String("destroyed")
+         && name != QLatin1String("deleteLater");
+}
+
 void QDeclarativePropertyMapPrivate::emitChanged(const QString &key, const QVariant &value)
 {
     Q_Q(QDeclarativePropertyMap);
@@ -95,6 +106,13 @@ void QDeclarativePropertyMapMetaObject::propertyCreated(int, QMetaPropertyBuilde
     priv->keys.append(QString::fromUtf8(b.name()));
 }
 
+int QDeclarativePropertyMapMetaObject::createProperty(const char *name, const char *value)
+{
+    if (!priv->validKeyName(name))
+        return -1;
+    return QDeclarativeOpenMetaObject::createProperty(name, value);
+}
+
 /*!
     \class QDeclarativePropertyMap
     \brief The QDeclarativePropertyMap class allows you to set key-value pairs that can be used in QML bindings.
@@ -181,12 +199,8 @@ QVariant QDeclarativePropertyMap::value(const QString &key) const
 void QDeclarativePropertyMap::insert(const QString &key, const QVariant &value)
 {
     Q_D(QDeclarativePropertyMap);
-    //The following strings shouldn't be used as property names
-    if (key != QLatin1String("keys")
-     && key != QLatin1String("valueChanged")
-     && key != QLatin1String("QObject")
-     && key != QLatin1String("destroyed")
-     && key != QLatin1String("deleteLater")) {
+
+    if (d->validKeyName(key)) {
         d->mo->setValue(key.toUtf8(), value);
     } else {
         qWarning() << "Creating property with name"
index 5583701..966ac19 100644 (file)
@@ -61,6 +61,7 @@ private slots:
     void count();
 
     void crashBug();
+    void QTBUG_17868();
 };
 
 void tst_QDeclarativePropertyMap::insert()
@@ -218,6 +219,23 @@ void tst_QDeclarativePropertyMap::crashBug()
     delete obj;
 }
 
+void tst_QDeclarativePropertyMap::QTBUG_17868()
+{
+    QDeclarativePropertyMap map;
+
+    QDeclarativeEngine engine;
+    QDeclarativeContext context(&engine);
+    context.setContextProperty("map", &map);
+    map.insert("key", 1);
+    QDeclarativeComponent c(&engine);
+    c.setData("import QtQuick 2.0\nItem {property bool error:false; Component.onCompleted: {try{console.log(map.keys());}catch(e) {error=true;}}}",QUrl());
+    QObject *obj = c.create(&context);
+    QVERIFY(obj);
+    QVERIFY(!obj->property("error").toBool());
+    delete obj;
+
+}
+
 QTEST_MAIN(tst_QDeclarativePropertyMap)
 
 #include "tst_qdeclarativepropertymap.moc"