Skip to content

Commit 871e61f

Browse files
committed
Fully use available buffer space where converting Base64
I didn't think this through carefully enough when first writing this code, but it's not necessary to reserve space for the 1-2 wchars which may be emitted before exiting the function. Why? Well, we are guaranteed that when we enter the function, there are at least 3 spaces in the wchar buffer. The only way those can be consumed is if wchars are emitted in the main 'while' loop, but if it does emit any wchars, it will set 'bits' to zero at the same time, which means the final part will not emit anything. 'bits' can be incremented again by the main loop, but the main loop only runs while there are still at least 3 spaces in the buffer. So basically, we are guaranteed that when the main loop terminates, either there are 3 or more spaces remaining in the wchar buffer, or else 'bits' is zero, or both.
1 parent d9f3ca7 commit 871e61f

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

ext/mbstring/libmbfl/filters/mbfilter_base64.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ static int decode_base64(char c)
234234

235235
static size_t mb_base64_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf, size_t bufsize, unsigned int *state)
236236
{
237+
ZEND_ASSERT(bufsize >= 3);
238+
237239
unsigned char *p = *in, *e = p + *in_len;
238-
/* Reserve two slots at the end of the output buffer so that we always have
239-
* space to emit any trailing bytes when we hit the end of the input string */
240-
uint32_t *out = buf, *limit = buf + bufsize - 2;
240+
uint32_t *out = buf, *limit = buf + bufsize;
241241

242242
unsigned int bits = *state & 0xFF, cache = *state >> 8;
243243

@@ -266,6 +266,7 @@ static size_t mb_base64_to_wchar(unsigned char **in, size_t *in_len, uint32_t *b
266266

267267
if (p == e) {
268268
if (bits) {
269+
/* If we reach here, there will be at least 3 spaces remaining in output buffer */
269270
if (bits == 18) {
270271
*out++ = (cache >> 10) & 0xFF;
271272
*out++ = (cache >> 2) & 0xFF;

0 commit comments

Comments
 (0)