fix buffer overflow in put pixel + margin calculation

This commit is contained in:
2026-03-30 14:37:33 +02:00
parent 92c6237f06
commit 0f77e0c6e4
3 changed files with 50 additions and 49 deletions
+41 -38
View File
@@ -41,6 +41,8 @@ class MazeMLX:
) )
def put_pixel(self, x, y) -> None: 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) offset = y * self.size_line + x * (self.bpp // 8)
self.buf[offset + 0] = 0xFF self.buf[offset + 0] = 0xFF
@@ -64,23 +66,23 @@ class MazeMLX:
def update_maze(self, maze: np.ndarray) -> None: def update_maze(self, maze: np.ndarray) -> None:
self.clear_image() self.clear_image()
margin = math.trunc(
math.sqrt(self.width if self.width > self.height else self.height) rows = len(maze)
// 2 cols = len(maze[0])
)
line_len = math.trunc( line_len = min(self.width // cols, self.height // rows)
(
(self.height - margin) // len(maze) maze_width = cols * line_len
if self.height > self.width maze_height = rows * line_len
else (self.width - margin) // len(maze[0])
) margin_x = (self.width - maze_width) // 2
) margin_y = (self.height - maze_height) // 2
for y in range(len(maze)): for y in range(len(maze)):
for x in range(len(maze[0])): for x in range(len(maze[0])):
x0 = x * line_len + margin x0 = x * line_len + margin_x
y0 = y * line_len + margin y0 = y * line_len + margin_y
x1 = x * line_len + line_len + margin x1 = x * line_len + line_len + margin_x
y1 = y * line_len + line_len + margin y1 = y * line_len + line_len + margin_y
if maze[y][x].get_north(): if maze[y][x].get_north():
self.put_line((x0, y0), (x1, y0)) self.put_line((x0, y0), (x1, y0))
@@ -104,33 +106,34 @@ class MazeMLX:
maze = amazing.maze.get_maze() maze = amazing.maze.get_maze()
if maze is None: if maze is None:
return return
margin = math.trunc(
math.sqrt(self.width if self.width > self.height else self.height) rows = len(maze)
// 2 cols = len(maze[0])
)
cell_size = math.trunc( line_len = min(self.width // cols, self.height // rows)
(
(self.height - margin) // len(maze) maze_width = cols * line_len
if self.height > self.width maze_height = rows * line_len
else (self.width - margin) // len(maze[0])
) margin_x = (self.width - maze_width) // 2
) margin_y = (self.height - maze_height) // 2
self.update_maze(maze) self.update_maze(maze)
for i in range(len(path)): for i in range(len(path)):
ul = ( ul = (
(actual[0]) * cell_size + margin + 12, (actual[0]) * line_len + margin_x + 12,
(actual[1]) * cell_size + 12 + margin, (actual[1]) * line_len + 12 + margin_y,
) )
dr = ( dr = (
(actual[0]) * cell_size + cell_size + margin - 12, (actual[0]) * line_len + line_len + margin_x - 12,
(actual[1]) * cell_size + cell_size - 12 + margin, (actual[1]) * line_len + line_len - 12 + margin_y,
) )
self.put_block(ul, dr) self.put_block(ul, dr)
self.redraw_image() self.redraw_image()
x0 = actual[0] * cell_size + margin + 12 x0 = actual[0] * line_len + margin_x + 12
y0 = actual[1] * cell_size + margin + 12 y0 = actual[1] * line_len + margin_y + 12
x1 = actual[0] * cell_size + cell_size + margin - 12 x1 = actual[0] * line_len + line_len + margin_x - 12
y1 = actual[1] * cell_size + cell_size + margin - 12 y1 = actual[1] * line_len + line_len + margin_y - 12
yield yield
match path[i]: match path[i]:
case "N": case "N":
@@ -146,12 +149,12 @@ class MazeMLX:
self.put_block((x0, y0), (x0 - 24, y1)) self.put_block((x0, y0), (x0 - 24, y1))
actual = (actual[0] - 1, actual[1]) actual = (actual[0] - 1, actual[1])
ul = ( ul = (
(actual[0]) * cell_size + margin + 12, (actual[0]) * line_len + margin_x + 12,
(actual[1]) * cell_size + 12 + margin, (actual[1]) * line_len + 12 + margin_y,
) )
dr = ( dr = (
(actual[0]) * cell_size + cell_size + margin - 12, (actual[0]) * line_len + line_len + margin_x - 12,
(actual[1]) * cell_size + cell_size - 12 + margin, (actual[1]) * line_len + line_len - 12 + margin_y,
) )
self.put_block(ul, dr) self.put_block(ul, dr)
self.redraw_image() self.redraw_image()
+1 -1
View File
@@ -1,7 +1,7 @@
WIDTH=15 WIDTH=15
HEIGHT=15 HEIGHT=15
ENTRY=1,1 ENTRY=1,1
EXIT=15,15 EXIT=10,10
OUTPUT_FILE=maze.txt OUTPUT_FILE=maze.txt
PERFECT=False PERFECT=False
GENERATOR=Kruskal GENERATOR=Kruskal
+8 -10
View File
@@ -11,7 +11,7 @@ class MazeSolver(ABC):
@abstractmethod @abstractmethod
def solve( def solve(
self, maze: Maze, height: int = None, width: int = None self, maze: Maze, height: int | None = None, width: int | None = None
) -> str: ... ) -> str: ...
@@ -38,9 +38,6 @@ class AStar(MazeSolver):
super().__init__(start, end) super().__init__(start, end)
self.path = [] self.path = []
def g(self, n: tuple[int, int]) -> int:
return len(self.path) + 1
def h(self, n: tuple[int, int]) -> int: def h(self, n: tuple[int, int]) -> int:
return ( return (
max(n[0], self.end[0]) max(n[0], self.end[0])
@@ -49,9 +46,6 @@ class AStar(MazeSolver):
- min(n[1], self.end[1]) - min(n[1], self.end[1])
) )
def f(self, n: tuple[int, int]) -> int:
return self.g(n) + self.h(n)
def get_paths( def get_paths(
self, self,
maze: np.ndarray, maze: np.ndarray,
@@ -103,7 +97,7 @@ class AStar(MazeSolver):
self.start, self.start,
0, 0,
self.h(self.start), self.h(self.start),
self.f(self.start), self.h(self.start),
None, None,
) )
) )
@@ -161,7 +155,9 @@ class AStar(MazeSolver):
break break
return res 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()) path = self.get_path(maze.get_maze())
return self.translate(path) return self.translate(path)
@@ -170,7 +166,9 @@ class DepthFirstSearchSolver(MazeSolver):
def __init__(self, start, end): def __init__(self, start, end):
super().__init__(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 = "" path_str = ""
visited = np.zeros((height, width), dtype=bool) visited = np.zeros((height, width), dtype=bool)
path = list() path = list()