Skip to content

Commit 17fac30

Browse files
committed
Fix GH-24489: Restore mbstring buffer growth fix lost in merge
1 parent 385f368 commit 17fac30

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎ext/mbstring/libmbfl/mbfl/mbfl_encoding.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ static inline zend_result mb_convert_buf_ensure(mb_convert_buf *buf, unsigned ch
170170
size_t newsize = oldsize + MAX(oldsize >> 1, needed);
171171
zend_string *newstr = safe_ferealloc((buf)->str, 1, newsize, _ZSTR_STRUCT_SIZE(0), /* fallible */ true);
172172
if (!newstr) {
173+
/* An earlier growth may have moved the buffer. */
174+
buf->out = *out;
175+
buf->limit = *limit;
173176
return FAILURE;
174177
}
175178
*out = (unsigned char*)ZSTR_VAL(newstr) + (*out - (unsigned char*)ZSTR_VAL((buf)->str));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
mbstring stores conversion-buffer pointers when growth fails
3+
--EXTENSIONS--
4+
mbstring
5+
--INI--
6+
memory_limit=8M
7+
--FILE--
8+
<?php
9+
// A single batch expands from 128 to 384 bytes, requiring two buffer growths.
10+
$input = str_repeat("\x80", 128);
11+
// The tracked allocator leaves enough space for the first growth, but not the second.
12+
$padding = str_repeat('a', 8 * 1024 * 1024 - memory_get_usage() - 384);
13+
mb_substr_count($input, 'Z', 'CP1252');
14+
echo "not reached\n";
15+
?>
16+
--EXPECTF--
17+
Fatal error: Allowed memory size of 8388608 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d

0 commit comments

Comments
 (0)