Skip to content

Commit 8b539ed

Browse files
author
Moriyoshi Koizumi
committed
Fixed lots of crashes in mbregex.
# most of them were caused by stupid mistakes
1 parent 1dba0c1 commit 8b539ed

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

ext/mbstring/php_mbregex.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ PHP_FUNCTION(mb_regex_encoding)
405405
static void
406406
_php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
407407
{
408+
zval tmp;
408409
zval *arg_pattern, *array;
409410
char *string;
410411
int string_len;
@@ -427,10 +428,13 @@ _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
427428
/* compile the regular expression from the supplied regex */
428429
if (Z_TYPE_P(arg_pattern) != IS_STRING) {
429430
/* we convert numbers to integers and treat them as a string */
430-
if (Z_TYPE_P(arg_pattern) == IS_DOUBLE) {
431-
convert_to_long_ex(&arg_pattern); /* get rid of decimal places */
431+
tmp = *arg_pattern;
432+
zval_copy_ctor(&tmp);
433+
if (Z_TYPE_P(&tmp) == IS_DOUBLE) {
434+
convert_to_long(&tmp); /* get rid of decimal places */
432435
}
433-
convert_to_string_ex(&arg_pattern);
436+
convert_to_string(&tmp);
437+
arg_pattern = &tmp;
434438
/* don't bother doing an extended regex with just a number */
435439
}
436440
err = php_mbregex_compile_pattern(
@@ -439,7 +443,8 @@ _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
439443
Z_STRLEN_P(arg_pattern),
440444
option, MBSTRG(current_mbctype) TSRMLS_CC);
441445
if (err) {
442-
RETURN_FALSE;
446+
RETVAL_FALSE;
447+
goto out;
443448
}
444449

445450
/* actually execute the regular expression */
@@ -451,7 +456,8 @@ _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
451456
&regs);
452457
if (err < 0) {
453458
mbre_free_registers(&regs);
454-
RETURN_FALSE;
459+
RETVAL_FALSE;
460+
goto out;
455461
}
456462

457463
match_len = 1;
@@ -476,6 +482,10 @@ _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
476482
match_len = 1;
477483
}
478484
RETVAL_LONG(match_len);
485+
out:
486+
if (arg_pattern == &tmp) {
487+
zval_dtor(&tmp);
488+
}
479489
}
480490

481491
/* {{{ proto int mb_ereg(string pattern, string string [, array registers])
@@ -690,25 +700,29 @@ PHP_FUNCTION(mb_eregi_replace)
690700
split multibyte string into array by regular expression */
691701
PHP_FUNCTION(mb_split)
692702
{
693-
zval *arg_pat;
703+
char *arg_pattern;
704+
int arg_pattern_len;
694705
mb_regex_t re;
695706
struct mbre_registers regs = {0, 0, 0, 0};
696707
char *string;
697-
int n, err, string_len, pos;
708+
int string_len;
709+
710+
int n, err, pos;
698711
long count = -1;
699712

700-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|l", &arg_pat,
701-
&string, &string_len, &count) == FAILURE) {
713+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &arg_pattern, &arg_pattern_len, &string, &string_len, &count) == FAILURE) {
702714
RETURN_FALSE;
703715
}
704716

705-
if (count == 0) count = 1;
717+
if (count == 0) {
718+
count = 1;
719+
}
706720

707721
/* create regex pattern buffer */
708722
err = php_mbregex_compile_pattern(
709723
&re,
710-
Z_STRVAL_P(arg_pat),
711-
Z_STRLEN_P(arg_pat),
724+
arg_pattern,
725+
arg_pattern_len,
712726
MBSTRG(regex_default_options), MBSTRG(current_mbctype) TSRMLS_CC);
713727
if (err) {
714728
RETURN_FALSE;

0 commit comments

Comments
 (0)