Skip to content

Commit 05f2f73

Browse files
committed
Merge branch 'PHP-7.0' into PHP-7.1
* PHP-7.0: Fix bug #73648 - integer overflow in substr
2 parents 29433f9 + d1d002f commit 05f2f73

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

ext/standard/string.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static zend_string *php_hex2bin(const unsigned char *old, const size_t oldlen)
165165
int is_letter = ((unsigned int) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(unsigned int) - 1);
166166
unsigned char d;
167167

168-
/* basically (c >= '0' && c <= '9') || (l >= 'A' && l <= 'F') */
168+
/* basically (c >= '0' && c <= '9') || (l >= 'A' && l <= 'F') */
169169
if (EXPECTED((((c ^ '0') - 10) >> (8 * sizeof(unsigned int) - 1)) | is_letter)) {
170170
d = (l - 0x10 - 0x27 * is_letter) << 4;
171171
} else {
@@ -2381,7 +2381,11 @@ PHP_FUNCTION(substr)
23812381
}
23822382
}
23832383

2384-
if ((f + l) > (zend_long)ZSTR_LEN(str)) {
2384+
if (f > (zend_long)ZSTR_LEN(str)) {
2385+
RETURN_FALSE;
2386+
}
2387+
2388+
if ((size_t)l > ZSTR_LEN(str) - (size_t)f) {
23852389
l = ZSTR_LEN(str) - f;
23862390
}
23872391

@@ -2854,7 +2858,7 @@ PHPAPI char *php_strtr(char *str, size_t len, char *str_from, char *str_to, size
28542858
for (i = 0; i < trlen; i++) {
28552859
xlat[(size_t)(unsigned char) str_from[i]] = str_to[i];
28562860
}
2857-
2861+
28582862
for (i = 0; i < len; i++) {
28592863
str[i] = xlat[(size_t)(unsigned char) str[i]];
28602864
}
@@ -3247,7 +3251,7 @@ static zend_string *php_str_to_str_i_ex(zend_string *haystack, char *lc_haystack
32473251
zend_string_release(lc_needle);
32483252
goto nothing_todo;
32493253
}
3250-
3254+
32513255
if (str_len > ZSTR_LEN(lc_needle)) {
32523256
new_str = zend_string_safe_alloc(count, str_len - ZSTR_LEN(lc_needle), ZSTR_LEN(haystack), 0);
32533257
} else {
@@ -3410,7 +3414,7 @@ PHP_FUNCTION(strtr)
34103414
ZVAL_LONG(&tmp, num_key);
34113415
convert_to_string(&tmp);
34123416
str_key = Z_STR(tmp);
3413-
}
3417+
}
34143418
replace = zval_get_string(entry);
34153419
if (ZSTR_LEN(str_key) < 1) {
34163420
RETVAL_STR_COPY(str);
@@ -3973,7 +3977,7 @@ static zend_long php_str_replace_in_subject(zval *search, zval *replace, zval *s
39733977
zend_string_release(lc_subject_str);
39743978
lc_subject_str = NULL;
39753979
}
3976-
}
3980+
}
39773981
}
39783982

39793983
zend_string_release(search_str);

0 commit comments

Comments
 (0)