Skip to content

Commit 20407d0

Browse files
committed
Fix bug #77370 - check that we do not read past buffer end when parsing multibytes
1 parent a918020 commit 20407d0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ext/mbstring/oniguruma/regparse.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ strdup_with_null(OnigEncoding enc, UChar* s, UChar* end)
246246
}
247247
#endif
248248

249+
#if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
250+
# define UNEXPECTED(condition) __builtin_expect(condition, 0)
251+
#else
252+
# define UNEXPECTED(condition) (condition)
253+
#endif
254+
249255
/* scan pattern methods */
250256
#define PEND_VALUE 0
251257

@@ -260,14 +266,17 @@ strdup_with_null(OnigEncoding enc, UChar* s, UChar* end)
260266
c = ONIGENC_MBC_TO_CODE(enc, p, end); \
261267
pfetch_prev = p; \
262268
p += ONIGENC_MBC_ENC_LEN(enc, p); \
269+
if(UNEXPECTED(p > end)) p = end; \
263270
} while (0)
264271

265272
#define PINC_S do { \
266273
p += ONIGENC_MBC_ENC_LEN(enc, p); \
274+
if(UNEXPECTED(p > end)) p = end; \
267275
} while (0)
268276
#define PFETCH_S(c) do { \
269277
c = ONIGENC_MBC_TO_CODE(enc, p, end); \
270278
p += ONIGENC_MBC_ENC_LEN(enc, p); \
279+
if(UNEXPECTED(p > end)) p = end; \
271280
} while (0)
272281

273282
#define PPEEK (p < end ? ONIGENC_MBC_TO_CODE(enc, p, end) : PEND_VALUE)

ext/mbstring/tests/bug77370.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Bug #77370 (Buffer overflow on mb regex functions - fetch_token)
3+
--SKIPIF--
4+
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
5+
--FILE--
6+
<?php
7+
var_dump(mb_split(" \xfd",""));
8+
?>
9+
--EXPECT--
10+
array(1) {
11+
[0]=>
12+
string(0) ""
13+
}

0 commit comments

Comments
 (0)