From c49e0403f03c3901e2f766b10f6fa67ac19c9654 Mon Sep 17 00:00:00 2001 From: Masaki Kagaya Date: Sat, 19 Jul 2025 19:15:36 +0900 Subject: [PATCH 1/6] filter: Add FILTER_VALIDATE_STR filter for string length validation Implements min/max string length validation according to Unicode Standard 3.9.6. Adds Unicode and invalid UTF-8 test cases. --- ext/filter/filter.c | 1 + ext/filter/filter.stub.php | 5 +++ ext/filter/filter_arginfo.h | 3 +- ext/filter/filter_private.h | 3 +- ext/filter/logical_filters.c | 42 ++++++++++++++++++ ext/filter/php_filter.h | 1 + ext/filter/tests/validate_str_basic.phpt | 26 +++++++++++ .../tests/validate_str_invalid_type.phpt | 43 +++++++++++++++++++ ext/filter/tests/validate_str_unicode.phpt | 35 +++++++++++++++ 9 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 ext/filter/tests/validate_str_basic.phpt create mode 100644 ext/filter/tests/validate_str_invalid_type.phpt create mode 100644 ext/filter/tests/validate_str_unicode.phpt diff --git a/ext/filter/filter.c b/ext/filter/filter.c index 7f3624eb4c07a..2e96119d4ae18 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -49,6 +49,7 @@ static const filter_list_entry filter_list[] = { { "validate_email", FILTER_VALIDATE_EMAIL, php_filter_validate_email }, { "validate_ip", FILTER_VALIDATE_IP, php_filter_validate_ip }, { "validate_mac", FILTER_VALIDATE_MAC, php_filter_validate_mac }, + { "validate_str", FILTER_VALIDATE_STR, php_filter_validate_str }, { "string", FILTER_SANITIZE_STRING, php_filter_string }, { "stripped", FILTER_SANITIZE_STRING, php_filter_string }, diff --git a/ext/filter/filter.stub.php b/ext/filter/filter.stub.php index a8790bb7cd615..9414ab718e8b9 100644 --- a/ext/filter/filter.stub.php +++ b/ext/filter/filter.stub.php @@ -106,6 +106,11 @@ * @cvalue FILTER_VALIDATE_MAC */ const FILTER_VALIDATE_MAC = UNKNOWN; +/** + * @var int + * @cvalue FILTER_VALIDATE_STR + */ +const FILTER_VALIDATE_STR = UNKNOWN; /** * @var int diff --git a/ext/filter/filter_arginfo.h b/ext/filter/filter_arginfo.h index 32a5c87184a87..8df955b51da67 100644 --- a/ext/filter/filter_arginfo.h +++ b/ext/filter/filter_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: a0f9a546d59bb27854af79a92e353f118ca6bdaf */ + * Stub hash: c75c8a3d2e37b201d31f37cc3ed15fbf41d37f54 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_has_var, 0, 2, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, input_type, IS_LONG, 0) @@ -79,6 +79,7 @@ static void register_filter_symbols(int module_number) REGISTER_LONG_CONSTANT("FILTER_VALIDATE_EMAIL", FILTER_VALIDATE_EMAIL, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_VALIDATE_IP", FILTER_VALIDATE_IP, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_VALIDATE_MAC", FILTER_VALIDATE_MAC, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("FILTER_VALIDATE_STR", FILTER_VALIDATE_STR, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_DEFAULT", FILTER_DEFAULT, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_UNSAFE_RAW", FILTER_UNSAFE_RAW, CONST_PERSISTENT); zend_constant *const_FILTER_SANITIZE_STRING = REGISTER_LONG_CONSTANT("FILTER_SANITIZE_STRING", FILTER_SANITIZE_STRING, CONST_PERSISTENT | CONST_DEPRECATED); diff --git a/ext/filter/filter_private.h b/ext/filter/filter_private.h index a1bbb500f8db4..8bd7a97af2201 100644 --- a/ext/filter/filter_private.h +++ b/ext/filter/filter_private.h @@ -66,7 +66,8 @@ #define FILTER_VALIDATE_IP 0x0113 #define FILTER_VALIDATE_MAC 0x0114 #define FILTER_VALIDATE_DOMAIN 0x0115 -#define FILTER_VALIDATE_LAST 0x0115 +#define FILTER_VALIDATE_STR 0x0116 +#define FILTER_VALIDATE_LAST 0x0116 #define FILTER_VALIDATE_ALL 0x0100 diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c index 0dd307122345c..0802257b3d989 100644 --- a/ext/filter/logical_filters.c +++ b/ext/filter/logical_filters.c @@ -19,6 +19,7 @@ #include "php_filter.h" #include "filter_private.h" #include "ext/standard/url.h" +#include "ext/standard/html.h" #include "ext/pcre/php_pcre.h" #include "zend_multiply.h" @@ -1091,3 +1092,44 @@ void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ } } /* }}} */ + +/** + * Returns the number of Unicode codepoints (including U+FFFD for invalid sequences) + * in a UTF-8 encoded string. + */ +static size_t php_utf8_strlen(const unsigned char *str, size_t str_len) +{ + size_t len = 0, cursor = 0; + zend_result status; + + while (cursor < str_len) { + php_next_utf8_char(str, str_len, &cursor, &status); + len++; + } + + return len; +} + + +void php_filter_validate_str(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ +{ + int min_range_set, max_range_set; + zval *option_val; + zend_long min_range, max_range; + size_t len; + const char *str = Z_STRVAL_P(value); + size_t str_size = Z_STRLEN_P(value); + + FETCH_LONG_OPTION(min_range, "min_range"); + FETCH_LONG_OPTION(max_range, "max_range"); + + len = php_utf8_strlen((const unsigned char *)str, str_size); + + if (min_range_set && len < min_range) { + RETURN_VALIDATION_FAILED; + } + if (max_range_set && max_range < len) { + RETURN_VALIDATION_FAILED; + } +} +/* }}} */ \ No newline at end of file diff --git a/ext/filter/php_filter.h b/ext/filter/php_filter.h index f782907898fca..3cad86874a70a 100644 --- a/ext/filter/php_filter.h +++ b/ext/filter/php_filter.h @@ -62,6 +62,7 @@ void php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL); void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL); void php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL); void php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL); +void php_filter_validate_str(PHP_INPUT_FILTER_PARAM_DECL); void php_filter_string(PHP_INPUT_FILTER_PARAM_DECL); void php_filter_encoded(PHP_INPUT_FILTER_PARAM_DECL); diff --git a/ext/filter/tests/validate_str_basic.phpt b/ext/filter/tests/validate_str_basic.phpt new file mode 100644 index 0000000000000..14d38ccc647fe --- /dev/null +++ b/ext/filter/tests/validate_str_basic.phpt @@ -0,0 +1,26 @@ +--TEST-- +FILTER_VALIDATE_STR: Emoji string length +--SKIPIF-- + +--FILE-- + ['min_range' => 2, 'max_range' => 2]]; +$options2 = ['options' => ['max_range' => 2, 'default' => 'error']]; +$options3 = ['options' => ['min_range' => 0]]; + +var_dump( + filter_var('ab', filter_id('validate_str'), $options1), + filter_var('🐘🐘', FILTER_VALIDATE_STR, $options1), + filter_var('🐘', FILTER_VALIDATE_STR, $options1), + filter_var('🐘🐘🐘', FILTER_VALIDATE_STR, $options1), + filter_var('🐘🐘🐘', FILTER_VALIDATE_STR, $options2), + filter_var('', FILTER_VALIDATE_STR, $options3), +); +?> +--EXPECT-- +string(2) "ab" +string(8) "🐘🐘" +bool(false) +bool(false) +string(5) "error" +string(0) "" diff --git a/ext/filter/tests/validate_str_invalid_type.phpt b/ext/filter/tests/validate_str_invalid_type.phpt new file mode 100644 index 0000000000000..21ace085afcd7 --- /dev/null +++ b/ext/filter/tests/validate_str_invalid_type.phpt @@ -0,0 +1,43 @@ +--TEST-- +FILTER_VALIDATE_STR: Invalid input types +--SKIPIF-- + +--FILE-- + ['min_range' => 2, 'max_range' => 4]]; +$handle = fopen("php://memory", "r"); +class Dummy { public $x = 1; } + +var_dump( + filter_var(1234, FILTER_VALIDATE_STR, $options), + filter_var(3.14, FILTER_VALIDATE_STR, $options), + filter_var(['a', 'b'], FILTER_VALIDATE_STR, $options), + filter_var(new Dummy(), FILTER_VALIDATE_STR, $options), + filter_var(NULL, FILTER_VALIDATE_STR, $options), + filter_var(true, FILTER_VALIDATE_STR, $options), + filter_var(false, FILTER_VALIDATE_STR, $options), + filter_var($handle, FILTER_VALIDATE_STR, $options) +); + +fclose($handle); + +?> +--EXPECT-- +string(4) "1234" +string(4) "3.14" +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) +bool(false) diff --git a/ext/filter/tests/validate_str_unicode.phpt b/ext/filter/tests/validate_str_unicode.phpt new file mode 100644 index 0000000000000..2e4486f51e974 --- /dev/null +++ b/ext/filter/tests/validate_str_unicode.phpt @@ -0,0 +1,35 @@ +--TEST-- +FILTER_VALIDATE_STR: Unicode maximal subpart validation (Table 3-11 reference) +--SKIPIF-- + +--FILE-- + ['min_range' => 2, 'max_range' => 2]]; +$options2 = ['options' => ['max_range' => 5]]; + +echo bin2hex(filter_var("a\x80", FILTER_VALIDATE_STR, $options1)), "\n"; +echo bin2hex(filter_var("\xE1\x80\xE2\xF0\x91\x92\xF1\xBF\x41", FILTER_VALIDATE_STR, $options2)), "\n"; +?> +--EXPECT-- +6180 +e180e2f09192f1bf41 From 81aa54cc3b9a50bc8c64a24b9e2c3af905411bbf Mon Sep 17 00:00:00 2001 From: Masaki Kagaya Date: Fri, 13 Mar 2026 06:50:21 +0900 Subject: [PATCH 2/6] Filter: Clarify comment for php_utf8_strlen() --- ext/filter/logical_filters.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c index 88d9bd8accbd9..35aff5d18bd45 100644 --- a/ext/filter/logical_filters.c +++ b/ext/filter/logical_filters.c @@ -1125,8 +1125,8 @@ zend_result php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ /* }}} */ /** - * Returns the number of Unicode codepoints (including U+FFFD for invalid sequences) - * in a UTF-8 encoded string. + * Returns the number of Unicode code points in a UTF-8 encoded string. + * Invalid UTF-8 byte sequences (U+FFFD) are counted as one replacement character. */ static size_t php_utf8_strlen(const unsigned char *str, size_t str_len) { From ae9d58b7cab85d2eb4e53dbe3e4ba33cf04a2961 Mon Sep 17 00:00:00 2001 From: Masaki Kagaya Date: Fri, 13 Mar 2026 06:59:17 +0900 Subject: [PATCH 3/6] Filter: Validate min_range and max_range option values --- ext/filter/logical_filters.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c index 35aff5d18bd45..20f4bf35a703c 100644 --- a/ext/filter/logical_filters.c +++ b/ext/filter/logical_filters.c @@ -21,6 +21,7 @@ #include "filter_private.h" #include "ext/pcre/php_pcre.h" #include "ext/uri/php_uri.h" +#include "ext/standard/html.h" #include "zend_multiply.h" @@ -1154,6 +1155,24 @@ zend_result php_filter_validate_str(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ FETCH_LONG_OPTION(min_range, "min_range"); FETCH_LONG_OPTION(max_range, "max_range"); + if (min_range_set && min_range < 0) { + php_error_docref(NULL, E_WARNING, + "min_range must be greater than or equal to 0"); + RETURN_VALIDATION_FAILED; + } + + if (max_range_set && max_range < 0) { + php_error_docref(NULL, E_WARNING, + "max_range must be greater than or equal to 0"); + RETURN_VALIDATION_FAILED; + } + + if (min_range_set && max_range_set && min_range > max_range) { + php_error_docref(NULL, E_WARNING, + "min_range must be less than or equal to max_range"); + RETURN_VALIDATION_FAILED; + } + len = php_utf8_strlen((const unsigned char *)str, str_size); if (min_range_set && len < min_range) { From d827d36e0ac4a23e971bf5d857162b1046e3d8e8 Mon Sep 17 00:00:00 2001 From: Masaki Kagaya Date: Fri, 13 Mar 2026 07:07:43 +0900 Subject: [PATCH 4/6] Filter: Rename min_range/max_range options to min_len/max_len --- ext/filter/logical_filters.c | 24 +++++++++---------- ext/filter/tests/validate_str_basic.phpt | 6 ++--- .../tests/validate_str_invalid_type.phpt | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c index 20f4bf35a703c..dca21a598348d 100644 --- a/ext/filter/logical_filters.c +++ b/ext/filter/logical_filters.c @@ -1145,40 +1145,40 @@ static size_t php_utf8_strlen(const unsigned char *str, size_t str_len) zend_result php_filter_validate_str(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ { - int min_range_set, max_range_set; + int min_len_set, max_len_set; zval *option_val; - zend_long min_range, max_range; + zend_long min_len, max_len; size_t len; const char *str = Z_STRVAL_P(value); size_t str_size = Z_STRLEN_P(value); - FETCH_LONG_OPTION(min_range, "min_range"); - FETCH_LONG_OPTION(max_range, "max_range"); + FETCH_LONG_OPTION(min_len, "min_len"); + FETCH_LONG_OPTION(max_len, "max_len"); - if (min_range_set && min_range < 0) { + if (min_len_set && min_len < 0) { php_error_docref(NULL, E_WARNING, - "min_range must be greater than or equal to 0"); + "min_len must be greater than or equal to 0"); RETURN_VALIDATION_FAILED; } - if (max_range_set && max_range < 0) { + if (max_len_set && max_len < 0) { php_error_docref(NULL, E_WARNING, - "max_range must be greater than or equal to 0"); + "max_len must be greater than or equal to 0"); RETURN_VALIDATION_FAILED; } - if (min_range_set && max_range_set && min_range > max_range) { + if (min_len_set && max_len_set && min_len > max_len) { php_error_docref(NULL, E_WARNING, - "min_range must be less than or equal to max_range"); + "min_len must be less than or equal to max_len"); RETURN_VALIDATION_FAILED; } len = php_utf8_strlen((const unsigned char *)str, str_size); - if (min_range_set && len < min_range) { + if (min_len_set && len < min_len) { RETURN_VALIDATION_FAILED; } - if (max_range_set && max_range < len) { + if (max_len_set && max_len < len) { RETURN_VALIDATION_FAILED; } return SUCCESS; diff --git a/ext/filter/tests/validate_str_basic.phpt b/ext/filter/tests/validate_str_basic.phpt index 14d38ccc647fe..c5f9c9576f2dc 100644 --- a/ext/filter/tests/validate_str_basic.phpt +++ b/ext/filter/tests/validate_str_basic.phpt @@ -4,9 +4,9 @@ FILTER_VALIDATE_STR: Emoji string length --FILE-- ['min_range' => 2, 'max_range' => 2]]; -$options2 = ['options' => ['max_range' => 2, 'default' => 'error']]; -$options3 = ['options' => ['min_range' => 0]]; +$options1 = ['options' => ['min_len' => 2, 'max_len' => 2]]; +$options2 = ['options' => ['max_len' => 2, 'default' => 'error']]; +$options3 = ['options' => ['min_len' => 0]]; var_dump( filter_var('ab', filter_id('validate_str'), $options1), diff --git a/ext/filter/tests/validate_str_invalid_type.phpt b/ext/filter/tests/validate_str_invalid_type.phpt index 21ace085afcd7..26edb788bd919 100644 --- a/ext/filter/tests/validate_str_invalid_type.phpt +++ b/ext/filter/tests/validate_str_invalid_type.phpt @@ -14,7 +14,7 @@ FILTER_VALIDATE_STR: Invalid input types * This test ensures FILTER_VALIDATE_STR behaves accordingly for various input types. */ -$options = ['options' => ['min_range' => 2, 'max_range' => 4]]; +$options = ['options' => ['min_len' => 2, 'max_len' => 4]]; $handle = fopen("php://memory", "r"); class Dummy { public $x = 1; } From 3902faedd25541463f705bacbec2889ce114bafe Mon Sep 17 00:00:00 2001 From: Masaki Kagaya Date: Fri, 13 Mar 2026 07:26:08 +0900 Subject: [PATCH 5/6] Filter: Add tests for invalid min_len/max_len options --- .../tests/validate_str_invalid_options.phpt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 ext/filter/tests/validate_str_invalid_options.phpt diff --git a/ext/filter/tests/validate_str_invalid_options.phpt b/ext/filter/tests/validate_str_invalid_options.phpt new file mode 100644 index 0000000000000..79e8af1607f26 --- /dev/null +++ b/ext/filter/tests/validate_str_invalid_options.phpt @@ -0,0 +1,37 @@ +--TEST-- +FILTER_VALIDATE_STR: invalid min_len/max_len options +--FILE-- + ["min_len" => -1] +])); + +echo "--- max_len negative ---\n"; +var_dump(filter_var("abc", FILTER_VALIDATE_STR, [ + "options" => ["max_len" => -1] +])); + +echo "--- min_len greater than max_len ---\n"; +var_dump(filter_var("abc", FILTER_VALIDATE_STR, [ + "options" => [ + "min_len" => 10, + "max_len" => 5 + ] +])); + +?> +--EXPECTF-- +--- min_len negative --- + +Warning: filter_var(): min_len must be greater than or equal to 0 in %s on line %d +bool(false) +--- max_len negative --- + +Warning: filter_var(): max_len must be greater than or equal to 0 in %s on line %d +bool(false) +--- min_len greater than max_len --- + +Warning: filter_var(): min_len must be less than or equal to max_len in %s on line %d +bool(false) \ No newline at end of file From ceab7eea726f2019309cb2b16561011d1b854017 Mon Sep 17 00:00:00 2001 From: Masaki Kagaya Date: Fri, 13 Mar 2026 11:05:33 +0900 Subject: [PATCH 6/6] filter: Rename FILTER_VALIDATE_STR to FILTER_VALIDATE_STRLEN The validator checks the length of a UTF-8 string in Unicode code points. Renaming improves clarity and aligns the filter name with the min_len / max_len options. --- ext/filter/filter.c | 2 +- ext/filter/filter.stub.php | 4 ++-- ext/filter/filter_arginfo.h | 4 ++-- ext/filter/filter_private.h | 2 +- ext/filter/logical_filters.c | 2 +- ext/filter/php_filter.h | 2 +- ext/filter/tests/validate_str_basic.phpt | 12 ++++++------ .../tests/validate_str_invalid_options.phpt | 8 ++++---- .../tests/validate_str_invalid_type.phpt | 18 +++++++++--------- ext/filter/tests/validate_str_unicode.phpt | 4 ++-- 10 files changed, 29 insertions(+), 29 deletions(-) diff --git a/ext/filter/filter.c b/ext/filter/filter.c index 5304453c409bf..056ad95b750cf 100644 --- a/ext/filter/filter.c +++ b/ext/filter/filter.c @@ -50,7 +50,7 @@ static const filter_list_entry filter_list[] = { { "validate_email", FILTER_VALIDATE_EMAIL, php_filter_validate_email }, { "validate_ip", FILTER_VALIDATE_IP, php_filter_validate_ip }, { "validate_mac", FILTER_VALIDATE_MAC, php_filter_validate_mac }, - { "validate_str", FILTER_VALIDATE_STR, php_filter_validate_str }, + { "validate_strlen", FILTER_VALIDATE_STRLEN, php_filter_validate_strlen }, { "string", FILTER_SANITIZE_STRING, php_filter_string }, { "stripped", FILTER_SANITIZE_STRING, php_filter_string }, diff --git a/ext/filter/filter.stub.php b/ext/filter/filter.stub.php index 7054d7f01709c..ec8cf135e9409 100644 --- a/ext/filter/filter.stub.php +++ b/ext/filter/filter.stub.php @@ -114,9 +114,9 @@ const FILTER_VALIDATE_MAC = UNKNOWN; /** * @var int - * @cvalue FILTER_VALIDATE_STR + * @cvalue FILTER_VALIDATE_STRLEN */ -const FILTER_VALIDATE_STR = UNKNOWN; +const FILTER_VALIDATE_STRLEN = UNKNOWN; /** * @var int diff --git a/ext/filter/filter_arginfo.h b/ext/filter/filter_arginfo.h index 48eae50c085d1..f638fef91e71f 100644 --- a/ext/filter/filter_arginfo.h +++ b/ext/filter/filter_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit filter.stub.php instead. - * Stub hash: c3eb55dfec619af1e46be206f51a2b0893ed399f */ + * Stub hash: 50101ce180f08678a6d921a5d6dcb2fc94e97a17 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_filter_has_var, 0, 2, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, input_type, IS_LONG, 0) @@ -80,7 +80,7 @@ static void register_filter_symbols(int module_number) REGISTER_LONG_CONSTANT("FILTER_VALIDATE_EMAIL", FILTER_VALIDATE_EMAIL, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_VALIDATE_IP", FILTER_VALIDATE_IP, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_VALIDATE_MAC", FILTER_VALIDATE_MAC, CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("FILTER_VALIDATE_STR", FILTER_VALIDATE_STR, CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("FILTER_VALIDATE_STRLEN", FILTER_VALIDATE_STRLEN, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_DEFAULT", FILTER_DEFAULT, CONST_PERSISTENT); REGISTER_LONG_CONSTANT("FILTER_UNSAFE_RAW", FILTER_UNSAFE_RAW, CONST_PERSISTENT); zend_constant *const_FILTER_SANITIZE_STRING = REGISTER_LONG_CONSTANT("FILTER_SANITIZE_STRING", FILTER_SANITIZE_STRING, CONST_PERSISTENT | CONST_DEPRECATED); diff --git a/ext/filter/filter_private.h b/ext/filter/filter_private.h index 429e7a851efda..dac410bb27c24 100644 --- a/ext/filter/filter_private.h +++ b/ext/filter/filter_private.h @@ -67,7 +67,7 @@ #define FILTER_VALIDATE_IP 0x0113 #define FILTER_VALIDATE_MAC 0x0114 #define FILTER_VALIDATE_DOMAIN 0x0115 -#define FILTER_VALIDATE_STR 0x0116 +#define FILTER_VALIDATE_STRLEN 0x0116 #define FILTER_VALIDATE_LAST 0x0116 #define FILTER_VALIDATE_ALL 0x0100 diff --git a/ext/filter/logical_filters.c b/ext/filter/logical_filters.c index dca21a598348d..474dd99acac52 100644 --- a/ext/filter/logical_filters.c +++ b/ext/filter/logical_filters.c @@ -1143,7 +1143,7 @@ static size_t php_utf8_strlen(const unsigned char *str, size_t str_len) } -zend_result php_filter_validate_str(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ +zend_result php_filter_validate_strlen(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */ { int min_len_set, max_len_set; zval *option_val; diff --git a/ext/filter/php_filter.h b/ext/filter/php_filter.h index ae6a62dad317c..e5035be2abefb 100644 --- a/ext/filter/php_filter.h +++ b/ext/filter/php_filter.h @@ -62,7 +62,7 @@ zend_result php_filter_validate_url(PHP_INPUT_FILTER_PARAM_DECL); zend_result php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL); zend_result php_filter_validate_ip(PHP_INPUT_FILTER_PARAM_DECL); zend_result php_filter_validate_mac(PHP_INPUT_FILTER_PARAM_DECL); -zend_result php_filter_validate_str(PHP_INPUT_FILTER_PARAM_DECL); +zend_result php_filter_validate_strlen(PHP_INPUT_FILTER_PARAM_DECL); zend_result php_filter_string(PHP_INPUT_FILTER_PARAM_DECL); zend_result php_filter_encoded(PHP_INPUT_FILTER_PARAM_DECL); diff --git a/ext/filter/tests/validate_str_basic.phpt b/ext/filter/tests/validate_str_basic.phpt index c5f9c9576f2dc..4b5230113ebf9 100644 --- a/ext/filter/tests/validate_str_basic.phpt +++ b/ext/filter/tests/validate_str_basic.phpt @@ -9,12 +9,12 @@ $options2 = ['options' => ['max_len' => 2, 'default' => 'error']]; $options3 = ['options' => ['min_len' => 0]]; var_dump( - filter_var('ab', filter_id('validate_str'), $options1), - filter_var('🐘🐘', FILTER_VALIDATE_STR, $options1), - filter_var('🐘', FILTER_VALIDATE_STR, $options1), - filter_var('🐘🐘🐘', FILTER_VALIDATE_STR, $options1), - filter_var('🐘🐘🐘', FILTER_VALIDATE_STR, $options2), - filter_var('', FILTER_VALIDATE_STR, $options3), + filter_var('ab', filter_id('validate_strlen'), $options1), + filter_var('🐘🐘', FILTER_VALIDATE_STRLEN, $options1), + filter_var('🐘', FILTER_VALIDATE_STRLEN, $options1), + filter_var('🐘🐘🐘', FILTER_VALIDATE_STRLEN, $options1), + filter_var('🐘🐘🐘', FILTER_VALIDATE_STRLEN, $options2), + filter_var('', FILTER_VALIDATE_STRLEN, $options3), ); ?> --EXPECT-- diff --git a/ext/filter/tests/validate_str_invalid_options.phpt b/ext/filter/tests/validate_str_invalid_options.phpt index 79e8af1607f26..098f8d0509eb1 100644 --- a/ext/filter/tests/validate_str_invalid_options.phpt +++ b/ext/filter/tests/validate_str_invalid_options.phpt @@ -4,17 +4,17 @@ FILTER_VALIDATE_STR: invalid min_len/max_len options ["min_len" => -1] ])); echo "--- max_len negative ---\n"; -var_dump(filter_var("abc", FILTER_VALIDATE_STR, [ +var_dump(filter_var("abc", FILTER_VALIDATE_STRLEN, [ "options" => ["max_len" => -1] ])); echo "--- min_len greater than max_len ---\n"; -var_dump(filter_var("abc", FILTER_VALIDATE_STR, [ +var_dump(filter_var("abc", FILTER_VALIDATE_STRLEN, [ "options" => [ "min_len" => 10, "max_len" => 5 @@ -34,4 +34,4 @@ bool(false) --- min_len greater than max_len --- Warning: filter_var(): min_len must be less than or equal to max_len in %s on line %d -bool(false) \ No newline at end of file +bool(false) diff --git a/ext/filter/tests/validate_str_invalid_type.phpt b/ext/filter/tests/validate_str_invalid_type.phpt index 26edb788bd919..ca513cc1cae83 100644 --- a/ext/filter/tests/validate_str_invalid_type.phpt +++ b/ext/filter/tests/validate_str_invalid_type.phpt @@ -11,7 +11,7 @@ FILTER_VALIDATE_STR: Invalid input types * - Scalar types (int, float, bool) are automatically converted to strings before filtering. * - Non-scalar types (array, object, resource, null) always result in false. * - * This test ensures FILTER_VALIDATE_STR behaves accordingly for various input types. + * This test ensures FILTER_VALIDATE_STRLEN behaves accordingly for various input types. */ $options = ['options' => ['min_len' => 2, 'max_len' => 4]]; @@ -19,14 +19,14 @@ $handle = fopen("php://memory", "r"); class Dummy { public $x = 1; } var_dump( - filter_var(1234, FILTER_VALIDATE_STR, $options), - filter_var(3.14, FILTER_VALIDATE_STR, $options), - filter_var(['a', 'b'], FILTER_VALIDATE_STR, $options), - filter_var(new Dummy(), FILTER_VALIDATE_STR, $options), - filter_var(NULL, FILTER_VALIDATE_STR, $options), - filter_var(true, FILTER_VALIDATE_STR, $options), - filter_var(false, FILTER_VALIDATE_STR, $options), - filter_var($handle, FILTER_VALIDATE_STR, $options) + filter_var(1234, FILTER_VALIDATE_STRLEN, $options), + filter_var(3.14, FILTER_VALIDATE_STRLEN, $options), + filter_var(['a', 'b'], FILTER_VALIDATE_STRLEN, $options), + filter_var(new Dummy(), FILTER_VALIDATE_STRLEN, $options), + filter_var(NULL, FILTER_VALIDATE_STRLEN, $options), + filter_var(true, FILTER_VALIDATE_STRLEN, $options), + filter_var(false, FILTER_VALIDATE_STRLEN, $options), + filter_var($handle, FILTER_VALIDATE_STRLEN, $options) ); fclose($handle); diff --git a/ext/filter/tests/validate_str_unicode.phpt b/ext/filter/tests/validate_str_unicode.phpt index 2e4486f51e974..efb7cd1fdfb56 100644 --- a/ext/filter/tests/validate_str_unicode.phpt +++ b/ext/filter/tests/validate_str_unicode.phpt @@ -27,8 +27,8 @@ FILTER_VALIDATE_STR: Unicode maximal subpart validation (Table 3-11 reference) $options1 = ['options' => ['min_range' => 2, 'max_range' => 2]]; $options2 = ['options' => ['max_range' => 5]]; -echo bin2hex(filter_var("a\x80", FILTER_VALIDATE_STR, $options1)), "\n"; -echo bin2hex(filter_var("\xE1\x80\xE2\xF0\x91\x92\xF1\xBF\x41", FILTER_VALIDATE_STR, $options2)), "\n"; +echo bin2hex(filter_var("a\x80", FILTER_VALIDATE_STRLEN, $options1)), "\n"; +echo bin2hex(filter_var("\xE1\x80\xE2\xF0\x91\x92\xF1\xBF\x41", FILTER_VALIDATE_STRLEN, $options2)), "\n"; ?> --EXPECT-- 6180