Skip to content

Commit 6a5b4d3

Browse files
committed
fix: address PR review feedback
Signed-off-by: Zendy <50132805+zendy199x@users.noreply.github.com>
1 parent dc28730 commit 6a5b4d3

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

example_transcoding/utils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ uint32_t image_u8::flood_fill(int x, int y, const color_quad_u8& c, const color_
2222
FLOOD_PUSH(y, x, x, 1);
2323
FLOOD_PUSH(y + 1, x, x, -1);
2424

25+
const size_t MAX_STACK_SIZE = 65536; // Prevent unbounded stack growth
2526
while (stack.size())
2627
{
28+
if (stack.size() > MAX_STACK_SIZE)
29+
return 0; // Return error on stack overflow
30+
2731
fill_segment s = stack.back();
2832
stack.pop_back();
2933

tests/test_utils.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import pytest
2+
utils = pytest.importorskip("example_transcoding.utils")
3+
image_u8 = utils.image_u8
4+
color_quad_u8 = utils.color_quad_u8
5+
pixel_coord = utils.pixel_coord
6+
7+
class TestUtils:
8+
def test_flood_fill_stack_overflow_protection(self):
9+
# Create a large image
10+
img = image_u8(1000, 1000)
11+
12+
# Fill the entire image with a background color
13+
bg_color = color_quad_u8(0, 0, 0, 255)
14+
img.set_all(bg_color)
15+
16+
# Try to flood fill with a small region that would cause stack overflow
17+
# This should not cause a crash or infinite loop
18+
fill_color = color_quad_u8(255, 255, 255, 255)
19+
b_color = color_quad_u8(0, 0, 0, 255)
20+
21+
# Test with a point that would trigger large stack growth
22+
result = img.flood_fill(500, 500, fill_color, b_color, None)
23+
24+
# Flood fill a large region; this should complete without stack overflow
25+
# and should not cause a crash or infinite loop
26+
fill_color = color_quad_u8(255, 255, 255, 255)
27+
b_color = color_quad_u8(0, 0, 0, 255)
28+
29+
# Test with a point that triggers flood fill of the large background region
30+
result = img.flood_fill(500, 500, fill_color, b_color, None)
31+
32+
# Should return a positive number indicating pixels filled
33+
assert result > 0
34+
35+
def test_flood_fill_normal_operation(self):
36+
# Create a small image
37+
img = image_u8(10, 10)
38+
39+
# Fill with background
40+
bg_color = color_quad_u8(0, 0, 0, 255)
41+
for y in range(10):
42+
for x in range(10):
43+
img.set_pixel_clipped(x, y, bg_color)
44+
45+
# Fill a small region
46+
fill_color = color_quad_u8(255, 255, 255, 255)
47+
b_color = color_quad_u8(0, 0, 0, 255)
48+
49+
# Test normal flood fill operation
50+
result = img.flood_fill(5, 5, fill_color, b_color, None)
51+
52+
# Should return a positive number
53+
assert result > 0
54+
55+
def test_flood_fill_edge_case_outside_bounds(self):
56+
# Create a small image
57+
img = image_u8(10, 10)
58+
59+
# Fill with background
60+
bg_color = color_quad_u8(0, 0, 0, 255)
61+
for y in range(10):
62+
for x in range(10):
63+
img.set_pixel_clipped(x, y, bg_color)
64+
65+
# Try to flood fill outside image bounds
66+
fill_color = color_quad_u8(255, 255, 255, 255)
67+
b_color = color_quad_u8(0, 0, 0, 255)
68+
69+
# Should return 0 for out of bounds
70+
result = img.flood_fill(-1, 5, fill_color, b_color, None)
71+
assert result == 0
72+
73+
result = img.flood_fill(5, -1, fill_color, b_color, None)
74+
assert result == 0
75+
76+
result = img.flood_fill(15, 5, fill_color, b_color, None)
77+
assert result == 0
78+
79+
result = img.flood_fill(5, 15, fill_color, b_color, None)
80+
assert result == 0

0 commit comments

Comments
 (0)