Skip to content

Commit 8a58585

Browse files
committed
Fix GH-18480: array_splice overflow on array length with offset.
close GH-18483
1 parent 0227d96 commit 8a58585

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.3.22
44

5+
- Core:
6+
. Fixed GH-18480 (array_splice with large values for offset/length arguments).
7+
(nielsdos/David Carlier)
8+
59
- Curl:
610
. Fixed GH-18460 (curl_easy_setopt with CURLOPT_USERPWD/CURLOPT_USERNAME/
711
CURLOPT_PASSWORD set the Authorization header when set to NULL).

ext/standard/array.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -3252,17 +3252,17 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H
32523252

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

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

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

3335-
for ( ; pos2 < offset + length && idx < in_hash->nNumUsed; idx++, p++) {
3335+
for ( ; pos2 - offset < length && idx < in_hash->nNumUsed; idx++, p++) {
33363336
if (Z_TYPE(p->val) == IS_UNDEF) continue;
33373337
pos2++;
33383338
zend_hash_del_bucket(in_hash, p);

ext/standard/tests/array/gh18480.phpt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
GH-18480 (array_splice overflow with large offset / length values)
3+
--FILE--
4+
<?php
5+
6+
foreach ([PHP_INT_MIN, PHP_INT_MAX] as $length) {
7+
$a = [PHP_INT_MAX];
8+
$offset = PHP_INT_MAX;
9+
var_dump(array_splice($a,$offset, $length));
10+
$a = [PHP_INT_MAX];
11+
$offset = PHP_INT_MIN;
12+
var_dump(array_splice($a,$offset, $length));
13+
$a = ["a" => PHP_INT_MAX];
14+
$offset = PHP_INT_MAX;
15+
var_dump(array_splice($a,$offset, $length));
16+
$a = ["a" => PHP_INT_MAX];
17+
$offset = PHP_INT_MIN;
18+
var_dump(array_splice($a,$offset, $length));
19+
}
20+
--EXPECTF--
21+
array(0) {
22+
}
23+
array(0) {
24+
}
25+
array(0) {
26+
}
27+
array(0) {
28+
}
29+
array(0) {
30+
}
31+
array(1) {
32+
[0]=>
33+
int(%d)
34+
}
35+
array(0) {
36+
}
37+
array(1) {
38+
["a"]=>
39+
int(%d)
40+
}

0 commit comments

Comments
 (0)