Should fix bug #77381, bug #77382, bug #77385, bug #77394.
code = ONIGENC_MBC_TO_CODE(enc, p, end);
len = enclen(enc, p);
+ if (*pp + len > end) len = end - *pp;
*pp += len;
#ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
ambig = NSTRING_IS_AMBIG(node);
p = prev = sn->s;
- prev_len = enclen(enc, p);
+ SAFE_ENC_LEN(enc, p, sn->end, prev_len);
p += prev_len;
slen = 1;
rlen = 0;
for (; p < sn->end; ) {
- len = enclen(enc, p);
+ SAFE_ENC_LEN(enc, p, sn->end, len);
if (len == prev_len) {
slen++;
}
ambig = NSTRING_IS_AMBIG(node);
p = prev = sn->s;
- prev_len = enclen(enc, p);
+ SAFE_ENC_LEN(enc, p, end, prev_len);
p += prev_len;
slen = 1;
for (; p < end; ) {
- len = enclen(enc, p);
- if (p + len > end) len = end - p;
+ SAFE_ENC_LEN(enc, p, end, len);
if (len == prev_len) {
slen++;
}
goto err;
}
- len = enclen(reg->enc, p);
+ SAFE_ENC_LEN(reg->enc, p, end, len);
if (n == 0) {
if (IS_NULL(snode)) {
}
#endif
-#if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
-# define UNEXPECTED(condition) __builtin_expect(condition, 0)
-#else
-# define UNEXPECTED(condition) (condition)
-#endif
-
/* scan pattern methods */
#define PEND_VALUE 0
tok->u.code = (OnigCodePoint )num;
}
else { /* string */
- p = tok->backp + enclen(enc, tok->backp);
+ int len;
+ SAFE_ENC_LEN(enc, tok->backp, end, len);
+ p = tok->backp + len;
}
break;
}
#endif
#endif
+#if (defined (__GNUC__) && __GNUC__ > 2 ) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX)
+# define UNEXPECTED(condition) __builtin_expect(condition, 0)
+#else
+# define UNEXPECTED(condition) (condition)
+#endif
+
+#define SAFE_ENC_LEN(enc, p, end, res) do { \
+ int __res = enclen(enc, p); \
+ if (UNEXPECTED(p + __res > end)) __res = end - p; \
+ res = __res; \
+} while(0);
+
#endif /* REGPARSE_H */
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
--FILE--
<?php
-var_dump(mb_ereg("()0\xfc00000\xfc00000\xfc00000\xfc",""))
+var_dump(mb_ereg("()0\xfc00000\xfc00000\xfc00000\xfc",""));
?>
--EXPECT--
bool(false)
\ No newline at end of file
--- /dev/null
+--TEST--
+Bug #77381 (heap buffer overflow in multibyte match_at)
+--SKIPIF--
+<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
+--FILE--
+<?php
+var_dump(mb_ereg("000||0\xfa","0"));
+var_dump(mb_ereg("(?i)000000000000000000000\xf0",""));
+var_dump(mb_ereg("0000\\"."\xf5","0"));
+var_dump(mb_ereg("(?i)FFF00000000000000000\xfd",""));
+?>
+--EXPECT--
+int(1)
+bool(false)
+bool(false)
+bool(false)