refactor: Avoid undefined behavior (signed int overflow) in negations

The computation of `abs(x)` (where the type of `x` is `png_int_32`)
should involve a cast to `png_uint_32` before negating `x`, ensuring
that the result is well-defined even for `INT32_MIN`.

Considering that the PNG Specification (and, implicitly, libpng)
formally prohibits `INT32_MIN` values, this is merely a zero-cost form
of hardening intended to appease UBSan, and not an actual bug fix.
This commit is contained in:
Cosmin Truta
2026-03-23 21:13:56 +02:00
parent a625147f66
commit 9929ba276e
2 changed files with 2 additions and 2 deletions
+1 -1
View File
@@ -4111,7 +4111,7 @@ png_image_finish_read(png_imagep image, png_const_colorp background,
row_stride = (png_int_32)/*SAFE*/png_row_stride;
if (row_stride < 0)
check = (png_uint_32)(-row_stride);
check = -(png_uint_32)row_stride;
else
check = (png_uint_32)row_stride;
+1 -1
View File
@@ -2030,7 +2030,7 @@ png_image_write_main(png_voidp argument)
display->row_stride = (png_int_32)/*SAFE*/png_row_stride;
if (display->row_stride < 0)
check = (png_uint_32)(-display->row_stride);
check = -(png_uint_32)display->row_stride;
else
check = (png_uint_32)display->row_stride;