Skip to content

Avoid crash for reset/end/next/prev() on ffi classes #9711

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

Merged
merged 2 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ PHP NEWS
. Fixed bug GH-10292 (Made the default value of the first param of srand() and
mt_srand() unknown). (kocsismate)
. Fix incorrect check in cs_8559_5 in map_from_unicode(). (nielsdos)
. Fix bug GH-9697 for reset/end/next/prev() attempting to move pointer of
properties table for certain internal classes such as FFI classes

02 Feb 2023, PHP 8.1.15

Expand Down
20 changes: 20 additions & 0 deletions ext/ffi/tests/arrayPointer.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
FFI: Test deprecated use of array helper functions on FFI classes doesn't crash
--EXTENSIONS--
ffi
--INI--
ffi.enable=1
--FILE--
<?php
error_reporting(E_ALL & ~E_DEPRECATED);
$data = FFI::new('int');
var_dump(reset($data));
var_dump(end($data));
var_dump(next($data));
var_dump(prev($data));
?>
--EXPECTF--
bool(false)
bool(false)
bool(false)
bool(false)
16 changes: 16 additions & 0 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,10 @@ PHP_FUNCTION(end)
ZEND_PARSE_PARAMETERS_END();

HashTable *array = get_ht_for_iap(array_zv, /* separate */ true);
if (zend_hash_num_elements(array) == 0) {
/* array->nInternalPointer is already 0 if the array is empty, even after removing elements */
RETURN_FALSE;
}
zend_hash_internal_pointer_end(array);

if (USED_RET()) {
Expand Down Expand Up @@ -1100,6 +1104,10 @@ PHP_FUNCTION(prev)
ZEND_PARSE_PARAMETERS_END();

HashTable *array = get_ht_for_iap(array_zv, /* separate */ true);
if (zend_hash_num_elements(array) == 0) {
/* array->nInternalPointer is already 0 if the array is empty, even after removing elements */
RETURN_FALSE;
}
zend_hash_move_backwards(array);

if (USED_RET()) {
Expand Down Expand Up @@ -1127,6 +1135,10 @@ PHP_FUNCTION(next)
ZEND_PARSE_PARAMETERS_END();

HashTable *array = get_ht_for_iap(array_zv, /* separate */ true);
if (zend_hash_num_elements(array) == 0) {
/* array->nInternalPointer is already 0 if the array is empty, even after removing elements */
RETURN_FALSE;
}
zend_hash_move_forward(array);

if (USED_RET()) {
Expand Down Expand Up @@ -1154,6 +1166,10 @@ PHP_FUNCTION(reset)
ZEND_PARSE_PARAMETERS_END();

HashTable *array = get_ht_for_iap(array_zv, /* separate */ true);
if (zend_hash_num_elements(array) == 0) {
/* array->nInternalPointer is already 0 if the array is empty, even after removing elements */
RETURN_FALSE;
}
zend_hash_internal_pointer_reset(array);

if (USED_RET()) {
Expand Down