Fix buffer overrun in Paint_Clear() at Scale 65 - #15
Open
timmg wants to merge 1 commit into
Open
Conversation
The inner loop was bounded by Paint.WidthByte, which Paint_SetScale(65) sets to WidthMemory*2 - a byte count, not a pixel count. Combined with the Addr = X*2 below it, each row wrote 1280 bytes on a 640-byte stride (at width 320), so the final row ran 2*width bytes past the end of the caller's buffer. The overrun happens for every width and height, on every call. Bounding the loop by Paint.WidthMemory counts pixels, which is what the body expects, and still covers the buffer exactly with no gaps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Paint_Clear()writes2 * widthbytes past the end of the caller's buffer wheneverPaint_SetScale(65)is in use — on every call, at every width and height.Paint_SetScale()setsPaint.WidthByte = Paint.WidthMemory*2for scale 65, so it holds a byte count (640 for a 320px panel). The inner loop ofPaint_Clear()used it as a pixel count, and the body then multiplies by 2 again to form the byte offset. Each row therefore writes 1280 bytes on a 640-byte stride, and the last row runs off the end:This changes the bound to
Paint.WidthMemory, which counts pixels — matching what the loop body expects. One line.Verification
A standalone reproduction (no SDK needed, code in the linked issue) reports, before the change:
and with an exactly-sized buffer under AddressSanitizer:
After the change: 0 bytes past the end at every size, ASan clean, and the visible buffer is still covered exactly — verified 0 of 110080 bytes left unwritten, no gaps and no change to the rendered result.
Impact
Waveshare's own examples
malloc()the framebuffer, so the overrun usually lands in heap slack and produces nothing visible. It becomes serious when the framebuffer is a static array. On an RP2350 board the linker placed TinyUSB'shw_endpoints[]directly after ours, so every redraw destroyed EP0: the board asserted its D+ pull-up but could never answer a SETUP packet, and never appeared on the USB bus at all.🤖 Generated with Claude Code
Fixes #16 — that issue has the full write-up and a standalone reproduction.