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:
@@ -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
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user