Promote mb_str_split warning to ValueError

Also add a TODO about documenting this funcion on PHP.net
Convert some checks to assertions as if they don't hold something went wrong during memory allocation
Due to these changes this function cannot return false anymore, fix stubs accordingly
This commit is contained in:
George Peter Banyard 2020-04-02 21:57:38 +02:00
parent f488b5d0f9
commit d44ee9112f
4 changed files with 42 additions and 13 deletions

View file

@ -1862,6 +1862,7 @@ static int mbfl_split_output(int c, void *data)
return 0; return 0;
} }
/* TODO Document this function on php.net */
PHP_FUNCTION(mb_str_split) PHP_FUNCTION(mb_str_split)
{ {
zend_string *str, *encoding = NULL; zend_string *str, *encoding = NULL;
@ -1879,8 +1880,8 @@ PHP_FUNCTION(mb_str_split)
ZEND_PARSE_PARAMETERS_END(); ZEND_PARSE_PARAMETERS_END();
if (split_length <= 0) { if (split_length <= 0) {
php_error_docref(NULL, E_WARNING, "The length of each segment must be greater than zero"); zend_argument_value_error(2, "must be greater than 0");
RETURN_FALSE; RETURN_THROWS();
} }
/* fill mbfl_string structure */ /* fill mbfl_string structure */
@ -1945,10 +1946,8 @@ PHP_FUNCTION(mb_str_split)
mbfl_memory_device_output, mbfl_memory_device_output,
NULL, NULL,
&device); &device);
/* if something wrong with the decoded */ /* assert that nothing is wrong with the decoder */
if (decoder == NULL) { ZEND_ASSERT(decoder != NULL);
RETURN_FALSE;
}
/* wchar filter */ /* wchar filter */
mbfl_string_init(&result_string); /* mbfl_string to store chunk in the callback */ mbfl_string_init(&result_string); /* mbfl_string to store chunk in the callback */
@ -1966,11 +1965,8 @@ PHP_FUNCTION(mb_str_split)
mbfl_split_output, mbfl_split_output,
NULL, NULL,
&params); &params);
/* if something wrong with the filter */ /* assert that nothing is wrong with the filter */
if (filter == NULL){ ZEND_ASSERT(filter != NULL);
mbfl_convert_filter_delete(decoder); /* this will free allocated memory for the decoded */
RETURN_FALSE;
}
while (p < last - 1) { /* cycle each byte except last with callback function */ while (p < last - 1) { /* cycle each byte except last with callback function */
(*filter->filter_function)(*p++, filter); (*filter->filter_function)(*p++, filter);

View file

@ -19,7 +19,7 @@ function mb_parse_str(string $encoded_string, &$result): bool {}
function mb_output_handler(string $contents, int $status): string {} function mb_output_handler(string $contents, int $status): string {}
function mb_str_split(string $str, int $split_length = 1, string $encoding = UNKNOWN): array|false {} function mb_str_split(string $str, int $split_length = 1, string $encoding = UNKNOWN): array {}
function mb_strlen(string $str, string $encoding = UNKNOWN): int|false {} function mb_strlen(string $str, string $encoding = UNKNOWN): int|false {}

View file

@ -36,7 +36,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_output_handler, 0, 2, IS_STRI
ZEND_ARG_TYPE_INFO(0, status, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, status, IS_LONG, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_str_split, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_str_split, 0, 1, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, split_length, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, split_length, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)

View file

@ -0,0 +1,33 @@
--TEST--
mb_str_split() error conditions
--SKIPIF--
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
--FILE--
<?php
$string = "日本"; /* 2 chars */
// Invalid split length
try {
mb_str_split($string, 0);
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
try {
mb_str_split($string, -5);
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
//Invalid Encoding
try {
mb_str_split($string, 1, "BAD_ENCODING");
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECT--
mb_str_split(): Argument #2 ($split_length) must be greater than 0
mb_str_split(): Argument #2 ($split_length) must be greater than 0
mb_str_split(): Argument #3 ($encoding) must be a valid encoding, "BAD_ENCODING" given