Repository navigation
Refuse a zero width in the simplified write API, and share its row stride validation with the read API - #962
Conversation
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
left a comment
There was a problem hiding this comment.
@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.
| * zero, and the size check below would divide by zero. | ||
| */ | ||
| if (image->width == 0) | ||
| png_error(image->opaque->png_ptr, "image width is zero"); |
There was a problem hiding this comment.
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.
|
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 |
|
Thanks for the feedback, @jbowler. Just to clarify the exact issue this PR addresses: When an application passes if (buffer_size / stride < image->height)On x86/x86_64, an integer division by zero generates a hardware trap ( 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 Furthermore, |
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.