Skip to content

extmod/modframebuf: Fix 16-bit integer overflow in pixel index arithmetic. - #19586

Closed
Yudis-bit wants to merge 1 commit into
micropython:masterfrom
Yudis-bit:fix/modframebuf-16bit-overflow
Closed

extmod/modframebuf: Fix 16-bit integer overflow in pixel index arithmetic.#19586
Yudis-bit wants to merge 1 commit into
micropython:masterfrom
Yudis-bit:fix/modframebuf-16bit-overflow

Conversation

@Yudis-bit

Copy link
Copy Markdown

Problem

On platforms with 16-bit int, the expression x + y * fb->stride overflows when y >= 128 with a 256-wide framebuffer, causing out-of-bounds reads and writes. Fixes #19431.

Fix

Cast x and y to size_t in all 12 pixel-index expressions in the RGB565, GS2, GS4, and GS8 pixel functions. The two existing size_t declarations (h/v line drawing) already handled promotion correctly.

Verification

No behavioral change on 32-bit-int platforms. On 16-bit-int platforms, the cast prevents overflow for all framebuffer sizes up to SIZE_MAX.

Signed-off-by: Yudistira Putra 85178972+Yudis-bit@users.noreply.github.com

…etic.

On platforms with 16-bit int, the expression x + y * fb->stride
overflows when y >= 128 with a 256-wide framebuffer, causing
out-of-bounds reads and writes. Cast x and y to size_t in all
pixel-index expressions to force promotion to a type wide enough
for the full framebuffer address space.

The two existing size_t index declarations (horizontal line and
vertical line drawing) already handled the promotion correctly;
this change covers the 12 remaining expressions in the RGB565,
GS2_HMSB, GS4_HMSB, and GS8 pixel functions.

Fixes: micropython#19431
Signed-off-by: Yudistira Putra <85178972+Yudis-bit@users.noreply.github.com>
@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.59%. Comparing base (06bcfd5) to head (8254c55).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19586   +/-   ##
=======================================
  Coverage   98.59%   98.59%           
=======================================
  Files         182      182           
  Lines       23299    23299           
  Branches        5        5           
=======================================
  Hits        22971    22971           
  Misses        327      327           
  Partials        1        1           
Flag Coverage Δ
unix-coverage-32bit 98.59% <100.00%> (ø)
unix-coverage-64bit 98.52% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Aug 7, 2026
@dpgeorge

dpgeorge commented Aug 7, 2026

Copy link
Copy Markdown
Member

Thanks for the contribution, but:

  • you need to commit and sign-off with a valid email address
  • you need to fill in the PR template properly

@dpgeorge dpgeorge closed this Aug 7, 2026
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

Code size report:

Reference:  webassembly/main: Fix unused-variable build error in standard. [06bcfd5]
Comparison: extmod/modframebuf: Fix 16-bit integer overflow in pixel index arithmetic. [merge of 8254c55]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   +48 +0.006% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

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

Labels

extmod Relates to extmod/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extmod/modframebuf.c: pixel index arithmetic overflows on 16-bit int targets (out-of-bounds writes)

2 participants