Skip to content

Commit cc5a08b

Browse files
committed
fixed undefined behavior on step = 0
1 parent e8f7ffd commit cc5a08b

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

include/bitbishop/lookups/between_squares.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Lookups {
1717
* - +-9 for NE–SW diagonal movement
1818
* - +-7 for NW–SE diagonal movement
1919
* - 0 if source and destination square are identical
20+
* - 0 if source and destination squares are not aligned
2021
*
2122
* This function assumes that the two squares are aligned. Its primary use
2223
* is internal traversal of rays when building geometric lookup tables such
@@ -72,9 +73,13 @@ constexpr Bitboard ray_between(Square from, Square to) {
7273
return Bitboard::Zeros();
7374
}
7475

76+
// Defensive check: if step is 0, squares aren't truly aligned
7577
const int step = direction(from, to);
76-
Bitboard ray = Bitboard::Zeros();
78+
if (step == 0) {
79+
return Bitboard::Zeros();
80+
}
7781

82+
Bitboard ray = Bitboard::Zeros();
7883
for (std::uint8_t square = from.value() + step; square != to.value(); square += step) {
7984
ray.set(square);
8085
}

0 commit comments

Comments
 (0)