Skip to content

Commit 527ad1d

Browse files
committed
Avoid signed integer overflow in string offset check
Cast to size_t before performing operations instead of afterwards.
1 parent e4b12fc commit 527ad1d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Accessing PHP_INT_MAX and PHP_INT_MIN as string offsets
3+
--FILE--
4+
<?php
5+
6+
$str = "";
7+
var_dump($str[PHP_INT_MAX]);
8+
var_dump($str[PHP_INT_MIN]);
9+
10+
?>
11+
--EXPECTF--
12+
Notice: Uninitialized string offset: %d in %s on line %d
13+
string(0) ""
14+
15+
Notice: Uninitialized string offset: -%d in %s on line %d
16+
string(0) ""

Zend/zend_execute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
23692369
offset = Z_LVAL_P(dim);
23702370
}
23712371

2372-
if (UNEXPECTED(Z_STRLEN_P(container) < (size_t)((offset < 0) ? -offset : (offset + 1)))) {
2372+
if (UNEXPECTED(Z_STRLEN_P(container) < ((offset < 0) ? -(size_t)offset : ((size_t)offset + 1)))) {
23732373
if (type != BP_VAR_IS) {
23742374
zend_error(E_NOTICE, "Uninitialized string offset: " ZEND_LONG_FMT, offset);
23752375
ZVAL_EMPTY_STRING(result);

0 commit comments

Comments
 (0)