From 434fa0406e1b01395a2b7f0aa481ca5dc367fa09 Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Sat, 5 Sep 2026 21:53:47 -0700 Subject: Harden _WhoControlsSquareFast against g_SwapTable out-of-bounds indexing Add ASSERT(uWhite < 32)/ASSERT(uBlack < 32), the bound that actually matters for g_SwapTable[14][32][32] -- the existing (& 0xFFFFFF00) checks only caught garbage above bit 7 and would have passed silently through the exact out-of-bounds indexing fixed in the previous commit, had a similar bit-layout mistake been made again in the future. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01XxmVi2sTMwpPp4i6WYFjan --- src/eval.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/eval.c b/src/eval.c index eb6e118..63fd301 100755 --- a/src/eval.c +++ b/src/eval.c @@ -1149,6 +1149,16 @@ Return value: PIECE p; CHAR ch; + // g_SwapTable is only [14][32][32] (data.c) -- this is the bound + // that actually matters, not just "no garbage above bit 7" below. + // PAWN_BIT..KING_BIT briefly didn't fit in bits 0-4 earlier this + // session (when _WhoControlsSquareFast was converted to read them + // directly instead of going through the old ATTACK_BITV struct) + // and silently indexed out of bounds on every attacked square -- + // the (& 0xFFFFFF00) checks below never caught it (board_ + // representation/EVAL.md section 9). + ASSERT(uWhite < 32); + ASSERT(uBlack < 32); ASSERT((uWhite & 0xFFFFFF00) == 0); ASSERT((uBlack & 0xFFFFFF00) == 0); -- cgit v1.3