Skip to content

Commit 06a08dc

Browse files
committed
fix: add bounds clamping in c_display::fill_rect() for negative coords
The base class fill_rect() computed framebuffer offsets without validating that coordinates are non-negative, causing out-of-bounds memory writes with negative inputs. The child class c_surface already had this protection.
1 parent 9b07902 commit 06a08dc

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/core/display.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ class c_display {
121121

122122
int _width = m_width;
123123
int _height = m_height;
124+
if (x0 < 0) x0 = 0;
125+
if (y0 < 0) y0 = 0;
126+
if (x1 >= _width) x1 = _width - 1;
127+
if (y1 >= _height) y1 = _height - 1;
128+
if (x0 > x1 || y0 > y1) return;
124129
int x, y;
125130
if (m_color_bytes == 2)
126131
{
@@ -190,7 +195,7 @@ class c_display {
190195
c_surface* m_surface_group[SURFACE_CNT_MAX];
191196
int m_surface_cnt; //surface count
192197
int m_surface_index;
193-
198+
194199
};
195200

196201
class c_layer
@@ -240,7 +245,7 @@ class c_surface {
240245
{
241246
return;
242247
}
243-
248+
244249
if (z_order > (unsigned int)m_max_zorder)
245250
{
246251
ASSERT(false);
@@ -269,7 +274,7 @@ class c_surface {
269274
((unsigned int*)(m_layers[z_order].fb))[(x - layer_rect.m_left) + (y - layer_rect.m_top) * layer_rect.width()] = rgb;
270275
}
271276
}
272-
277+
273278
if (z_order == m_top_zorder)
274279
{
275280
return draw_pixel_low_level(x, y, rgb);
@@ -320,7 +325,7 @@ class c_surface {
320325
}
321326
else
322327
{
323-
((unsigned int*)m_layers[z_order].fb)[(y - layer_rect.m_top) * width + (x - layer_rect.m_left)] = rgb;
328+
((unsigned int*)m_layers[z_order].fb)[(y - layer_rect.m_top) * width + (x - layer_rect.m_left)] = rgb;
324329
}
325330
}
326331
}
@@ -430,7 +435,7 @@ class c_surface {
430435
void activate_layer(c_rect active_rect, unsigned int active_z_order)//empty active rect means inactivating the layer
431436
{
432437
ASSERT(active_z_order > Z_ORDER_LEVEL_0 && active_z_order <= Z_ORDER_LEVEL_MAX);
433-
438+
434439
//Show the layers below the current active rect.
435440
c_rect current_active_rect = m_layers[active_z_order].active_rect;
436441
for(int low_z_order = Z_ORDER_LEVEL_0; low_z_order < active_z_order; low_z_order++)
@@ -551,7 +556,7 @@ inline c_display::c_display(void* phy_fb, int display_width, int display_height,
551556
ASSERT(color_bytes == 2 || color_bytes == 4);
552557
ASSERT(m_surface_cnt <= SURFACE_CNT_MAX);
553558
memset(m_surface_group, 0, sizeof(m_surface_group));
554-
559+
555560
for (int i = 0; i < m_surface_cnt; i++)
556561
{
557562
m_surface_group[i] = new c_surface(surface_width, surface_height, color_bytes);

0 commit comments

Comments
 (0)