Bug 69324

Summary: kasteroids segfaults sometimes when using shields
Product: [Applications] kasteroids Reporter: Todd Kirby <doubleshot>
Component: generalAssignee: Martin R. Jones <mjones>
Status: RESOLVED FIXED    
Severity: crash    
Priority: NOR    
Version: 2.2   
Target Milestone: ---   
Platform: Compiled Sources   
OS: Linux   
Latest Commit: Version Fixed In:
Attachments: fix powerup collision detection problem
Backtrace of kasteroids collision segfault

Description Todd Kirby 2003-11-30 10:16:29 UTC
Version:           2.2 (using KDE KDE 3.1.4)
Installed from:    Compiled From Sources
Compiler:          gcc 3.3.2 
OS:          Linux

There is a bug in the collision detection code for powerups. Hitting the shield key at the exact moment a powerup hits the ship (not easy to do, but it happens from time to time) causes a segfault. 

in view.cpp (kde 3.1.4) line 813 there is a loop to detect powerup collisions. It does not correctly handle the situation where a powerup hits both the ship and shield at the same time. The first time thru the loop the code handles the powerup colliding with the ship. It is processed and deleted from the list. The second time thru the loop the code tries to process the powerup hitting the shield, but since it has already been removed we get a segfault.

The fix is to add a break after line 842 (vitalschanged = true). There is no need to check for further collisions anyways since the powerup was collected.

I can attach a patch if that makes things clearer.

-Todd
Comment 1 Todd Kirby 2003-11-30 10:20:26 UTC
Created attachment 3471 [details]
fix powerup collision detection problem

diff to kasteroids/view.cpp in kde version 3.1.4
Comment 2 Todd Kirby 2003-11-30 20:15:51 UTC
Created attachment 3480 [details]
Backtrace of kasteroids collision segfault

Qt: 3.2.3
KDE: 3.1.4
KAsteroids: 2.2
Comment 3 Waldo Bastian 2004-02-17 22:33:09 UTC
CVS commit by waba: 

Fix crash when both shield and ship hit powerup at same time. (BR69324)
CCMAIL: 69324-done@bugs.kde.org


  M +4 -2      view.cpp   1.40.2.1


--- kdegames/kasteroids/view.cpp  #1.40:1.40.2.1
@@ -783,7 +783,7 @@ void KAsteroidsView::processPowerups()
         QPtrListIterator<KPowerup> it( powerups );
 
-        for( ; it.current(); ++it )
+        for( ; (pup = it.current()); )
         {
-            pup = it.current();
+            ++it; // We have to increase here, because pup may get deleted.
             pup->growOlder();
 
@@ -828,4 +828,5 @@ void KAsteroidsView::processPowerups()
                     powerups.removeRef( pup );
                     vitalsChanged = true;
+                    break;
                 }
 
@@ -835,4 +836,5 @@ void KAsteroidsView::processPowerups()
                     {
                       powerups.removeRef( pup );
+                      break;
                     }
                 }