-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix GH-15980: Signed integer overflow in main/streams/streams.c #15989
Conversation
We need to avoid signed integer overflows which are undefined behavior. We catch that, and set `offset` to `ZEND_LONG_MAX` (which is also the largest value of `zend_off_t` on all platforms). Of course, after such a seek a stream is no longer readable, but that matches the current behavior for offsets near `ZEND_LONG_MAX`.
if (UNEXPECTED(offset > ZEND_LONG_MAX - stream->position)) { | ||
offset = ZEND_LONG_MAX; | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long term we probably should warn about those large offsets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, although it's somewhat hard to determine what is too large. Something like ZEND_LONG_MAX
- chunk size may work. (I'm mostly thinking about 32bit systems.)
The new test case fails on 64bit Linux, so this needs closer investigation; for now I have reverted the commits. |
For some OS/FS, `fseek()` may fail, so `ftell()` will obviously report the old position.
We need to avoid signed integer overflows which are undefined behavior. We catch that, and set
offset
toZEND_LONG_MAX
(which is also the largest value ofzend_off_t
on all platforms). Of course, after such a seek a stream is no longer readable, but that matches the current behavior for offsets nearZEND_LONG_MAX
.