summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi42@gmail.com>2019-01-07 18:28:16 -0500
committerRangi <remy.oukaour+rangi42@gmail.com>2019-01-07 18:28:47 -0500
commitfe04acad7babb7fde1e4dfaa4768877cc84d7e48 (patch)
tree2514ff0ff940496afb66001cb5478089b05d4f3f
parent1b1b0ac6ea1bb0ca11c2aa05411ece130f9cb98c (diff)
Document bugfix: ScriptCall can overflow wScriptStack and crash
-rw-r--r--docs/bugs_and_glitches.md71
-rw-r--r--engine/overworld/scripting.asm2
2 files changed, 38 insertions, 35 deletions
diff --git a/docs/bugs_and_glitches.md b/docs/bugs_and_glitches.md
index f95d519cb..0fa796938 100644
--- a/docs/bugs_and_glitches.md
+++ b/docs/bugs_and_glitches.md
@@ -1422,43 +1422,46 @@ This supports up to six entries.
## `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
-ScriptCall:
-; 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
-; of the script address can then be overwritten by modifications
-; to wScriptDelay, causing the script to return to the rst/interrupt
-; space.
-
- push de
- ld hl, wScriptStackSize
- ld e, [hl]
- inc [hl]
- ld d, $0
- ld hl, wScriptStack
- add hl, de
- add hl, de
- add hl, de
- pop de
- ld a, [wScriptBank]
- ld [hli], a
- ld a, [wScriptPos]
- ld [hli], a
- ld a, [wScriptPos + 1]
- ld [hl], a
- ld a, b
- ld [wScriptBank], a
- ld a, e
- ld [wScriptPos], a
- ld a, d
- ld [wScriptPos + 1], a
- ret
+```diff
+ ScriptCall:
+-; 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
+-; of the script address can then be overwritten by modifications
+-; to wScriptDelay, causing the script to return to the rst/interrupt
+-; space.
+-
++ ld hl, wScriptStackSize
++ ld a, [hl]
++ cp 5
++ ret nc
+ push de
+- ld hl, wScriptStackSize
+- ld e, [hl]
+ inc [hl]
++ ld e, a
+ ld d, 0
+ ld hl, wScriptStack
+ add hl, de
+ add hl, de
+ add hl, de
+ pop de
+ ld a, [wScriptBank]
+ ld [hli], a
+ ld a, [wScriptPos]
+ ld [hli], a
+ ld a, [wScriptPos + 1]
+ ld [hl], a
+ ld a, b
+ 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`
diff --git a/engine/overworld/scripting.asm b/engine/overworld/scripting.asm
index 5b72bd83d..d1f4ceecf 100644
--- a/engine/overworld/scripting.asm
+++ b/engine/overworld/scripting.asm
@@ -1454,7 +1454,7 @@ ScriptCall:
ld hl, wScriptStackSize
ld e, [hl]
inc [hl]
- ld d, $0
+ ld d, 0
ld hl, wScriptStack
add hl, de
add hl, de