bugfix: IndexOutOfRangeException in pathfinding due to byte overflow#832
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses critical IndexOutOfRangeException bugs in the pathfinding system caused by byte arithmetic overflows. By switching loop counters to integers, enforcing stricter bounds checking, and sanitizing offset calculations, the changes ensure that pathfinding operations remain stable even when monsters move near the edges of the map. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces safety checks and fixes overflow bugs in the pathfinding grid networks. Specifically, it moves the bounds check in BaseGridNetwork to prevent out-of-bounds array access, changes loop counters from byte to int in ScopedGridNetwork to avoid infinite loops from overflow, and adds index validation. The reviewer noted that casting wrapped coordinates directly to byte in BaseGridNetwork can still cause logical bugs (like teleportation across the map edges) and suggested performing coordinate arithmetic using int before casting.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Two
IndexOutOfRangeExceptionbugs caused bybytearithmetic overflow whenmonsters walk near map edges, plus additional performance improvements on the
hot neighbor-expansion path.
Bug fixes
Bug 1: Byte loop counter overflow in
ScopedGridNetwork.Prepare—maxXis
int(byte+byte promotes to int) but the loop usedbyte x. At offset 240with segment length 16,
maxX = 256. Whenxwraps from 255→0 the loopcontinues,
GetIndexOfPoint(0, y)returns -1, crashing on_gridNodes[-1].Bug 2: Byte overflow in
BaseGridNetwork.GetPossibleNextNodesdirectionoffsets —
(byte)(node.X + (-1))wraps to 255. The code accessedgrid[255, y]before the bounds check could reject it.Additional fix in
GetOffset—(byte)(gridSize - segmentLength)wrapsnegative values to large positives (e.g.
(byte)(-6) = 250), pushing thesegment outside grid bounds.
Performance improvements (same hot path)
(uint)x > 255replaces two signed comparisonswith one branch per axis.
grid[newX, newY]read once, reused for safezonecheck and cost mask, eliminating redundant
byte[,]bounds checks.sbyte[]instead ofsbyte[,], removing 2Dstride multiply and improving JIT bounds-check elimination.
newGnow uses the maskedcostToNodeinsteadof raw
this._grid[...], so safezone flags (bit 7) no longer corrupt pathcost.