Skip to content

Commit

Permalink
Fix bug #79037 (global buffer-overflow in mbfl_filt_conv_big5_wchar)
Browse files Browse the repository at this point in the history
  • Loading branch information
smalyshev committed Jan 21, 2020
1 parent 0f79b1b commit 2bcbc95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ext/mbstring/libmbfl/filters/mbfilter_big5.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ static unsigned short cp950_pua_tbl[][4] = {
{0xf70f,0xf848,0xc740,0xc8fe},
};

static inline int is_in_cp950_pua(int c1, int c) {
if ((c1 >= 0xfa && c1 <= 0xfe) || (c1 >= 0x8e && c1 <= 0xa0) ||
(c1 >= 0x81 && c1 <= 0x8d) || (c1 >= 0xc7 && c1 <= 0xc8)) {
return (c >=0x40 && c <= 0x7e) || (c >= 0xa1 && c <= 0xfe);
}
if (c1 == 0xc6) {
return c >= 0xa1 && c <= 0xfe;
}
return 0;
}

/*
* Big5 => wchar
*/
Expand Down Expand Up @@ -186,11 +197,7 @@ mbfl_filt_conv_big5_wchar(int c, mbfl_convert_filter *filter)

if (filter->from->no_encoding == mbfl_no_encoding_cp950) {
/* PUA for CP950 */
if (w <= 0 &&
(((c1 >= 0xfa && c1 <= 0xfe) || (c1 >= 0x8e && c1 <= 0xa0) ||
(c1 >= 0x81 && c1 <= 0x8d) ||(c1 >= 0xc7 && c1 <= 0xc8))
&& ((c > 0x39 && c < 0x7f) || (c > 0xa0 && c < 0xff))) ||
((c1 == 0xc6) && (c > 0xa0 && c < 0xff))) {
if (w <= 0 && is_in_cp950_pua(c1, c)) {
c2 = c1 << 8 | c;
for (k = 0; k < sizeof(cp950_pua_tbl)/(sizeof(unsigned short)*4); k++) {
if (c2 >= cp950_pua_tbl[k][2] && c2 <= cp950_pua_tbl[k][3]) {
Expand Down
10 changes: 10 additions & 0 deletions ext/mbstring/tests/bug79037.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Bug #79037: global buffer-overflow in `mbfl_filt_conv_big5_wchar`
--FILE--
<?php

var_dump(mb_convert_encoding("\x81\x3a", "UTF-8", "CP950"));

?>
--EXPECT--
string(1) "?"

0 comments on commit 2bcbc95

Please sign in to comment.