diff options
Diffstat (limited to 'ext/pcre/php_pcre.c')
-rw-r--r-- | ext/pcre/php_pcre.c | 144 |
1 files changed, 43 insertions, 101 deletions
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c index 39896bb07b..fdc1b1b856 100644 --- a/ext/pcre/php_pcre.c +++ b/ext/pcre/php_pcre.c @@ -1,7 +1,5 @@ /* +----------------------------------------------------------------------+ - | PHP Version 7 | - +----------------------------------------------------------------------+ | Copyright (c) The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | @@ -20,6 +18,7 @@ #include "php_ini.h" #include "php_globals.h" #include "php_pcre.h" +#include "php_pcre_arginfo.h" #include "ext/standard/info.h" #include "ext/standard/basic_functions.h" #include "zend_smart_str.h" @@ -50,21 +49,9 @@ struct _pcre_cache_entry { uint32_t capture_count; uint32_t name_count; uint32_t compile_options; - uint32_t extra_compile_options; uint32_t refcount; }; -enum { - PHP_PCRE_NO_ERROR = 0, - PHP_PCRE_INTERNAL_ERROR, - PHP_PCRE_BACKTRACK_LIMIT_ERROR, - PHP_PCRE_RECURSION_LIMIT_ERROR, - PHP_PCRE_BAD_UTF8_ERROR, - PHP_PCRE_BAD_UTF8_OFFSET_ERROR, - PHP_PCRE_JIT_STACKLIMIT_ERROR -}; - - PHPAPI ZEND_DECLARE_MODULE_GLOBALS(pcre) #ifdef HAVE_PCRE_JIT_SUPPORT @@ -140,6 +127,33 @@ static void pcre_handle_exec_error(int pcre_code) /* {{{ */ } /* }}} */ +static const char *php_pcre_get_error_msg(php_pcre_error_code error_code) /* {{{ */ +{ + switch (error_code) { + case PHP_PCRE_NO_ERROR: + return "No error"; + case PHP_PCRE_INTERNAL_ERROR: + return "Internal error"; + case PHP_PCRE_BAD_UTF8_ERROR: + return "Malformed UTF-8 characters, possibly incorrectly encoded"; + case PHP_PCRE_BAD_UTF8_OFFSET_ERROR: + return "The offset did not correspond to the beginning of a valid UTF-8 code point"; + case PHP_PCRE_BACKTRACK_LIMIT_ERROR: + return "Backtrack limit exhausted"; + case PHP_PCRE_RECURSION_LIMIT_ERROR: + return "Recursion limit exhausted"; + +#ifdef HAVE_PCRE_JIT_SUPPORT + case PHP_PCRE_JIT_STACKLIMIT_ERROR: + return "JIT stack limit exhausted"; +#endif + + default: + return "Unknown error"; + } +} +/* }}} */ + static void php_free_pcre_cache(zval *data) /* {{{ */ { pcre_cache_entry *pce = (pcre_cache_entry *) Z_PTR_P(data); @@ -169,7 +183,6 @@ static void php_pcre_free(void *block, void *data) pefree(block, 1); }/*}}}*/ -#define PHP_PCRE_DEFAULT_EXTRA_COPTIONS PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL #define PHP_PCRE_PREALLOC_MDATA_SIZE 32 static void php_pcre_init_pcre2(uint8_t jit) @@ -190,12 +203,6 @@ static void php_pcre_init_pcre2(uint8_t jit) } } - /* XXX The 'X' modifier is the default behavior in PCRE2. This option is - called dangerous in the manual, as typos in patterns can cause - unexpected results. We might want to to switch to the default PCRE2 - behavior, too, thus causing a certain BC break. */ - pcre2_set_compile_extra_options(cctx, PHP_PCRE_DEFAULT_EXTRA_COPTIONS); - if (!mctx) { mctx = pcre2_match_context_create(gctx); if (!mctx) { @@ -571,7 +578,6 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in { pcre2_code *re = NULL; uint32_t coptions = 0; - uint32_t extra_coptions = PHP_PCRE_DEFAULT_EXTRA_COPTIONS; PCRE2_UCHAR error[128]; PCRE2_SIZE erroffset; int errnumber; @@ -705,8 +711,8 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in case 'A': coptions |= PCRE2_ANCHORED; break; case 'D': coptions |= PCRE2_DOLLAR_ENDONLY;break; case 'S': /* Pass. */ break; + case 'X': /* Pass. */ break; case 'U': coptions |= PCRE2_UNGREEDY; break; - case 'X': extra_coptions &= ~PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL; break; case 'u': coptions |= PCRE2_UTF; /* In PCRE, by default, \d, \D, \s, \S, \w, and \W recognize only ASCII characters, even in UTF-8 mode. However, this can be changed by setting @@ -770,19 +776,9 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in pcre2_set_character_tables(cctx, tables); } - /* Set extra options for the compile context. */ - if (PHP_PCRE_DEFAULT_EXTRA_COPTIONS != extra_coptions) { - pcre2_set_compile_extra_options(cctx, extra_coptions); - } - /* Compile pattern and display a warning if compilation failed. */ re = pcre2_compile((PCRE2_SPTR)pattern, pattern_len, coptions, &errnumber, &erroffset, cctx); - /* Reset the compile context extra options to default. */ - if (PHP_PCRE_DEFAULT_EXTRA_COPTIONS != extra_coptions) { - pcre2_set_compile_extra_options(cctx, PHP_PCRE_DEFAULT_EXTRA_COPTIONS); - } - if (re == NULL) { if (key != regex) { zend_string_release_ex(key, 0); @@ -832,7 +828,6 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, in new_entry.re = re; new_entry.preg_options = poptions; new_entry.compile_options = coptions; - new_entry.extra_compile_options = extra_coptions; new_entry.refcount = 0; rc = pcre2_pattern_info(re, PCRE2_INFO_CAPTURECOUNT, &new_entry.capture_count); @@ -1128,7 +1123,7 @@ static void php_do_pcre_match(INTERNAL_FUNCTION_PARAMETERS, int global) /* {{{ * Z_PARAM_ZVAL(subpats) Z_PARAM_LONG(flags) Z_PARAM_LONG(start_offset) - ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); + ZEND_PARSE_PARAMETERS_END(); /* Compile regex or get it from cache. */ if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) { @@ -2308,8 +2303,8 @@ static void preg_replace_common(INTERNAL_FUNCTION_PARAMETERS, int is_filter) } } else { if (Z_TYPE_P(regex) != IS_ARRAY) { - php_error_docref(NULL, E_WARNING, "Parameter mismatch, pattern is a string while replacement is an array"); - RETURN_FALSE; + zend_type_error("Parameter mismatch, pattern is a string while replacement is an array"); + RETURN_THROWS(); } } @@ -2510,7 +2505,7 @@ static PHP_FUNCTION(preg_split) Z_PARAM_OPTIONAL Z_PARAM_LONG(limit_val) Z_PARAM_LONG(flags) - ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE); + ZEND_PARSE_PARAMETERS_END(); /* Compile regex or get it from cache. */ if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) { @@ -2978,72 +2973,18 @@ static PHP_FUNCTION(preg_last_error) } /* }}} */ -/* {{{ module definition structures */ +/* {{{ proto string preg_last_error_msg() + Returns the error message of the last regexp execution. */ +static PHP_FUNCTION(preg_last_error_msg) +{ + ZEND_PARSE_PARAMETERS_NONE(); -/* {{{ arginfo */ -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_match, 0, 0, 2) - ZEND_ARG_INFO(0, pattern) - ZEND_ARG_INFO(0, subject) - ZEND_ARG_INFO(1, subpatterns) /* array */ - ZEND_ARG_INFO(0, flags) - ZEND_ARG_INFO(0, offset) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_match_all, 0, 0, 2) - ZEND_ARG_INFO(0, pattern) - ZEND_ARG_INFO(0, subject) - ZEND_ARG_INFO(1, subpatterns) /* array */ - ZEND_ARG_INFO(0, flags) - ZEND_ARG_INFO(0, offset) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_replace, 0, 0, 3) - ZEND_ARG_INFO(0, regex) - ZEND_ARG_INFO(0, replace) - ZEND_ARG_INFO(0, subject) - ZEND_ARG_INFO(0, limit) - ZEND_ARG_INFO(1, count) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_replace_callback, 0, 0, 3) - ZEND_ARG_INFO(0, regex) - ZEND_ARG_INFO(0, callback) - ZEND_ARG_INFO(0, subject) - ZEND_ARG_INFO(0, limit) - ZEND_ARG_INFO(1, count) - ZEND_ARG_INFO(0, flags) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_replace_callback_array, 0, 0, 2) - ZEND_ARG_INFO(0, pattern) - ZEND_ARG_INFO(0, subject) - ZEND_ARG_INFO(0, limit) - ZEND_ARG_INFO(1, count) - ZEND_ARG_INFO(0, flags) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_split, 0, 0, 2) - ZEND_ARG_INFO(0, pattern) - ZEND_ARG_INFO(0, subject) - ZEND_ARG_INFO(0, limit) - ZEND_ARG_INFO(0, flags) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_quote, 0, 0, 1) - ZEND_ARG_INFO(0, str) - ZEND_ARG_INFO(0, delim_char) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_preg_grep, 0, 0, 2) - ZEND_ARG_INFO(0, regex) - ZEND_ARG_INFO(0, input) /* array */ - ZEND_ARG_INFO(0, flags) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO(arginfo_preg_last_error, 0) -ZEND_END_ARG_INFO() + RETURN_STRING(php_pcre_get_error_msg(PCRE_G(error_code))); +} /* }}} */ +/* {{{ module definition structures */ + static const zend_function_entry pcre_functions[] = { PHP_FE(preg_match, arginfo_preg_match) PHP_FE(preg_match_all, arginfo_preg_match_all) @@ -3055,6 +2996,7 @@ static const zend_function_entry pcre_functions[] = { PHP_FE(preg_quote, arginfo_preg_quote) PHP_FE(preg_grep, arginfo_preg_grep) PHP_FE(preg_last_error, arginfo_preg_last_error) + PHP_FE(preg_last_error_msg, arginfo_preg_last_error_msg) PHP_FE_END }; |