Skip to content

Commit 157ddd9

Browse files
committed
Fix potential integer overflow in nl2br
The buffer size was calculated manually, thus creating integer overflows for very large inputs, e.g. nl2br(str_repeat("\n", 613566757)). The code now uses safe_emalloc, thus making the code throw an error instead of crashing.
1 parent 88f46b1 commit 157ddd9

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

ext/standard/string.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4001,13 +4001,12 @@ PHP_FUNCTION(nl2br)
40014001
RETURN_STRINGL(str, str_len, 1);
40024002
}
40034003

4004-
if (is_xhtml) {
4005-
new_length = str_len + repl_cnt * (sizeof("<br />") - 1);
4006-
} else {
4007-
new_length = str_len + repl_cnt * (sizeof("<br>") - 1);
4008-
}
4004+
{
4005+
size_t repl_len = is_xhtml ? (sizeof("<br />") - 1) : (sizeof("<br>") - 1);
40094006

4010-
tmp = target = emalloc(new_length + 1);
4007+
new_length = str_len + repl_cnt * repl_len;
4008+
tmp = target = safe_emalloc(repl_cnt, repl_len, str_len + 1);
4009+
}
40114010

40124011
while (str < end) {
40134012
switch (*str) {

0 commit comments

Comments
 (0)