]> granicus.if.org Git - php/commitdiff
Validate phonemes parameter of metaphone()
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 22 Sep 2020 09:19:02 +0000 (11:19 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 22 Sep 2020 09:19:02 +0000 (11:19 +0200)
And thus avoid the false return value.

ext/standard/basic_functions.stub.php
ext/standard/basic_functions_arginfo.h
ext/standard/metaphone.c
ext/standard/tests/strings/metaphone.phpt

index ecc97686d3b3eeacb0b67f524ddfc68e74f27ffb..74a049831c6d68515345cdab4b28c941060f5f9c 100755 (executable)
@@ -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 {}
index 0f96cb302c4f4154fbfcedcb0114bd9d02ceafbc..5c63fb509a6e88462bf74cb0ec375a9a2ad96e18 100644 (file)
@@ -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)
index 1665caddceb481cffcdb345bda544897cc76c999..573bf9a4b0d0f9cae88b5a35972719e8d04ca81b 100644 (file)
@@ -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 */
 /* }}} */
index a6b52e3934a1f741af277ee363d5e29fa5c02517..0356c1fd699092267e2d51531fc2e8a3da4bfa08 100644 (file)
@@ -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"