Skip to content

Fix FFI::memcpy(), FFI::memcmp() and FFI::memset() accepting a negative size - #25827

Open
marc-mabe wants to merge 1 commit into
php:PHP-8.4from
marc-mabe:fix/ffi-mem-negative-size
Open

marc-mabe wants to merge 1 commit into
php:PHP-8.4from
marc-mabe:fix/ffi-mem-negative-size

Conversation

@marc-mabe

Copy link
Copy Markdown
Contributor

Follow-up to GH-25731, which rejected a negative size in FFI::string(). FFI::memcpy(), FFI::memcmp() and FFI::memset() have the same issue: the size was only compared with the size of non-pointer CData and the length of strings, where a negative size became a huge unsigned one and failed with a misleading data or string boundary error. For pointers, it was passed on to memcpy(), memcmp() or memset() as nearly SIZE_MAX, which crashed.

Reject a negative size with a ValueError, like FFI::string() does.

As GH-25731 is not released yet, its NEWS entry now covers these functions too, and string_negative_size.phpt becomes part of mem_size.phpt, which tests the sizes of all four functions.

…ve size

Follow-up to phpGH-25731, which rejected a negative size in FFI::string().
FFI::memcpy(), FFI::memcmp() and FFI::memset() have the same issue: the
size was only compared with the size of non-pointer CData and the length
of strings, where a negative size became a huge unsigned one and failed
with a misleading data or string boundary error. For pointers, it was
passed on to memcpy(), memcmp() or memset() as nearly SIZE_MAX, which
crashed.

Reject a negative size with a ValueError, like FFI::string() does.

As phpGH-25731 is not released yet, its NEWS entry now covers these
functions too, and string_negative_size.phpt becomes part of
mem_size.phpt, which tests the sizes of all four functions.
@marc-mabe
marc-mabe requested a review from dstogov as a code owner November 27, 2026 09:09
@marc-mabe
marc-mabe changed the base branch from master to PHP-8.4 November 27, 2026 09:14
@marc-mabe

Copy link
Copy Markdown
Contributor Author

ping @ndossche as you reviewed and merged #25731

@LamentXU123

Copy link
Copy Markdown
Member

I have some concerns on the target branch tho. Although the previous PR get merged, I still think this is quite a behavioral break in stable branches. As far as I can tell, we've done a bunch of fixes to reject NUL bytes by throwing ValueError in functions that have NUL truncation bugs. what is the reason do you think this fix is special enough to go to stable?

@marc-mabe

Copy link
Copy Markdown
Contributor Author

@LamentXU123

failed with a misleading data or string boundary error. For pointers, it was passed on to memcpy(), memcmp() or memset() as nearly SIZE_MAX, which crashed.

I see this as a bug fix. No more reasoning behind. I personally would be fine with master as well.

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.

3 participants