From 355c4211aa370ad593513f8abc23b71f16e27892 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Mon, 1 Sep 2025 11:30:16 +0200 Subject: [PATCH] Fix bug in largest_square: y direction We were not taking into account that the largest_square may be constrained by the y position as well as the x. i.e. is there enough vertical space for the square. This commit adds that check. We were working before because C++ is not memory safe, and we weren't running debug builds. --- main.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.cc b/main.cc index 8046875..1cec77c 100644 --- a/main.cc +++ b/main.cc @@ -137,7 +137,9 @@ struct Grid { if (grid_[b + y(pos) * length_] != '.') { break; } ++b; } - return b - x(pos); + auto len = b - x(pos); + auto ye = std::min(y(pos) + len, length_); + return ye - y(pos); } /** Get the next position to check. n is the size of the square we just