summaryrefslogtreecommitdiff
path: root/ext/standard/metaphone.c
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-09-22 11:19:02 +0200
committerNikita Popov <nikita.ppv@gmail.com>2020-09-22 11:19:02 +0200
commit84be22f1f5db499001379f762869d00fc3bc6f55 (patch)
tree1d81241b4256a2b189dac86b1fce51e0c140b7eb /ext/standard/metaphone.c
parentd0fb2f409eb0bf4cc865ade8c4de255bf9f9eaff (diff)
downloadphp-git-84be22f1f5db499001379f762869d00fc3bc6f55.tar.gz
Validate phonemes parameter of metaphone()
And thus avoid the false return value.
Diffstat (limited to 'ext/standard/metaphone.c')
-rw-r--r--ext/standard/metaphone.c42
1 files changed, 14 insertions, 28 deletions
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 */
/* }}} */