Skip to content

Fix zend_inference: fix unsound shift_left_overflows range when shift… - #24994

Closed
mkrishna793 wants to merge 2 commits into
php:PHP-8.4from
mkrishna793:fix-shift-left-overflow-8.4
Closed

mkrishna793 wants to merge 2 commits into
php:PHP-8.4from
mkrishna793:fix-shift-left-overflow-8.4

Conversation

@mkrishna793

Copy link
Copy Markdown
Contributor

Summary of Changes

In Zend/Optimizer/zend_inference.c, shift_left_overflows(n, s) previously checked safe_shift_left(n, s) > n for negative inputs. When (n << s) wrapped around to a value <= n (such as ZEND_LONG_MIN or negative wrapped values), the check falsely returned false (no overflow). Positive shifts also missed overflow when wrapping around (e.g. 7041795614029497201 << 6).

This PR replaces the heuristic with a round-trip arithmetic shift check ((shifted >> s) != n) that soundly detects overflow across all 64-bit and 32-bit inputs.

Includes the regression test provided by @arnaud-lb in Zend/tests/shift_left_overflow_range.phpt.

… wraps

shift_left_overflows() previously checked 'safe_shift_left(n, s) > n' for
negative numbers, which incorrectly returned false when (n << s) wrapped
around to a value <= n (such as ZEND_LONG_MIN or negative wrapped values).
Positive shifts also missed overflow when wrapping.

Use a round-trip arithmetic shift check ((shifted >> s) != n) to soundly
detect overflow across all inputs.
@arnaud-lb

Copy link
Copy Markdown
Member

Merged in 8df212a.

Thank you!

@arnaud-lb arnaud-lb closed this Oct 28, 2026
@mkrishna793

Copy link
Copy Markdown
Contributor Author

thank you so much for merging my commit

Merged in 8df212a.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants