From 0f77e0c6e46dd5dc68dda8720c3b5194c4409686 Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Mon, 30 Mar 2026 14:37:33 +0200 Subject: [PATCH] fix buffer overflow in put pixel + margin calculation --- a_maze_ing.py | 79 ++++++++++++++++++++------------------ config.txt | 2 +- src/amaz_lib/MazeSolver.py | 18 ++++----- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/a_maze_ing.py b/a_maze_ing.py index 8175b2e..04bd3ae 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -41,6 +41,8 @@ class MazeMLX: ) def put_pixel(self, x, y) -> None: + if x < 0 or y < 0 or x >= self.width or y >= self.height: + return offset = y * self.size_line + x * (self.bpp // 8) self.buf[offset + 0] = 0xFF @@ -64,23 +66,23 @@ class MazeMLX: def update_maze(self, maze: np.ndarray) -> None: self.clear_image() - margin = math.trunc( - math.sqrt(self.width if self.width > self.height else self.height) - // 2 - ) - line_len = math.trunc( - ( - (self.height - margin) // len(maze) - if self.height > self.width - else (self.width - margin) // len(maze[0]) - ) - ) + + rows = len(maze) + cols = len(maze[0]) + + line_len = min(self.width // cols, self.height // rows) + + maze_width = cols * line_len + maze_height = rows * line_len + + margin_x = (self.width - maze_width) // 2 + margin_y = (self.height - maze_height) // 2 for y in range(len(maze)): for x in range(len(maze[0])): - x0 = x * line_len + margin - y0 = y * line_len + margin - x1 = x * line_len + line_len + margin - y1 = y * line_len + line_len + margin + x0 = x * line_len + margin_x + y0 = y * line_len + margin_y + x1 = x * line_len + line_len + margin_x + y1 = y * line_len + line_len + margin_y if maze[y][x].get_north(): self.put_line((x0, y0), (x1, y0)) @@ -104,33 +106,34 @@ class MazeMLX: maze = amazing.maze.get_maze() if maze is None: return - margin = math.trunc( - math.sqrt(self.width if self.width > self.height else self.height) - // 2 - ) - cell_size = math.trunc( - ( - (self.height - margin) // len(maze) - if self.height > self.width - else (self.width - margin) // len(maze[0]) - ) - ) + + rows = len(maze) + cols = len(maze[0]) + + line_len = min(self.width // cols, self.height // rows) + + maze_width = cols * line_len + maze_height = rows * line_len + + margin_x = (self.width - maze_width) // 2 + margin_y = (self.height - maze_height) // 2 + self.update_maze(maze) for i in range(len(path)): ul = ( - (actual[0]) * cell_size + margin + 12, - (actual[1]) * cell_size + 12 + margin, + (actual[0]) * line_len + margin_x + 12, + (actual[1]) * line_len + 12 + margin_y, ) dr = ( - (actual[0]) * cell_size + cell_size + margin - 12, - (actual[1]) * cell_size + cell_size - 12 + margin, + (actual[0]) * line_len + line_len + margin_x - 12, + (actual[1]) * line_len + line_len - 12 + margin_y, ) self.put_block(ul, dr) self.redraw_image() - x0 = actual[0] * cell_size + margin + 12 - y0 = actual[1] * cell_size + margin + 12 - x1 = actual[0] * cell_size + cell_size + margin - 12 - y1 = actual[1] * cell_size + cell_size + margin - 12 + x0 = actual[0] * line_len + margin_x + 12 + y0 = actual[1] * line_len + margin_y + 12 + x1 = actual[0] * line_len + line_len + margin_x - 12 + y1 = actual[1] * line_len + line_len + margin_y - 12 yield match path[i]: case "N": @@ -146,12 +149,12 @@ class MazeMLX: self.put_block((x0, y0), (x0 - 24, y1)) actual = (actual[0] - 1, actual[1]) ul = ( - (actual[0]) * cell_size + margin + 12, - (actual[1]) * cell_size + 12 + margin, + (actual[0]) * line_len + margin_x + 12, + (actual[1]) * line_len + 12 + margin_y, ) dr = ( - (actual[0]) * cell_size + cell_size + margin - 12, - (actual[1]) * cell_size + cell_size - 12 + margin, + (actual[0]) * line_len + line_len + margin_x - 12, + (actual[1]) * line_len + line_len - 12 + margin_y, ) self.put_block(ul, dr) self.redraw_image() diff --git a/config.txt b/config.txt index 091d975..71cf595 100644 --- a/config.txt +++ b/config.txt @@ -1,7 +1,7 @@ WIDTH=15 HEIGHT=15 ENTRY=1,1 -EXIT=15,15 +EXIT=10,10 OUTPUT_FILE=maze.txt PERFECT=False GENERATOR=Kruskal diff --git a/src/amaz_lib/MazeSolver.py b/src/amaz_lib/MazeSolver.py index 330a999..c77bffb 100644 --- a/src/amaz_lib/MazeSolver.py +++ b/src/amaz_lib/MazeSolver.py @@ -11,7 +11,7 @@ class MazeSolver(ABC): @abstractmethod def solve( - self, maze: Maze, height: int = None, width: int = None + self, maze: Maze, height: int | None = None, width: int | None = None ) -> str: ... @@ -38,9 +38,6 @@ class AStar(MazeSolver): super().__init__(start, end) self.path = [] - def g(self, n: tuple[int, int]) -> int: - return len(self.path) + 1 - def h(self, n: tuple[int, int]) -> int: return ( max(n[0], self.end[0]) @@ -49,9 +46,6 @@ class AStar(MazeSolver): - min(n[1], self.end[1]) ) - def f(self, n: tuple[int, int]) -> int: - return self.g(n) + self.h(n) - def get_paths( self, maze: np.ndarray, @@ -103,7 +97,7 @@ class AStar(MazeSolver): self.start, 0, self.h(self.start), - self.f(self.start), + self.h(self.start), None, ) ) @@ -161,7 +155,9 @@ class AStar(MazeSolver): break return res - def solve(self, maze: Maze, height: int = None, width: int = None) -> str: + def solve( + self, maze: Maze, height: int | None = None, width: int | None = None + ) -> str: path = self.get_path(maze.get_maze()) return self.translate(path) @@ -170,7 +166,9 @@ class DepthFirstSearchSolver(MazeSolver): def __init__(self, start, end): super().__init__(start, end) - def solve(self, maze: Maze, height: int = None, width: int = None) -> str: + def solve( + self, maze: Maze, height: int | None = None, width: int | None = None + ) -> str: path_str = "" visited = np.zeros((height, width), dtype=bool) path = list()