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.
RETURN_STRINGL(str, str_len, 1);
}
- if (is_xhtml) {
- new_length = str_len + repl_cnt * (sizeof("<br />") - 1);
- } else {
- new_length = str_len + repl_cnt * (sizeof("<br>") - 1);
- }
+ {
+ size_t repl_len = is_xhtml ? (sizeof("<br />") - 1) : (sizeof("<br>") - 1);
- tmp = target = emalloc(new_length + 1);
+ new_length = str_len + repl_cnt * repl_len;
+ tmp = target = safe_emalloc(repl_cnt, repl_len, str_len + 1);
+ }
while (str < end) {
switch (*str) {