Skip to content

Commit

Permalink
fix(array): fix null dereference in iterator (#358)
Browse files Browse the repository at this point in the history
Refs: #357
  • Loading branch information
Xenira authored Feb 6, 2025
1 parent 9ffec5c commit 0427669
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,11 @@ impl<'a> Iter<'a> {
)
};

if key_type == -1 {
// Key type `-1` is ???
// Key type `1` is string
// Key type `2` is long
// Key type `3` is null meaning the end of the array
if key_type == -1 || key_type == 3 {
return None;
}

Expand All @@ -753,10 +757,16 @@ impl<'a> Iter<'a> {
);
}
let value = unsafe {
&*zend_hash_get_current_data_ex(
let val_ptr = zend_hash_get_current_data_ex(
self.ht as *const ZendHashTable as *mut ZendHashTable,
&mut self.pos as *mut HashPosition,
)
);

if val_ptr.is_null() {
return None;
}

&*val_ptr
};

if !key.is_long() && !key.is_string() {
Expand Down
5 changes: 3 additions & 2 deletions tests/src/integration/array.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
require('_utils.php');

// Tests sequential arrays
$array = test_array(['a', 'b', 'c']);
$array = test_array(['a', 'b', 'c', 'd']);
unset($array[2]);

assert(is_array($array));
assert(count($array) === 3);
assert(in_array('a', $array));
assert(in_array('b', $array));
assert(in_array('c', $array));
assert(in_array('d', $array));

// Tests associative arrays
$assoc = test_array_assoc([
Expand Down
2 changes: 1 addition & 1 deletion tests/src/integration/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[test]
fn binary_works() {
fn array_works() {
assert!(crate::integration::run_php("array.php"));
}

0 comments on commit 0427669

Please sign in to comment.