unit: Fixed out of bounds reads and write in unit->unit collision
authorLukas Krickl <lukas@krickl.dev>
Sat, 21 Jun 2025 13:49:56 +0000 (15:49 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sat, 21 Jun 2025 13:49:56 +0000 (15:49 +0200)
This was caused by attempting to use a clobbered A value as the loop
index.

src/actortables.s
src/unit.s

index 331ecc8c521ed620d34c18fcba6b24979cb696c4..bf7807afdd09f149cd613f15b34f86faf0e6f446 100644 (file)
@@ -3,7 +3,7 @@
 default_map_actor_table:
 .db 2 ; size
 dw unit_demo_2
-dw unit_demo_3
+dw unit_demo_3
 
 
 
index 1bdf21afdb9a7ec604bc89fd9d6c7d4c97697e84..344af061f4776f85862b40076d265523a7ba83a1 100644 (file)
@@ -265,13 +265,27 @@ unit_handle_inputs:
   ;   a = CF_COLLISION if collision occured
   ;   sets act_rt_collided_with
 unit_collides_with_any_other:
+#define scratch_loop_i scratch
   ld hl, p0_units
   ld a, UNITS_MAX ; loop counter
 
 @loop:
+    ld [scratch_loop_i], a
     push de ; save current actor on stack
     push bc
     
+    ; skip act type == 0
+    push de
+    push hl
+    ld de, act_type
+    add hl, de ; hl = actor type
+    ; skip if type is 0
+    ld a, [hl] 
+    cp a, 0
+    pop hl
+    pop de
+    jr z, @skip REL 
+    
     ; check if actor is current actor
     ld a, h
     cp a, d
@@ -300,6 +314,8 @@ unit_collides_with_any_other:
 @skip: 
     ld de, act_size
     add hl, de ; move to next actor
+
+    ld a, [scratch_loop_i]
     dec a
 
     pop bc
@@ -326,6 +342,7 @@ unit_collides_with_any_other:
   ; and set the CF_COLLISION flag
   ld a, CF_COLLISION
   ret
+#undefine scratch_loop_i
 
   ; moves a unit up
   ; moves are aborted