From d21c096f358eaf4dd7245680f38b836bebe43da5 Mon Sep 17 00:00:00 2001
From: Alex Blasche <alexander.blasche@digia.com>
Date: Mon, 20 Oct 2014 13:07:21 +0200
Subject: [PATCH] Fix crash in SpriteSequence

When QML declares sprites and goalSprite in wrong order the goalsprite
attempts to set it on null spriteEngine. This patch ensures that the
order doesn't matter anymore.

Task-number: QTBUG-40595
Change-Id: I57f1c8754b2e2af91e0c7f72d1d67fec3ad4ede5
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
---
 src/quick/items/qquickspritesequence.cpp      | 10 +++-
 .../data/spriteaftergoal.qml                  | 60 +++++++++++++++++++
 .../data/spritebeforegoal.qml                 | 60 +++++++++++++++++++
 .../tst_qquickspritesequence.cpp              | 26 ++++++++
 4 files changed, 153 insertions(+), 3 deletions(-)
 create mode 100644 tests/auto/quick/qquickspritesequence/data/spriteaftergoal.qml
 create mode 100644 tests/auto/quick/qquickspritesequence/data/spritebeforegoal.qml

diff --git a/src/quick/items/qquickspritesequence.cpp b/src/quick/items/qquickspritesequence.cpp
index 0be6486bf0..a9a823c2ce 100644
--- a/src/quick/items/qquickspritesequence.cpp
+++ b/src/quick/items/qquickspritesequence.cpp
@@ -243,7 +243,8 @@ void QQuickSpriteSequence::setGoalSprite(const QString &sprite)
     if (m_goalState != sprite){
         m_goalState = sprite;
         emit goalSpriteChanged(sprite);
-        m_spriteEngine->setGoal(m_spriteEngine->stateIndex(sprite));
+        if (m_spriteEngine)
+            m_spriteEngine->setGoal(m_spriteEngine->stateIndex(sprite));
     }
 }
 
@@ -257,10 +258,13 @@ void QQuickSpriteSequence::createEngine()
     //TODO: delay until component complete
     if (m_spriteEngine)
         delete m_spriteEngine;
-    if (m_sprites.count())
+    if (m_sprites.count()) {
         m_spriteEngine = new QQuickSpriteEngine(m_sprites, this);
-    else
+        if (!m_goalState.isEmpty())
+            m_spriteEngine->setGoal(m_spriteEngine->stateIndex(m_goalState));
+    } else {
         m_spriteEngine = 0;
+    }
     reset();
 }
 
diff --git a/tests/auto/quick/qquickspritesequence/data/spriteaftergoal.qml b/tests/auto/quick/qquickspritesequence/data/spriteaftergoal.qml
new file mode 100644
index 0000000000..189458ad90
--- /dev/null
+++ b/tests/auto/quick/qquickspritesequence/data/spriteaftergoal.qml
@@ -0,0 +1,60 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+// QTBUG-40595
+import QtQuick 2.0
+
+Rectangle {
+    width: 320
+    height: 320
+
+    SpriteSequence
+    {
+        anchors.centerIn: parent
+
+        width: 300
+        height: 300
+
+        goalSprite: "foobar"
+
+        sprites:
+        [
+            Sprite
+            {
+                name: "foobar"
+                source: "squarefacesprite.png"
+            }
+        ]
+    }
+}
+
diff --git a/tests/auto/quick/qquickspritesequence/data/spritebeforegoal.qml b/tests/auto/quick/qquickspritesequence/data/spritebeforegoal.qml
new file mode 100644
index 0000000000..23326fb5cc
--- /dev/null
+++ b/tests/auto/quick/qquickspritesequence/data/spritebeforegoal.qml
@@ -0,0 +1,60 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+// QTBUG-40595
+import QtQuick 2.0
+
+Rectangle {
+    width: 320
+    height: 320
+
+    SpriteSequence
+    {
+        anchors.centerIn: parent
+
+        width: 300
+        height: 300
+
+        sprites:
+        [
+            Sprite
+            {
+                name: "foobar"
+                source: "squarefacesprite.png"
+            }
+        ]
+
+        goalSprite: "foobar"
+    }
+}
+
diff --git a/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp b/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp
index 6cfdc90364..26ddff1009 100644
--- a/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp
+++ b/tests/auto/quick/qquickspritesequence/tst_qquickspritesequence.cpp
@@ -46,6 +46,8 @@ private slots:
     void test_framerateAdvance();//Separate codepath for QQuickSpriteEngine
     void test_huge();//Separate codepath for QQuickSpriteEngine
     void test_jumpToCrash();
+    void test_spriteBeforeGoal();
+    void test_spriteAfterGoal();
 };
 
 void tst_qquickspritesequence::test_properties()
@@ -119,6 +121,30 @@ void tst_qquickspritesequence::test_jumpToCrash()
     delete window;
 }
 
+void tst_qquickspritesequence::test_spriteBeforeGoal()
+{
+    QQuickView *window = new QQuickView(0);
+
+    window->setSource(testFileUrl("spritebeforegoal.qml"));
+    window->show();
+    QVERIFY(QTest::qWaitForWindowExposed(window));
+    //verify: Don't crash
+
+    delete window;
+}
+
+void tst_qquickspritesequence::test_spriteAfterGoal()
+{
+    QQuickView *window = new QQuickView(0);
+
+    window->setSource(testFileUrl("spriteaftergoal.qml"));
+    window->show();
+    QVERIFY(QTest::qWaitForWindowExposed(window));
+    //verify: Don't crash
+
+    delete window;
+}
+
 QTEST_MAIN(tst_qquickspritesequence)
 
 #include "tst_qquickspritesequence.moc"
-- 
GitLab