Skip to content

Commit 10be145

Browse files
committed
feedback
1 parent 0d71ef5 commit 10be145

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

ext/standard/string.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4903,14 +4903,14 @@ static zend_string *try_setlocale_str(zend_long cat, zend_string *loc) {
49034903
return zend_string_init(retval, strlen(retval), 0);
49044904
}
49054905

4906-
static zend_string *try_setlocale_zval(zend_long cat, zval *loc_zv) {
4906+
static zend_string *try_setlocale_zval(zend_long cat, zval *loc_zv, uint32_t arg_num) {
49074907
zend_string *tmp_loc_str;
49084908
zend_string *loc_str = zval_try_get_tmp_string(loc_zv, &tmp_loc_str);
49094909
if (UNEXPECTED(loc_str == NULL)) {
49104910
return NULL;
49114911
}
49124912
if (zend_str_has_nul_byte(loc_str)) {
4913-
zend_argument_value_error(2, "must not contain any null bytes");
4913+
zend_argument_value_error(arg_num, "must not contain any null bytes");
49144914
zend_tmp_string_release(tmp_loc_str);
49154915
return NULL;
49164916
}
@@ -4935,10 +4935,20 @@ PHP_FUNCTION(setlocale)
49354935
zend_string **strings = do_alloca(sizeof(zend_string *) * num_args, use_heap);
49364936

49374937
for (uint32_t i = 0; i < num_args; i++) {
4938-
if (UNEXPECTED(Z_TYPE(args[i]) != IS_ARRAY && !zend_parse_arg_path_str(&args[i], &strings[i], true, i + 2))) {
4938+
if (Z_TYPE(args[i]) == IS_ARRAY) {
4939+
if (UNEXPECTED(i != 0)) {
4940+
zend_wrong_parameter_type_error(i + 2, Z_EXPECTED_STRING_OR_NULL, &args[i]);
4941+
goto out;
4942+
}
4943+
num_args = 1;
4944+
break;
4945+
}
4946+
if (UNEXPECTED(!zend_parse_arg_path_str(&args[i], &strings[i], true, i + 2))) {
49394947
zend_wrong_parameter_type_error(
49404948
i + 2,
4941-
Z_TYPE(args[i]) == IS_STRING ? Z_EXPECTED_PATH : Z_EXPECTED_ARRAY_OR_STRING_OR_NULL,
4949+
Z_TYPE(args[i]) == IS_STRING
4950+
? Z_EXPECTED_PATH
4951+
: (i == 0 ? Z_EXPECTED_ARRAY_OR_STRING_OR_NULL : Z_EXPECTED_STRING_OR_NULL),
49424952
&args[i]);
49434953
goto out;
49444954
}
@@ -4949,7 +4959,7 @@ PHP_FUNCTION(setlocale)
49494959
if (Z_TYPE(args[i]) == IS_ARRAY) {
49504960
zval *elem;
49514961
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(args[i]), elem) {
4952-
result = try_setlocale_zval(cat, elem);
4962+
result = try_setlocale_zval(cat, elem, i + 2);
49534963
if (EG(exception)) {
49544964
goto out;
49554965
}

ext/standard/tests/strings/setlocale_null_byte.phpt

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,46 @@ class NullByteStringable {
1111
try {
1212
var_dump(setlocale(LC_ALL, "C\0locale"));
1313
} catch (ValueError $e) {
14-
echo $e->getMessage(), "\n";
14+
echo $e::class, ": ", $e->getMessage(), \PHP_EOL;
1515
}
1616

1717
try {
1818
var_dump(setlocale(LC_ALL, ["locale\0name", "C"]));
1919
} catch (ValueError $e) {
20-
echo $e->getMessage(), "\n";
20+
echo $e::class, ": ", $e->getMessage(), \PHP_EOL;
2121
}
2222

2323
try {
2424
var_dump(@setlocale(LC_ALL, [str_repeat("x", 255), "C\0locale"]));
2525
} catch (ValueError $e) {
26-
echo $e->getMessage(), "\n";
26+
echo $e::class, ": ", $e->getMessage(), \PHP_EOL;
2727
}
2828

2929
try {
30-
var_dump(setlocale(LC_ALL, [], new NullByteStringable()));
30+
var_dump(setlocale(LC_ALL, ["invalid", "C\0locale"]));
3131
} catch (ValueError $e) {
32-
echo $e->getMessage(), "\n";
32+
echo $e::class, ": ", $e->getMessage(), \PHP_EOL;
3333
}
34+
35+
try {
36+
var_dump(setlocale(LC_ALL, "invalid", ["C\0locale"]));
37+
} catch (TypeError $e) {
38+
echo $e::class, ": ", $e->getMessage(), \PHP_EOL;
39+
}
40+
41+
try {
42+
var_dump(setlocale(LC_ALL, "invalid", new NullByteStringable()));
43+
} catch (ValueError $e) {
44+
echo $e::class, ": ", $e->getMessage(), \PHP_EOL;
45+
}
46+
47+
var_dump(setlocale(LC_ALL, [], new NullByteStringable()));
3448
?>
3549
--EXPECT--
36-
setlocale(): Argument #2 ($locales) must not contain any null bytes
37-
setlocale(): Argument #2 ($locales) must not contain any null bytes
38-
setlocale(): Argument #2 ($locales) must not contain any null bytes
39-
setlocale(): Argument #3 must not contain any null bytes
50+
ValueError: setlocale(): Argument #2 ($locales) must not contain any null bytes
51+
ValueError: setlocale(): Argument #2 ($locales) must not contain any null bytes
52+
ValueError: setlocale(): Argument #2 ($locales) must not contain any null bytes
53+
ValueError: setlocale(): Argument #2 ($locales) must not contain any null bytes
54+
TypeError: setlocale(): Argument #3 must be of type ?string, array given
55+
ValueError: setlocale(): Argument #3 must not contain any null bytes
56+
bool(false)

0 commit comments

Comments
 (0)