Skip to content

Commit cbc416e

Browse files
committed
Merge branch 'PHP-7.1'
* PHP-7.1: Fix bug #73648 - integer overflow in substr
2 parents 50dea59 + 05f2f73 commit cbc416e

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
@@ -167,7 +167,7 @@ static zend_string *php_hex2bin(const unsigned char *old, const size_t oldlen)
167167
int is_letter = ((unsigned int) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(unsigned int) - 1);
168168
unsigned char d;
169169

170-
/* basically (c >= '0' && c <= '9') || (l >= 'A' && l <= 'F') */
170+
/* basically (c >= '0' && c <= '9') || (l >= 'A' && l <= 'F') */
171171
if (EXPECTED((((c ^ '0') - 10) >> (8 * sizeof(unsigned int) - 1)) | is_letter)) {
172172
d = (l - 0x10 - 0x27 * is_letter) << 4;
173173
} else {
@@ -2412,7 +2412,11 @@ PHP_FUNCTION(substr)
24122412
}
24132413
}
24142414

2415-
if ((f + l) > (zend_long)ZSTR_LEN(str)) {
2415+
if (f > (zend_long)ZSTR_LEN(str)) {
2416+
RETURN_FALSE;
2417+
}
2418+
2419+
if ((size_t)l > ZSTR_LEN(str) - (size_t)f) {
24162420
l = ZSTR_LEN(str) - f;
24172421
}
24182422

@@ -2889,7 +2893,7 @@ PHPAPI char *php_strtr(char *str, size_t len, char *str_from, char *str_to, size
28892893
for (i = 0; i < trlen; i++) {
28902894
xlat[(size_t)(unsigned char) str_from[i]] = str_to[i];
28912895
}
2892-
2896+
28932897
for (i = 0; i < len; i++) {
28942898
str[i] = xlat[(size_t)(unsigned char) str[i]];
28952899
}
@@ -3282,7 +3286,7 @@ static zend_string *php_str_to_str_i_ex(zend_string *haystack, char *lc_haystack
32823286
zend_string_release(lc_needle);
32833287
goto nothing_todo;
32843288
}
3285-
3289+
32863290
if (str_len > ZSTR_LEN(lc_needle)) {
32873291
new_str = zend_string_safe_alloc(count, str_len - ZSTR_LEN(lc_needle), ZSTR_LEN(haystack), 0);
32883292
} else {
@@ -3445,7 +3449,7 @@ PHP_FUNCTION(strtr)
34453449
ZVAL_LONG(&tmp, num_key);
34463450
convert_to_string(&tmp);
34473451
str_key = Z_STR(tmp);
3448-
}
3452+
}
34493453
replace = zval_get_string(entry);
34503454
if (ZSTR_LEN(str_key) < 1) {
34513455
RETVAL_STR_COPY(str);
@@ -4012,7 +4016,7 @@ static zend_long php_str_replace_in_subject(zval *search, zval *replace, zval *s
40124016
zend_string_release(lc_subject_str);
40134017
lc_subject_str = NULL;
40144018
}
4015-
}
4019+
}
40164020
}
40174021

40184022
zend_string_release(search_str);

0 commit comments

Comments
 (0)