From: Nikita Popov Date: Tue, 22 Sep 2020 09:19:02 +0000 (+0200) Subject: Validate phonemes parameter of metaphone() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84be22f1f5db499001379f762869d00fc3bc6f55;p=php Validate phonemes parameter of metaphone() And thus avoid the false return value. --- diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index ecc97686d3..74a049831c 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -494,7 +494,7 @@ function inet_pton(string $ip_address): string|false {} /* metaphone.c */ -function metaphone(string $text, int $phones = 0): string|false {} +function metaphone(string $text, int $phonemes = 0): string {} /* {{{ head.c */ function header(string $string, bool $replace = true, int $http_response_code = 0): void {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 0f96cb302c..5c63fb509a 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 5e8cd8c22edadede2814b26b6fb984f1c3626850 */ + * Stub hash: 5d9126adf07f6f480b5879f551de466140b98462 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -725,9 +725,9 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_inet_pton, 0, 1, MAY_BE_STRING|M ZEND_END_ARG_INFO() #endif -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_metaphone, 0, 1, MAY_BE_STRING|MAY_BE_FALSE) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_metaphone, 0, 1, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, text, IS_STRING, 0) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, phones, IS_LONG, 0, "0") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, phonemes, IS_LONG, 0, "0") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_header, 0, 1, IS_VOID, 0) diff --git a/ext/standard/metaphone.c b/ext/standard/metaphone.c index 1665caddce..573bf9a4b0 100644 --- a/ext/standard/metaphone.c +++ b/ext/standard/metaphone.c @@ -20,7 +20,7 @@ #include "php.h" -static int metaphone(unsigned char *word, size_t word_len, zend_long max_phonemes, zend_string **phoned_word, int traditional); +static void metaphone(unsigned char *word, size_t word_len, zend_long max_phonemes, zend_string **phoned_word, int traditional); /* {{{ Break english phrases down into their phonemes */ PHP_FUNCTION(metaphone) @@ -35,14 +35,13 @@ PHP_FUNCTION(metaphone) Z_PARAM_LONG(phones) ZEND_PARSE_PARAMETERS_END(); - if (metaphone((unsigned char *)ZSTR_VAL(str), ZSTR_LEN(str), phones, &result, 1) == 0) { - RETVAL_STR(result); - } else { - if (result) { - zend_string_free(result); - } - RETURN_FALSE; + if (phones < 0) { + zend_argument_value_error(2, "must be greater than or equal to 0"); + RETURN_THROWS(); } + + metaphone((unsigned char *)ZSTR_VAL(str), ZSTR_LEN(str), phones, &result, 1); + RETVAL_STR(result); } /* }}} */ @@ -145,7 +144,7 @@ static char Lookahead(char *word, int how_far) ZSTR_LEN(*phoned_word) = p_idx; \ } /* Slap a null character on the end of the phoned word */ -#define End_Phoned_Word { \ +#define End_Phoned_Word() { \ if (p_idx == max_buffer_len) { \ *phoned_word = zend_string_extend(*phoned_word, 1 * sizeof(char) + max_buffer_len, 0); \ max_buffer_len += 1; \ @@ -160,24 +159,13 @@ static char Lookahead(char *word, int how_far) #define Isbreak(c) (!isalpha(c)) /* {{{ metaphone */ -static int metaphone(unsigned char *word, size_t word_len, zend_long max_phonemes, zend_string **phoned_word, int traditional) +static void metaphone(unsigned char *word, size_t word_len, zend_long max_phonemes, zend_string **phoned_word, int traditional) { int w_idx = 0; /* point in the phonization we're at. */ size_t p_idx = 0; /* end of the phoned phrase */ size_t max_buffer_len = 0; /* maximum length of the destination buffer */ - -/*-- Parameter checks --*/ - /* Negative phoneme length is meaningless */ - - if (max_phonemes < 0) - return -1; - - /* Empty/null string is meaningless */ - /* Overly paranoid */ - /* assert(word != NULL && word[0] != '\0'); */ - - if (word == NULL) - return -1; + ZEND_ASSERT(word != NULL); + ZEND_ASSERT(max_phonemes >= 0); /*-- Allocate memory for our phoned_phrase --*/ if (max_phonemes == 0) { /* Assume largest possible */ @@ -194,8 +182,8 @@ static int metaphone(unsigned char *word, size_t word_len, zend_long max_phoneme for (; !isalpha(Curr_Letter); w_idx++) { /* On the off chance we were given nothing but crap... */ if (Curr_Letter == '\0') { - End_Phoned_Word - return SUCCESS; /* For testing */ + End_Phoned_Word(); + return; } } @@ -460,8 +448,6 @@ static int metaphone(unsigned char *word, size_t word_len, zend_long max_phoneme w_idx += skip_letter; } /* END FOR */ - End_Phoned_Word; - - return 0; + End_Phoned_Word(); } /* END metaphone */ /* }}} */ diff --git a/ext/standard/tests/strings/metaphone.phpt b/ext/standard/tests/strings/metaphone.phpt index a6b52e3934..0356c1fd69 100644 --- a/ext/standard/tests/strings/metaphone.phpt +++ b/ext/standard/tests/strings/metaphone.phpt @@ -5,9 +5,12 @@ metaphone() tests var_dump(metaphone("")); var_dump(metaphone(-1)); -var_dump(metaphone(-1, -1)); -var_dump(metaphone("valid phrase", -1)); +try { + var_dump(metaphone("valid phrase", -1)); +} catch (ValueError $e) { + echo $e->getMessage(), "\n"; +} var_dump(metaphone("valid phrase", 0)); var_dump(metaphone("valid phrase", 10000)); @@ -27,8 +30,7 @@ echo "Done\n"; --EXPECT-- string(0) "" string(0) "" -bool(false) -bool(false) +metaphone(): Argument #2 ($phonemes) must be greater than or equal to 0 string(6) "FLTFRS" string(6) "FLTFRS" string(26) "0FLFRWRTKRFLNKHTLSLN0KLTR0"