Skip to content

Commit 9bf9232

Browse files
committed
fix(array): fix null dereference in iterator
Refs: #357
1 parent 9ffec5c commit 9bf9232

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/types/array.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,11 @@ impl<'a> Iter<'a> {
739739
)
740740
};
741741

742-
if key_type == -1 {
742+
// Key type `-1` is ???
743+
// Key type `1` is string
744+
// Key type `2` is long
745+
// Key type `3` is null meaning the end of the array
746+
if key_type == -1 || key_type == 3 {
743747
return None;
744748
}
745749

@@ -753,10 +757,16 @@ impl<'a> Iter<'a> {
753757
);
754758
}
755759
let value = unsafe {
756-
&*zend_hash_get_current_data_ex(
760+
let val_ptr = zend_hash_get_current_data_ex(
757761
self.ht as *const ZendHashTable as *mut ZendHashTable,
758762
&mut self.pos as *mut HashPosition,
759-
)
763+
);
764+
765+
if val_ptr.is_null() {
766+
return None;
767+
}
768+
769+
&*val_ptr
760770
};
761771

762772
if !key.is_long() && !key.is_string() {

tests/src/integration/array.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
require('_utils.php');
44

55
// Tests sequential arrays
6-
$array = test_array(['a', 'b', 'c']);
6+
$array = test_array(['a', 'b', 'c', 'd']);
7+
unset($array[2]);
78

89
assert(is_array($array));
910
assert(count($array) === 3);
1011
assert(in_array('a', $array));
1112
assert(in_array('b', $array));
12-
assert(in_array('c', $array));
13+
assert(in_array('d', $array));
1314

1415
// Tests associative arrays
1516
$assoc = test_array_assoc([

tests/src/integration/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#[test]
2-
fn binary_works() {
2+
fn array_works() {
33
assert!(crate::integration::run_php("array.php"));
44
}

0 commit comments

Comments
 (0)