Skip to content

Commit

Permalink
Fix bug #77418 - Heap overflow in utf32be_mbc_to_code
Browse files Browse the repository at this point in the history
(cherry picked from commit aeec40c)
  • Loading branch information
smalyshev authored and cmb69 committed Jan 7, 2019
1 parent 11ce508 commit b6fe458
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -33,6 +33,7 @@ PHP NEWS

- MBString:
. Fixed bug #77367 (Negative size parameter in mb_split). (Stas)
. Fixed bug #77418 (Heap overflow in utf32be_mbc_to_code). (Stas)

- OCI8:
. Fixed bug #76804 (oci_pconnect with OCI_CRED_EXT not working). (KoenigsKind)
Expand Down
4 changes: 3 additions & 1 deletion ext/mbstring/oniguruma/src/utf16_be.c
Expand Up @@ -128,16 +128,18 @@ utf16be_is_mbc_newline(const UChar* p, const UChar* end)
}

static OnigCodePoint
utf16be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
utf16be_mbc_to_code(const UChar* p, const UChar* end)
{
OnigCodePoint code;

if (UTF16_IS_SURROGATE_FIRST(*p)) {
if (end - p < 4) return 0;
code = ((((p[0] - 0xd8) << 2) + ((p[1] & 0xc0) >> 6) + 1) << 16)
+ ((((p[1] & 0x3f) << 2) + (p[2] - 0xdc)) << 8)
+ p[3];
}
else {
if (end - p < 2) return 0;
code = p[0] * 256 + p[1];
}
return code;
Expand Down
3 changes: 2 additions & 1 deletion ext/mbstring/oniguruma/src/utf16_le.c
Expand Up @@ -141,13 +141,14 @@ utf16le_is_mbc_newline(const UChar* p, const UChar* end)
}

static OnigCodePoint
utf16le_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
utf16le_mbc_to_code(const UChar* p, const UChar* end)
{
OnigCodePoint code;
UChar c0 = *p;
UChar c1 = *(p+1);

if (UTF16_IS_SURROGATE_FIRST(c1)) {
if (end - p < 4) return 0;
code = ((((c1 - 0xd8) << 2) + ((c0 & 0xc0) >> 6) + 1) << 16)
+ ((((c0 & 0x3f) << 2) + (p[3] - 0xdc)) << 8)
+ p[2];
Expand Down
1 change: 1 addition & 0 deletions ext/mbstring/oniguruma/src/utf32_be.c
Expand Up @@ -67,6 +67,7 @@ utf32be_is_mbc_newline(const UChar* p, const UChar* end)
static OnigCodePoint
utf32be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
{
if (end - p < 4) return 0;
return (OnigCodePoint )(((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3]);
}

Expand Down
1 change: 1 addition & 0 deletions ext/mbstring/oniguruma/src/utf32_le.c
Expand Up @@ -67,6 +67,7 @@ utf32le_is_mbc_newline(const UChar* p, const UChar* end)
static OnigCodePoint
utf32le_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
{
if (end - p < 4) return 0;
return (OnigCodePoint )(((p[3] * 256 + p[2]) * 256 + p[1]) * 256 + p[0]);
}

Expand Down
14 changes: 14 additions & 0 deletions ext/mbstring/tests/bug77418.phpt
@@ -0,0 +1,14 @@
--TEST--
Bug #77371 (Heap overflow in utf32be_mbc_to_code)
--SKIPIF--
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
--FILE--
<?php
mb_regex_encoding("UTF-32");
var_dump(mb_split("\x00\x00\x00\x5c\x00\x00\x00B","000000000000000000000000000000"));
?>
--EXPECT--
array(1) {
[0]=>
string(30) "000000000000000000000000000000"
}

0 comments on commit b6fe458

Please sign in to comment.