Skip to content

Commit d44ee91

Browse files
committed
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
1 parent f488b5d commit d44ee91

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

ext/mbstring/mbstring.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,7 @@ static int mbfl_split_output(int c, void *data)
18621862
return 0;
18631863
}
18641864

1865+
/* TODO Document this function on php.net */
18651866
PHP_FUNCTION(mb_str_split)
18661867
{
18671868
zend_string *str, *encoding = NULL;
@@ -1879,8 +1880,8 @@ PHP_FUNCTION(mb_str_split)
18791880
ZEND_PARSE_PARAMETERS_END();
18801881

18811882
if (split_length <= 0) {
1882-
php_error_docref(NULL, E_WARNING, "The length of each segment must be greater than zero");
1883-
RETURN_FALSE;
1883+
zend_argument_value_error(2, "must be greater than 0");
1884+
RETURN_THROWS();
18841885
}
18851886

18861887
/* fill mbfl_string structure */
@@ -1945,10 +1946,8 @@ PHP_FUNCTION(mb_str_split)
19451946
mbfl_memory_device_output,
19461947
NULL,
19471948
&device);
1948-
/* if something wrong with the decoded */
1949-
if (decoder == NULL) {
1950-
RETURN_FALSE;
1951-
}
1949+
/* assert that nothing is wrong with the decoder */
1950+
ZEND_ASSERT(decoder != NULL);
19521951

19531952
/* wchar filter */
19541953
mbfl_string_init(&result_string); /* mbfl_string to store chunk in the callback */
@@ -1966,11 +1965,8 @@ PHP_FUNCTION(mb_str_split)
19661965
mbfl_split_output,
19671966
NULL,
19681967
&params);
1969-
/* if something wrong with the filter */
1970-
if (filter == NULL){
1971-
mbfl_convert_filter_delete(decoder); /* this will free allocated memory for the decoded */
1972-
RETURN_FALSE;
1973-
}
1968+
/* assert that nothing is wrong with the filter */
1969+
ZEND_ASSERT(filter != NULL);
19741970

19751971
while (p < last - 1) { /* cycle each byte except last with callback function */
19761972
(*filter->filter_function)(*p++, filter);

ext/mbstring/mbstring.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function mb_parse_str(string $encoded_string, &$result): bool {}
1919

2020
function mb_output_handler(string $contents, int $status): string {}
2121

22-
function mb_str_split(string $str, int $split_length = 1, string $encoding = UNKNOWN): array|false {}
22+
function mb_str_split(string $str, int $split_length = 1, string $encoding = UNKNOWN): array {}
2323

2424
function mb_strlen(string $str, string $encoding = UNKNOWN): int|false {}
2525

ext/mbstring/mbstring_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_output_handler, 0, 2, IS_STRI
3636
ZEND_ARG_TYPE_INFO(0, status, IS_LONG, 0)
3737
ZEND_END_ARG_INFO()
3838

39-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mb_str_split, 0, 1, MAY_BE_ARRAY|MAY_BE_FALSE)
39+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mb_str_split, 0, 1, IS_ARRAY, 0)
4040
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
4141
ZEND_ARG_TYPE_INFO(0, split_length, IS_LONG, 0)
4242
ZEND_ARG_TYPE_INFO(0, encoding, IS_STRING, 0)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
mb_str_split() error conditions
3+
--SKIPIF--
4+
<?php extension_loaded('mbstring') or die('skip mbstring not available'); ?>
5+
--FILE--
6+
<?php
7+
8+
$string = "日本"; /* 2 chars */
9+
10+
// Invalid split length
11+
try {
12+
mb_str_split($string, 0);
13+
} catch (\ValueError $e) {
14+
echo $e->getMessage() . \PHP_EOL;
15+
}
16+
try {
17+
mb_str_split($string, -5);
18+
} catch (\ValueError $e) {
19+
echo $e->getMessage() . \PHP_EOL;
20+
}
21+
22+
//Invalid Encoding
23+
try {
24+
mb_str_split($string, 1, "BAD_ENCODING");
25+
} catch (\ValueError $e) {
26+
echo $e->getMessage() . \PHP_EOL;
27+
}
28+
29+
?>
30+
--EXPECT--
31+
mb_str_split(): Argument #2 ($split_length) must be greater than 0
32+
mb_str_split(): Argument #2 ($split_length) must be greater than 0
33+
mb_str_split(): Argument #3 ($encoding) must be a valid encoding, "BAD_ENCODING" given

0 commit comments

Comments
 (0)