Skip to content

Refuse a zero width in the simplified write API, and share its row stride validation with the read API - #962

Open
akashdeshmukh-git wants to merge 2 commits into
pnggroup:libpng18from
akashdeshmukh-git:fix-simplified-write-zero-width
Open

akashdeshmukh-git wants to merge 2 commits into
pnggroup:libpng18from
akashdeshmukh-git:fix-simplified-write-zero-width

Conversation

@akashdeshmukh-git

Copy link
Copy Markdown

png_image_write_to_memory (and the other png_image_write_ APIs) divide by zero when image->width is 0 and row_stride is 0: the absolute row stride is then zero and the 32-bit buffer size check divides by it, which crashes the application (SIGFPE on x86) before png_set_IHDR can report the invalid width. The first commit fixes this and adds a pngstride test that checks the validation of the image size and row stride in both the simplified read and write APIs.
The second commit moves that validation, which existed as two copies in png_image_finish_read and png_image_write_main, into one internal function used by both. The copies had drifted apart (see 0b08768 and the first commit), and sharing them keeps the read and write APIs enforcing the same limits on the buffers they access. Both sides now report the same error messages.
The libpng16 branch has the same division by zero; the first commit should cherry-pick cleanly onto it.

In `png_image_write_main`, a zero `image->width` with a zero
`row_stride` made the absolute row stride zero, and the check of the
buffer size against 32 bits then divided by it, crashing the
application (SIGFPE on x86) before `png_set_IHDR` could report the
invalid width.

Add the pngstride test, which checks the validation of the image size
and of the row stride in both the simplified read and write APIs.
`png_image_finish_read` and `png_image_write_main` each had their own
copy of the code that defaults the `row_stride` argument and checks the
image size and the buffer size against the limits of the simplified
API. The two copies drifted apart: the write side checked the buffer
size against the minimum row stride and without the component size,
until commit 0b08768, and it divided
by zero on a zero width, until the previous commit.

Move this validation to a single internal function,
`png_image_check_row_stride`, used by both sides, so that the read and
write APIs enforce the same limits on the buffers they access. Both
sides now report the same error messages. The function negates the
row stride with `0U -`, which does not trigger MSVC warning C4146.

@jbowler jbowler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ctruta: reject, unnecessary, extra code, cannot arise in a correctly written app and will hide the app error because the png_error message causes no hard failure can can be overwritten width subsequent calls to the API if the app ignores the error return.

Comment thread pngwrite.c Outdated
* zero, and the size check below would divide by zero.
*/
if (image->width == 0)
png_error(image->opaque->png_ptr, "image width is zero");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a clear application error and one that the app should not be making. "png_error" can be ignored and the app can and probably will proceed merrily, merrily, merrily, merrily living in a dream, as the song says.

@jbowler

jbowler commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Application programmers need to debug their own code. It is not appropriate for libpng to do this; already many warnings inside the library are simply ignored and the simplified API makes this easy as it does not crash on png_error, it just returns ignorable return code. [[nodiscard]] might help, but not this.

@akashdeshmukh-git

Copy link
Copy Markdown
Author

Thanks for the feedback, @jbowler.

Just to clarify the exact issue this PR addresses:

When an application passes width = 0 (or row_stride = 0) to png_image_write_to_memory or other simplified write APIs, stride evaluates to 0. The 32-bit buffer size calculation then divides by stride:

if (buffer_size / stride < image->height)

On x86/x86_64, an integer division by zero generates a hardware trap (SIGFPE), crashing the host process immediately before png_error or png_set_IHDR can execute, and bypassing any application-level error handling.

While application authors should certainly validate their inputs, a fundamental C library should ideally avoid hardware fault traps on invalid parameters passed through its public API. Returning 0 and recording PNG_IMAGE_FLAG_ERROR allows host applications, web servers, and language bindings to handle bad input cleanly without crashing.

Furthermore, png_image_finish_read() already contains this exact zero-width and row stride validation logic. The second commit in this PR simply shares that validation between the read and write paths in an internal helper, ensuring both APIs enforce the same buffer constraints consistently without duplicating code.

@jbowler

jbowler commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

#923
#925

@ctruta - this is weird; someone, somewhere wants to change this code for some reason...

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.

2 participants