Repository: doukutsu-rs/doukutsu-rs (language: rust)

Issue:
Crash in Labyrinth M
Running on linux, the game crashes in Labyrinth M (where Curly accompanies the player). The error message I get when running with `RUST_BACKTRACE=1`is as follows:
```
thread 'main' panicked at src/game/npc/ai/maze.rs:1526:13:
attempt to add with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panicking.rs:595:5
   1: core::panicking::panic_fmt
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/panicking.rs:67:14
   2: core::panicking::panic
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/panicking.rs:117:5
   3: doukutsu_rs::game::npc::ai::maze::<impl doukutsu_rs::game::npc::NPC>::tick_n187_fuzz_core
             at ./src/game/npc/ai/maze.rs:1526:13
   4: <doukutsu_rs::game::npc::NPC as doukutsu_rs::entity::GameEntity<([&mut doukutsu_rs::game::player::Player; 2],&doukutsu_rs::game::npc::list::NPCList,&mut doukutsu_rs::game::stage::Stage,&mut doukutsu_rs::game::weapon::bullet::BulletManager,&mut doukutsu_rs::components::flash::Flash,&mut doukutsu_rs::game::npc::boss::BossNPC)>>::tick
             at ./src/game/npc/mod.rs:441:20
   5: doukutsu_rs::scene::game_scene::GameScene::tick_world
             at ./src/scene/game_scene.rs:1380:13
   6: <doukutsu_rs::scene::game_scene::GameScene as doukutsu_rs::scene::Scene>::tick
             at ./src/scene/game_scene.rs:1863:29
   7: doukutsu_rs::game::Game::update
             at ./src/game/mod.rs:114:25
   8: <doukutsu_rs::framework::backend_sdl2::SDL2EventLoop as doukutsu_rs::framework::backend::BackendEventLoop>::run
             at ./src/framework/backend_sdl2.rs:412:13
   9: doukutsu_rs::framework::context::Context::run
             at ./src/framework/context.rs:43:9
  10: doukutsu_rs::game::init
             at ./src/game/mod.rs:322:5
  11: doukutsu_rs::main
             at ./src/main.rs:24:18
  12: core::ops::function::FnOnce::call_once
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
```

I get around 5 minutes into the area before it crashes.

I installed doukutsu-rs by following the instructions here: https://doukutsu-rs.gitbook.io/docs/getting-started/initial-setup-and-compiling#linux


Discussion / hints:
Oddly enough, using the [Linux x64 build](https://ci.appveyor.com/api/projects/alula/doukutsu-rs/artifacts/doukutsu-rs_linux.zip?branch=master&job=linux-x64) from Appveyor I had no issues in this section.
It's still an issue, because overflow checks are disabled in release builds and the compiling guide doesn't compile it with optimizations unless you specify `--release`.

Sounds like a regression introduced by one of @periwinkle9's patches?

> Sounds like a regression introduced by one of @periwinkle9's patches?

If I'm not mistaken, @periwinkle9 didn't make changes to the function where the error occurs
Looking at the `git blame`, it seems I didn't touch the offending line.
https://github.com/doukutsu-rs/doukutsu-rs/blob/12a305becb40552f54a0a6fad4b0d7de141785a7/src/game/npc/ai/maze.rs#L1526
This line could be replaced by `self.action_counter3 = self.action_counter3.wrapping_add(4);`, or even `self.action_counter3 = (self.action_counter3 + 4) & 0xFF;` I think would work based on how that variable is used.

I have to wonder how many more of these overflow bugs are lurking around in the code because nobody's sat in each room for like 20 minutes...

Fix patch:
diff --git a/src/game/npc/ai/maze.rs b/src/game/npc/ai/maze.rs
index cb67e58d..de0df6a8 100644
--- a/src/game/npc/ai/maze.rs
+++ b/src/game/npc/ai/maze.rs
@@ -1523,7 +1523,7 @@ impl NPC {
         } else if self.action_num == 2 {
             let player = self.get_closest_player_mut(players);
 
-            self.action_counter3 += 4;
+            self.action_counter3 = self.action_counter3.wrapping_add(4);
             self.direction = if player.x >= self.x { Direction::Right } else { Direction::Left };
 
             self.vel_y += (self.target_y - self.y).signum() * 0x10;


Test patch: