Skip to content

fix: Increase PNGdec buffer size to support wide images#995

Merged
daveallie merged 4 commits intocrosspoint-reader:masterfrom
osteotek:fix-wide-png-crash
Feb 19, 2026
Merged

fix: Increase PNGdec buffer size to support wide images#995
daveallie merged 4 commits intocrosspoint-reader:masterfrom
osteotek:fix-wide-png-crash

Conversation

@osteotek
Copy link
Member

@osteotek osteotek commented Feb 19, 2026

Summary

  • Increased PNG_MAX_BUFFERED_PIXELS from 6402 to 16416 in platformio.ini to support up to 2048px wide RGBA images
  • adds a check to abort decoding and log an error if the required PNG scanline buffer exceeds the configured PNG_MAX_BUFFERED_PIXELS, preventing possible buffer overruns.
  • fixes [1.1.0-rc] PNG in EPUB causes crash #993

AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? < YES >

Copilot AI review requested due to automatic review settings February 19, 2026 15:26
@osteotek
Copy link
Member Author

osteotek commented Feb 19, 2026

Increasing PNG scanline buffer fixes crash on book from #993
Haven't tested beyond that though

@osteotek osteotek changed the title fix: Increase PNGdec buffer size to support wide EPUB images fix: Increase PNGdec buffer size to support wide images Feb 19, 2026

This comment was marked as outdated.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 19, 2026

📝 Walkthrough

Walkthrough

Adds runtime validation helpers to PNG decoding to prevent buffer overflows by computing required memory and checking against PNG_MAX_BUFFERED_PIXELS, while increasing the maximum buffered scanline width from 800px to 2048px in build configuration.

Changes

Cohort / File(s) Summary
PNG Decoder Buffer Validation
lib/Epub/Epub/converters/PngToFramebufferConverter.cpp
Introduces two internal helpers to compute memory requirements for PNG decoding: bytesPerPixelFromType() and requiredPngInternalBufferBytes(). Adds runtime guard to validate internal PNG decode buffer is large enough; logs errors, closes PNG, and aborts decoding if buffer exceeds PNG_MAX_BUFFERED_PIXELS.
Build Configuration
platformio.ini
Updates PNG scanline buffered width from 800px to 2048px, adjusting PNG_MAX_BUFFERED_PIXELS define from 6402 to 16416 with accompanying comment update.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • znelson
  • daveallie
  • jdk2pq
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately describes the main change: increasing PNG buffer size to support wider images, which directly addresses the crash issue from a buffer overflow.
Description check ✅ Passed The description is directly related to the changeset, explaining the buffer size increase and the guard logic added to prevent buffer overruns, with reference to the linked issue.
Linked Issues check ✅ Passed The code changes address issue #993 by increasing PNG_MAX_BUFFERED_PIXELS and adding a runtime guard to prevent internal buffer overflow when decoding wide PNG images, directly resolving the crash described in the linked issue.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the PNG buffer issue described in issue #993; no extraneous modifications or unrelated functionality changes are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@osteotek
Copy link
Member Author

The first issue being the PNG decode failing and the second one being the crash caused after the decode fails. There will most likely always be PNGs which are too big to fit, but that error should be handled gracefully.

@allgoewer Correct. Added buffer check, overruning buffer size will stop png decoding with log message

[92240] [ERR] [PNG] PNG row buffer too small: need 11234 bytes for width=1400 type=6, configured PNG_MAX_BUFFERED_PIXELS=6402
[92240] [ERR] [PNG] Aborting decode to avoid PNGdec internal buffer overflow

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 103fac2 and 5662e27.

📒 Files selected for processing (2)
  • lib/Epub/Epub/converters/PngToFramebufferConverter.cpp
  • platformio.ini
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (2)
lib/Epub/Epub/converters/PngToFramebufferConverter.cpp (2)

93-117: Clear, focused sizing helpers.

The per-pixel-type sizing and internal-buffer requirement calculation are clean and easy to reason about.


333-343: Good safety gate before decode.

The preflight check cleanly prevents PNGdec internal buffer overruns before the callback runs.

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@platformio.ini`:
- Around line 34-36: The PNG buffer constant in platformio.ini is too low for
2048px RGBA images and causes the guard in PngToFramebufferConverter.cpp (around
the check at line 335) to reject valid images; update the
-DPNG_MAX_BUFFERED_PIXELS definition to at least 16418 (preferably 16450 or a
small safety margin) so the computed requirement ((2048*4)+1)*2+32 is satisfied,
then rebuild so the preprocessor macro used by PngToFramebufferConverter.cpp
reflects the new limit.

Comment on lines +34 to +36
# Increase PNG scanline buffer to support up to 2048px wide images
# Default is (320*4+1)*2=2562, we need more for larger images
-DPNG_MAX_BUFFERED_PIXELS=6402
-DPNG_MAX_BUFFERED_PIXELS=16416
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# Locate key files
fd -i platformio\.ini
fd -i "PngToFramebufferConverter" --type f
rg -l "PNG_MAX_BUFFERED_PIXELS" -t cpp -t ini

Repository: crosspoint-reader/crosspoint-reader

Length of output: 246


🏁 Script executed:

# Read platformio.ini around lines 34-36
cat -n platformio.ini | sed -n '30,40p'

Repository: crosspoint-reader/crosspoint-reader

Length of output: 459


🏁 Script executed:

# Read PngToFramebufferConverter.cpp to find the guard
cat -n lib/Epub/Epub/converters/PngToFramebufferConverter.cpp

Repository: crosspoint-reader/crosspoint-reader

Length of output: 15427


🏁 Script executed:

# Search for PNG_MAX_BUFFERED_PIXELS definition and usage
rg "PNG_MAX_BUFFERED_PIXELS" -B 2 -A 2

Repository: crosspoint-reader/crosspoint-reader

Length of output: 2400


🏁 Script executed:

# Search for PNGdec.h
fd -i "pngdec" -o -i "png.h"

Repository: crosspoint-reader/crosspoint-reader

Length of output: 195


PNG_MAX_BUFFERED_PIXELS=16416 is insufficient for advertised 2048px RGBA support.

The guard in PngToFramebufferConverter.cpp line 335 will reject 2048px RGBA images, which require 16,418 bytes (((2048*4)+1)*2+32), while the constant is set to 16,416 bytes. Raise the limit to at least 16,418:

🔧 Suggested adjustment
-  -DPNG_MAX_BUFFERED_PIXELS=16416
+  -DPNG_MAX_BUFFERED_PIXELS=16418
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@platformio.ini` around lines 34 - 36, The PNG buffer constant in
platformio.ini is too low for 2048px RGBA images and causes the guard in
PngToFramebufferConverter.cpp (around the check at line 335) to reject valid
images; update the -DPNG_MAX_BUFFERED_PIXELS definition to at least 16418
(preferably 16450 or a small safety margin) so the computed requirement
((2048*4)+1)*2+32 is satisfied, then rebuild so the preprocessor macro used by
PngToFramebufferConverter.cpp reflects the new limit.

@daveallie daveallie merged commit d461d93 into crosspoint-reader:master Feb 19, 2026
5 checks passed
daveallie pushed a commit that referenced this pull request Feb 19, 2026
## Summary

* Increased `PNG_MAX_BUFFERED_PIXELS` from 6402 to 16416 in
`platformio.ini` to support up to 2048px wide RGBA images
* adds a check to abort decoding and log an error if the required PNG
scanline buffer exceeds the configured `PNG_MAX_BUFFERED_PIXELS`,
preventing possible buffer overruns.
* fixes
#993

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**< YES >**_
saslv pushed a commit to saslv/crosspoint-reader that referenced this pull request Feb 19, 2026
…eader#995)

## Summary

* Increased `PNG_MAX_BUFFERED_PIXELS` from 6402 to 16416 in
`platformio.ini` to support up to 2048px wide RGBA images
* adds a check to abort decoding and log an error if the required PNG
scanline buffer exceeds the configured `PNG_MAX_BUFFERED_PIXELS`,
preventing possible buffer overruns.
* fixes
crosspoint-reader#993

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**< YES >**_
@martinbrook
Copy link
Contributor

@osteotek what was the png size and type which was causing the crash ? Maybe I can add another page to the test epub generator (https://github.com/crosspoint-reader/crosspoint-reader/blob/master/scripts/generate_test_epub.py) so we can catch any future regressions ?

@osteotek
Copy link
Member Author

osteotek commented Feb 19, 2026

@martinbrook PNG with width=1400, it's on the first page of this epub - https://standardebooks.org/ebooks/dashiell-hammett/the-maltese-falcon/downloads/dashiell-hammett_the-maltese-falcon.epub

el pushed a commit to el/crosspoint-reader that referenced this pull request Feb 19, 2026
…eader#995)

## Summary

* Increased `PNG_MAX_BUFFERED_PIXELS` from 6402 to 16416 in
`platformio.ini` to support up to 2048px wide RGBA images
* adds a check to abort decoding and log an error if the required PNG
scanline buffer exceeds the configured `PNG_MAX_BUFFERED_PIXELS`,
preventing possible buffer overruns.
* fixes
crosspoint-reader#993

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**< YES >**_
@osteotek
Copy link
Member Author

@martinbrook I may have fixed it incorrectly, I haven't been really following how images subsystems work. Please feel free to fix it properly, if needed.

lukestein pushed a commit to lukestein/crosspoint-reader that referenced this pull request Feb 20, 2026
…eader#995)

## Summary

* Increased `PNG_MAX_BUFFERED_PIXELS` from 6402 to 16416 in
`platformio.ini` to support up to 2048px wide RGBA images
* adds a check to abort decoding and log an error if the required PNG
scanline buffer exceeds the configured `PNG_MAX_BUFFERED_PIXELS`,
preventing possible buffer overruns.
* fixes
crosspoint-reader#993

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? _**< YES >**_
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.1.0-rc] PNG in EPUB causes crash

4 participants