Document bugfix: ScriptCall can overflow wScriptStack and crash

This commit is contained in:
Rangi 2019-01-07 18:28:16 -05:00
parent 1b1b0ac6ea
commit fe04acad7b
2 changed files with 38 additions and 35 deletions

View File

@ -1422,43 +1422,46 @@ This supports up to six entries.
## `ScriptCall` can overflow `wScriptStack` and crash ## `ScriptCall` can overflow `wScriptStack` and crash
In [engine/overworld/scripting.asm](/engine/overworld/scripting.asm): **Fix:** Edit `ScriptCall` in [engine/overworld/scripting.asm](/engine/overworld/scripting.asm):
```asm ```diff
ScriptCall: ScriptCall:
; Bug: The script stack has a capacity of 5 scripts, yet there is -; Bug: The script stack has a capacity of 5 scripts, yet there is
; nothing to stop you from pushing a sixth script. The high part -; nothing to stop you from pushing a sixth script. The high part
; of the script address can then be overwritten by modifications -; of the script address can then be overwritten by modifications
; to wScriptDelay, causing the script to return to the rst/interrupt -; to wScriptDelay, causing the script to return to the rst/interrupt
; space. -; space.
-
push de + ld hl, wScriptStackSize
ld hl, wScriptStackSize + ld a, [hl]
ld e, [hl] + cp 5
inc [hl] + ret nc
ld d, $0 push de
ld hl, wScriptStack - ld hl, wScriptStackSize
add hl, de - ld e, [hl]
add hl, de inc [hl]
add hl, de + ld e, a
pop de ld d, 0
ld a, [wScriptBank] ld hl, wScriptStack
ld [hli], a add hl, de
ld a, [wScriptPos] add hl, de
ld [hli], a add hl, de
ld a, [wScriptPos + 1] pop de
ld [hl], a ld a, [wScriptBank]
ld a, b ld [hli], a
ld [wScriptBank], a ld a, [wScriptPos]
ld a, e ld [hli], a
ld [wScriptPos], a ld a, [wScriptPos + 1]
ld a, d ld [hl], a
ld [wScriptPos + 1], a ld a, b
ret ld [wScriptBank], a
ld a, e
ld [wScriptPos], a
ld a, d
ld [wScriptPos + 1], a
ret
``` ```
*To do:* Fix this bug.
## `LoadSpriteGFX` does not limit the capacity of `UsedSprites` ## `LoadSpriteGFX` does not limit the capacity of `UsedSprites`

View File

@ -1454,7 +1454,7 @@ ScriptCall:
ld hl, wScriptStackSize ld hl, wScriptStackSize
ld e, [hl] ld e, [hl]
inc [hl] inc [hl]
ld d, $0 ld d, 0
ld hl, wScriptStack ld hl, wScriptStack
add hl, de add hl, de
add hl, de add hl, de