Skip to content

Fix GH-13932: Attempt to fix mbstring on windows build (msvc). #13933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -5853,6 +5853,8 @@ static void transfer_encode_mime_bytes(mb_convert_buf *tmpbuf, mb_convert_buf *o
MB_CONVERT_BUF_STORE(outbuf, out, limit);
}

#define MBSTRING_HEADER_ENC_WCHAR_BUFSIZE 90

static zend_string* mb_mime_header_encode(zend_string *input, const mbfl_encoding *incode, const mbfl_encoding *outcode, bool base64, char *linefeed, size_t linefeed_len, zend_long indent)
{
unsigned char *in = (unsigned char*)ZSTR_VAL(input);
Expand Down Expand Up @@ -5883,8 +5885,7 @@ static zend_string* mb_mime_header_encode(zend_string *input, const mbfl_encodin
unsigned int state = 0;
/* wchar_buf should be big enough that when it is full, we definitely have enough
* wchars to fill an entire line of output */
const size_t wchar_buf_len = 90;
uint32_t wchar_buf[wchar_buf_len];
uint32_t wchar_buf[MBSTRING_HEADER_ENC_WCHAR_BUFSIZE];
uint32_t *p, *e;
/* What part of wchar_buf is filled with still-unprocessed data which should not
* be overwritten? */
Expand All @@ -5895,7 +5896,7 @@ static zend_string* mb_mime_header_encode(zend_string *input, const mbfl_encodin
* spaces), just pass it through unchanged */
bool checking_leading_spaces = true;
while (in_len) {
size_t out_len = incode->to_wchar(&in, &in_len, wchar_buf, wchar_buf_len, &state);
size_t out_len = incode->to_wchar(&in, &in_len, wchar_buf, MBSTRING_HEADER_ENC_WCHAR_BUFSIZE, &state);
p = wchar_buf;
e = wchar_buf + out_len;

Expand Down Expand Up @@ -5929,9 +5930,9 @@ no_passthrough: ;
* do so all the way to the end of the string */
while (in_len) {
/* Decode part of the input string, refill wchar_buf */
ZEND_ASSERT(offset + MBSTRING_MIN_WCHAR_BUFSIZE <= wchar_buf_len);
size_t out_len = incode->to_wchar(&in, &in_len, wchar_buf + offset, wchar_buf_len - offset, &state);
ZEND_ASSERT(out_len <= wchar_buf_len - offset);
ZEND_ASSERT(offset + MBSTRING_MIN_WCHAR_BUFSIZE <= MBSTRING_HEADER_ENC_WCHAR_BUFSIZE);
size_t out_len = incode->to_wchar(&in, &in_len, wchar_buf + offset, MBSTRING_HEADER_ENC_WCHAR_BUFSIZE - offset, &state);
ZEND_ASSERT(out_len <= MBSTRING_HEADER_ENC_WCHAR_BUFSIZE - offset);
p = wchar_buf;
e = wchar_buf + offset + out_len;
/* ASCII output is broken into space-delimited 'words'
Expand Down Expand Up @@ -6039,16 +6040,16 @@ mime_encoding_needed: ;
/* Do we need to refill wchar_buf to make sure we don't run out of wchars
* in the middle of a line? */
offset = e - p;
if (wchar_buf_len - offset < MBSTRING_MIN_WCHAR_BUFSIZE) {
if (MBSTRING_HEADER_ENC_WCHAR_BUFSIZE - offset < MBSTRING_MIN_WCHAR_BUFSIZE) {
goto start_new_line;
}
memmove(wchar_buf, p, offset * sizeof(uint32_t));

while(true) {
refill_wchar_buf: ;
ZEND_ASSERT(offset + MBSTRING_MIN_WCHAR_BUFSIZE <= wchar_buf_len);
size_t out_len = incode->to_wchar(&in, &in_len, wchar_buf + offset, wchar_buf_len - offset, &state);
ZEND_ASSERT(out_len <= wchar_buf_len - offset);
ZEND_ASSERT(offset + MBSTRING_MIN_WCHAR_BUFSIZE <= MBSTRING_HEADER_ENC_WCHAR_BUFSIZE);
size_t out_len = incode->to_wchar(&in, &in_len, wchar_buf + offset, MBSTRING_HEADER_ENC_WCHAR_BUFSIZE - offset, &state);
ZEND_ASSERT(out_len <= MBSTRING_HEADER_ENC_WCHAR_BUFSIZE - offset);
p = wchar_buf;
e = wchar_buf + offset + out_len;

Expand Down Expand Up @@ -6129,7 +6130,7 @@ start_new_line: ;
buf.out = mb_convert_buf_add(buf.out, ' ');
line_start = mb_convert_buf_len(&buf);
offset = e - p;
if (in_len && (wchar_buf_len - offset >= MBSTRING_MIN_WCHAR_BUFSIZE)) {
if (in_len && (MBSTRING_HEADER_ENC_WCHAR_BUFSIZE - offset >= MBSTRING_MIN_WCHAR_BUFSIZE)) {
/* Copy any remaining wchars to beginning of buffer and refill
* the rest of the buffer */
memmove(wchar_buf, p, offset * sizeof(uint32_t));
Expand Down