Fix buffer overflow in ucPixels for RGBA images at max width#43
Open
PE5PVB wants to merge 1 commit intobitbank2:masterfrom
Open
Fix buffer overflow in ucPixels for RGBA images at max width#43PE5PVB wants to merge 1 commit intobitbank2:masterfrom
PE5PVB wants to merge 1 commit intobitbank2:masterfrom
Conversation
DecodePNG() aligns pCurr and pPrev to 16-byte boundaries within ucPixels, adding up to 15 bytes of padding per line. The default PNG_MAX_BUFFERED_PIXELS value did not account for this alignment overhead, causing a buffer overflow into ucFileBuf when decoding RGBA (color type 6) images at the maximum supported width (320px on Arduino, 2048px on Linux/Mac). For a 320px wide RGBA image: - iPitch = 1280, each line needs 1281 bytes (iPitch + filter byte) - With alignment: 15 + 1281 + 15 + 1281 = 2592 bytes needed - Old buffer size: ((320*4+1)*2) = 2562 bytes (30 bytes short) The overflow corrupts ucFileBuf (the file read buffer immediately following ucPixels in the PNGIMAGE struct), causing zlib decompression errors after a few scanlines. This results in only the top portion of the image being rendered. Adding 16 bytes per line to the default accounts for worst-case alignment padding. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
DecodePNG()inpng.inlalignspCurrandpPrevto 16-byte boundaries withinucPixels, adding up to 15 bytes of padding per line. The defaultPNG_MAX_BUFFERED_PIXELSvalue does not account for this alignment overhead, causing a buffer overflow intoucFileBufwhen decoding RGBA (color type 6) images at the maximum supported width.Details
The alignment code in
DecodePNG()(png.inl lines 776-782):For a 320px wide RGBA image (Arduino default):
((320*4+1)*2)The 30-byte overflow writes past
ucPixelsintoucFileBuf(the file read buffer immediately following in thePNGIMAGEstruct). This corrupts the buffered file data, causing zlib decompression errors after a few scanlines.Symptoms
Fix
Add 16 bytes per line to the default
PNG_MAX_BUFFERED_PIXELSto account for worst-case 16-byte alignment padding:The same fix is applied to the Linux/Mac default (2048px width).
The RAM increase is only 32 bytes, which is negligible.