[php-src] master: mbstring: Save the write offset before reallocating the conversion buffer

From: Date: Thu, 26 Nov 2026 10:41:07 +0000
Subject: [php-src] master: mbstring: Save the write offset before reallocating the conversion buffer
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144209@lists.php.net to get a copy of this message
Author: Florian Engelhardt (realFlowControl) Committer: Alex Dowad (alexdowad) Date: 2026-11-26T19:40:45+09:00 Commit: https://github.com/php/php-src/commit/eb9aebeee7be34c8957bb1f36977cbc1ec137765 Raw diff: https://github.com/php/php-src/commit/eb9aebeee7be34c8957bb1f36977cbc1ec137765.diff mbstring: Save the write offset before reallocating the conversion buffer Changed paths: M ext/mbstring/libmbfl/mbfl/mbfl_encoding.h Diff: diff --git a/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h b/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h index 594e2f3ebd2a..9a2ab1a2d0c5 100644 --- a/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h +++ b/ext/mbstring/libmbfl/mbfl/mbfl_encoding.h @@ -168,6 +168,8 @@ static inline zend_result mb_convert_buf_ensure(mb_convert_buf *buf, unsigned ch if ((size_t)(*limit - *out) < needed) { size_t oldsize = *limit - (unsigned char*)ZSTR_VAL((buf)->str); size_t growth = MAX(oldsize >> 1, needed); + /* Save offset before realloc, which may move the buffer, subtracting invalidated pointers afterwards is UB */ + size_t out_offset = *out - (unsigned char*)ZSTR_VAL((buf)->str); zend_string *newstr = safe_ferealloc((buf)->str, 1, oldsize, zend_string_safe_offset(1, oldsize, growth), /* fallible */ true); if (!newstr) { /* An earlier growth may have moved the buffer. */ @@ -177,7 +179,7 @@ static inline zend_result mb_convert_buf_ensure(mb_convert_buf *buf, unsigned ch } /* GCC can't see that allocation fails if growth is too large, so make it explicit. */ ZEND_ASSERT(growth <= ZSTR_MAX_LEN); - *out = (unsigned char*)ZSTR_VAL(newstr) + (*out - (unsigned char*)ZSTR_VAL((buf)->str)); + *out = (unsigned char*)ZSTR_VAL(newstr) + out_offset; *limit = (unsigned char*)ZSTR_VAL(newstr) + oldsize + growth; (buf)->str = newstr; }

« previous php.cvs (#144209) next »