getimagesize(): undefined behavior in BMP handler for biHeight 0x80000000 (abs of INT32_MIN), and the two outputs disagree
Description
php_handle_bmp() in ext/standard/image.c (verified in the php-8.6.0 tag, the code is old and not new in 8.6.0) computes the BMP height as:
result->height = abs((int32_t)result->height);
For a BMP whose BITMAPINFOHEADER biHeight field is 0x80000000 (INT32_MIN), abs(INT32_MIN) is undefined behavior in C. A UBSan build reports:
runtime error: negation of -2147483648 cannot be represented in type 'int'
On a plain build the function also returns self-inconsistent results: the numeric height and the attribute string disagree.
This is a correctness bug, not a security issue: there is no out-of-bounds access and ASan reports nothing. Filing it as a normal bug.
Reproduction
<?php
$bmp = 'BM' . pack('V', 54) . pack('v', 0) . pack('v', 0) . pack('V', 54)
. pack('V', 40) // BITMAPINFOHEADER size
. pack('V', 1) // biWidth
. pack('V', 0x80000000) // biHeight = INT32_MIN
. pack('v', 1) . pack('v', 24)
. str_repeat("\0", 24);
file_put_contents('/tmp/test.bmp', $bmp);
var_dump(getimagesize('/tmp/test.bmp'));
Expected
One consistent height in both outputs (or the file rejected).
Actual (PHP 8.6.0, plain build)
[0] => int(1)
[1] => int(2147483648)
[3] => string(...) "width="1" height="-2147483648""
The numeric value says 2147483648 while the attribute string says -2147483648.
Related, same root area: the [3] attribute string prints the unsigned dimensions with %d, so any width or height above INT_MAX (PNG as well, not only BMP) shows as negative there.
Suggested fix
Negate in unsigned arithmetic instead of calling abs() on a signed value, for example:
int32_t h = (int32_t)result->height;
result->height = h < 0 ? 0u - (uint32_t)h : (uint32_t)h;
or reject INT32_MIN explicitly.
Verification
The root cause was confirmed in the php-8.6.0 source of ext/standard/image.c, and the exact expression was reproduced under UBSan (negation of -2147483648 cannot be represented in type 'int'). The end-to-end behaviour above was reproduced on PHP 8.6.0 (plain and ASan/UBSan builds) during the audit that found this. This report was prepared with AI assistance; the claims in it were verified against the source before filing.
This looks like a duplicate of GH-25948. PR #25952 already fixes it.