Skip to content

Fix GH-18480: array_splice overflow on array length with offset. #18483

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

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 6 additions & 6 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -3252,17 +3252,17 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H

/* If hash for removed entries exists, go until offset+length and copy the entries to it */
if (removed != NULL) {
for ( ; pos < offset + length && idx < in_hash->nNumUsed; idx++, entry++) {
for ( ; pos - offset < length && idx < in_hash->nNumUsed; idx++, entry++) {
if (Z_TYPE_P(entry) == IS_UNDEF) continue;
pos++;
Z_TRY_ADDREF_P(entry);
zend_hash_next_index_insert_new(removed, entry);
zend_hash_packed_del_val(in_hash, entry);
}
} else { /* otherwise just skip those entries */
int pos2 = pos;
zend_long pos2 = pos;

for ( ; pos2 < offset + length && idx < in_hash->nNumUsed; idx++, entry++) {
for ( ; pos2 - offset < length && idx < in_hash->nNumUsed; idx++, entry++) {
if (Z_TYPE_P(entry) == IS_UNDEF) continue;
pos2++;
zend_hash_packed_del_val(in_hash, entry);
Expand Down Expand Up @@ -3317,7 +3317,7 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H

/* If hash for removed entries exists, go until offset+length and copy the entries to it */
if (removed != NULL) {
for ( ; pos < offset + length && idx < in_hash->nNumUsed; idx++, p++) {
for ( ; pos - offset < length && idx < in_hash->nNumUsed; idx++, p++) {
if (Z_TYPE(p->val) == IS_UNDEF) continue;
pos++;
entry = &p->val;
Expand All @@ -3330,9 +3330,9 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H
zend_hash_del_bucket(in_hash, p);
}
} else { /* otherwise just skip those entries */
int pos2 = pos;
zend_long pos2 = pos;

for ( ; pos2 < offset + length && idx < in_hash->nNumUsed; idx++, p++) {
for ( ; pos2 - offset < length && idx < in_hash->nNumUsed; idx++, p++) {
if (Z_TYPE(p->val) == IS_UNDEF) continue;
pos2++;
zend_hash_del_bucket(in_hash, p);
Expand Down
40 changes: 40 additions & 0 deletions ext/standard/tests/array/gh18480.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
GH-18480 (array_splice overflow with large offset / length values)
--FILE--
<?php

foreach ([PHP_INT_MIN, PHP_INT_MAX] as $length) {
$a = [PHP_INT_MAX];
$offset = PHP_INT_MAX;
var_dump(array_splice($a,$offset, $length));
$a = [PHP_INT_MAX];
$offset = PHP_INT_MIN;
var_dump(array_splice($a,$offset, $length));
$a = ["a" => PHP_INT_MAX];
$offset = PHP_INT_MAX;
var_dump(array_splice($a,$offset, $length));
$a = ["a" => PHP_INT_MAX];
$offset = PHP_INT_MIN;
var_dump(array_splice($a,$offset, $length));
}
--EXPECTF--
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(1) {
[0]=>
int(%d)
}
array(0) {
}
array(1) {
["a"]=>
int(%d)
}
Loading