Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,27 @@ static PHP_INI_MH(OnUpdate_mbstring_http_output_conv_mimetypes)
return SUCCESS;
}
/* }}} */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the PHP_INI_ENTRY() is commented out by preprocessor macros, then shouldn't the PHP_INI_MH() function be too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I should!

#if HAVE_MBREGEX
/* {{{ static PHP_INI_MH(OnUpdate_regex_stack_limit */
static PHP_INI_MH(OnUpdate_regex_stack_limit)
{
zend_long stack_limit;

stack_limit = atol(ZSTR_VAL(new_value));
if (stack_limit > 0 && stack_limit <= UINT_MAX) {
onig_set_match_stack_limit_size(stack_limit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably use onig_set_match_stack_limit_size_of_match_param() and onig_match_with_param()

} else if (stack_limit <= 0) {
onig_set_match_stack_limit_size(UINT_MAX);
} else {
php_error_docref("ref.mbstring", E_WARNING, "mbstring.regex_stack_limit exceeds UNIT_MAX");
return FAILURE;
}

return SUCCESS;
}
#endif
/* }}} */
/* }}} */

/* {{{ php.ini directive registration */
Expand All @@ -1499,6 +1520,9 @@ PHP_INI_BEGIN()
PHP_INI_ALL,
OnUpdateLong,
strict_detection, zend_mbstring_globals, mbstring_globals)
#if HAVE_MBREGEX
PHP_INI_ENTRY("mbstring.regex_stack_limit", "100000", PHP_INI_ALL, OnUpdate_regex_stack_limit)
#endif
PHP_INI_END()
/* }}} */

Expand Down
24 changes: 24 additions & 0 deletions ext/mbstring/tests/mbregex_stack_limit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Test oniguruma stack limit
--SKIPIF--
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
--FILE--
<?php
$s = str_repeat(' ', 30000);

ini_set('mbstring.regex_stack_limit', 10000);
var_dump(mb_ereg('\\s+$', $s));

ini_set('mbstring.regex_stack_limit', 30000);
var_dump(mb_ereg('\\s+$', $s));

ini_set('mbstring.regex_stack_limit', 30001);
var_dump(mb_ereg('\\s+$', $s));

echo 'OK';
?>
--EXPECT--
bool(false)
bool(false)
int(1)
OK
25 changes: 25 additions & 0 deletions ext/mbstring/tests/mbregex_stack_limit2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Test oniguruma stack limit
--SKIPIF--
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
--FILE--
<?php
function mb_trim( $string, $chars = "", $chars_array = array() )
{
for( $x=0; $x<iconv_strlen( $chars ); $x++ ) $chars_array[] = preg_quote( iconv_substr( $chars, $x, 1 ) );
$encoded_char_list = implode( "|", array_merge( array( "\s","\t","\n","\r", "\0", "\x0B" ), $chars_array ) );

$string = mb_ereg_replace( "^($encoded_char_list)*", "", $string );
$string = mb_ereg_replace( "($encoded_char_list)*$", "", $string );
return $string;
}

ini_set('mbstring.regex_stack_limit', 10000);
var_dump(mb_trim(str_repeat(' ', 10000)));

echo 'OK';
?>
--EXPECTF--
Warning: mb_ereg_replace(): mbregex search failure in php_mbereg_replace_exec(): match-stack limit over in %s on line %d
string(0) ""
OK