From bf8a9dc1248083dfb0ce5599b11af1727d349631 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Fri, 29 Oct 2010 15:03:38 +0200 Subject: Don't crash when null-pointer is passed to qScriptValueFromValue() qScriptValueFromValue_helper() (or rather, QScriptEnginePrivate::create()) will fall back to wrapping the value using newVariant(), so there is no reason for the QVariant specialization to check the return value. (In particular, if the return value was invalid, that's _because_ the engine was null, so we should definitely not call a function on the engine.) Task-number: QTBUG-14842 Reviewed-by: Jedrzej Nowacki --- src/script/api/qscriptengine.h | 5 +---- tests/auto/qscriptengine/tst_qscriptengine.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/script/api/qscriptengine.h b/src/script/api/qscriptengine.h index 830d477..24c8c13 100644 --- a/src/script/api/qscriptengine.h +++ b/src/script/api/qscriptengine.h @@ -332,10 +332,7 @@ inline QScriptValue qScriptValueFromValue(QScriptEngine *engine, const T &t) template <> inline QScriptValue qScriptValueFromValue(QScriptEngine *engine, const QVariant &v) { - QScriptValue result = qScriptValueFromValue_helper(engine, v.userType(), v.data()); - if (!result.isValid()) - result = engine->newVariant(v); - return result; + return qScriptValueFromValue_helper(engine, v.userType(), v.data()); } inline bool qscriptvalue_cast_helper(const QScriptValue &value, int type, void *ptr) diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index 7133a6c..3a376ec 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -120,6 +120,7 @@ private slots: void uncaughtException(); void errorMessage_QT679(); void valueConversion(); + void qScriptValueFromValue_noEngine(); void importExtension(); void infiniteRecursion(); void castWithPrototypeChain(); @@ -2258,6 +2259,12 @@ void tst_QScriptEngine::valueConversion() } } +void tst_QScriptEngine::qScriptValueFromValue_noEngine() +{ + QVERIFY(!qScriptValueFromValue(0, 123).isValid()); + QVERIFY(!qScriptValueFromValue(0, QVariant(123)).isValid()); +} + static QScriptValue __import__(QScriptContext *ctx, QScriptEngine *eng) { return eng->importExtension(ctx->argument(0).toString()); -- cgit v0.12