Skip to content

fix: add bounds clamping in c_display::fill_rect() for negative coordinates - #81

Open
srpatcha wants to merge 3 commits into
idea4good:masterfrom
srpatcha:fix/display-fill-rect-bounds
Open

fix: add bounds clamping in c_display::fill_rect() for negative coordinates#81
srpatcha wants to merge 3 commits into
idea4good:masterfrom
srpatcha:fix/display-fill-rect-bounds

Conversation

@srpatcha

Copy link
Copy Markdown

Fix buffer underflow from negative coordinates in c_display::fill_rect()

Problem

c_display::fill_rect() in src/core/display.h computes framebuffer offsets using y * _width + x0 without validating that coordinates are non-negative. Negative coordinates cause out-of-bounds memory writes to the framebuffer, potentially corrupting memory or causing crashes.

Root Cause

The child class c_surface::fill_rect() properly clamps coordinates, but the base class c_display::fill_rect() does not. While the inner loop checks x < _width and y < _height for upper bounds, it does not check for negative values. The framebuffer pointer arithmetic &((unsigned short*)m_phy_fb)[y * _width + x0] will produce an address before the buffer when x0 or y is negative.

Fix

Added bounds clamping at the start of the direct framebuffer path (after the driver delegation checks):

  • Clamp x0, y0 to minimum of 0
  • Clamp x1, y1 to maximum of width-1, height-1
  • Early return if the clamped rect is empty (x0 > x1 or y0 > y1)

Testing

  • Call fill_rect() with negative coordinates (e.g., fill_rect(-10, -5, 50, 50, color)) and verify no crash occurs.
  • Verify normal positive-coordinate rectangles still render correctly.

Impact

Affects any GuiLite application that passes negative coordinates to the base display class's fill_rect. This is a memory safety issue.

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.
@srpatcha
srpatcha force-pushed the fix/display-fill-rect-bounds branch from 638bec2 to 06a08dc Compare April 25, 2026 02:16
srpatcha and others added 2 commits April 25, 2026 01:25
…n support

- Add c_progress_bar class inheriting from c_wnd
- Support horizontal and vertical orientations
- Implement color gradient fill based on progress
- Add animated fill with configurable speed
- Include percentage text display
- Support completion callback
- Add min/max range with bounds clamping
- Add test file for widget validation

Bug-fix: Added bounds clamping for value setting

Signed-off-by: Srikanth Patchava <spatchava@meta.com>
The base draw_pixel() only checked if coordinates exceeded width/height,
but not if they were negative. Negative coordinates cause out-of-bounds
memory writes by indexing m_phy_fb at a negative offset.

Same class of bug as the recently-fixed fill_rect() in c_display.

Signed-off-by: Srikanth Patchava <spatchava@meta.com>
@srpatcha

Copy link
Copy Markdown
Author

Friendly ping — this adds bounds clamping in c_display::fill_rect() for negative coordinates to prevent out-of-bounds memory access. Ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant