Skip to content

Commit 9c28385

Browse files
committed
Optimize out another bounds check in BIG5 decoder
This gives about a 9% speed boost for BIG5 encoding conversion. (Not as much as I was hoping!)
1 parent 5c64cf5 commit 9c28385

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

ext/mbstring/libmbfl/filters/mbfilter_big5.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,16 @@ static size_t mb_big5_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf
391391
unsigned char *p = *in, *e = p + *in_len;
392392
uint32_t *out = buf, *limit = buf + bufsize;
393393

394+
e--; /* Stop the main loop 1 byte short of the end of the input */
395+
394396
while (p < e && out < limit) {
395397
unsigned char c = *p++;
396398

397399
if (c <= 0x7F) {
398400
*out++ = c;
399-
} else if (c > 0xA0 && c <= 0xF9 && c != 0xC8 && p < e) {
401+
} else if (c > 0xA0 && c <= 0xF9 && c != 0xC8) {
402+
/* We don't need to check p < e here; it's not possible that this pointer dereference
403+
* will be outside the input string, because of e-- above */
400404
unsigned char c2 = *p++;
401405

402406
if ((c2 >= 0x40 && c2 <= 0x7E) || (c2 >= 0xA1 && c2 <= 0xFE)) {
@@ -414,7 +418,13 @@ static size_t mb_big5_to_wchar(unsigned char **in, size_t *in_len, uint32_t *buf
414418
}
415419
}
416420

417-
*in_len = e - p;
421+
/* Finish up last byte of input string if there is one */
422+
if (p == e && out < limit) {
423+
unsigned char c = *p++;
424+
*out++ = (c <= 0x7F) ? c : MBFL_BAD_INPUT;
425+
}
426+
427+
*in_len = e - p + 1;
418428
*in = p;
419429
return out - buf;
420430
}

0 commit comments

Comments
 (0)