Skip to content

Commit 862ee53

Browse files
committed
address review comments
1 parent 2370a04 commit 862ee53

3 files changed

Lines changed: 51 additions & 13 deletions

File tree

ext/standard/string.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4927,18 +4927,19 @@ PHP_FUNCTION(setlocale)
49274927
zend_long cat;
49284928
zval *args = NULL;
49294929
uint32_t num_args;
4930+
ALLOCA_FLAG(use_heap);
49304931

49314932
ZEND_PARSE_PARAMETERS_START(2, -1)
49324933
Z_PARAM_LONG(cat)
49334934
Z_PARAM_VARIADIC('+', args, num_args)
49344935
ZEND_PARSE_PARAMETERS_END();
49354936

4936-
if (ZEND_ARG_USES_STRICT_TYPES()) {
4937-
for (uint32_t i = 0; i < num_args; i++) {
4938-
if (UNEXPECTED(Z_TYPE(args[i]) != IS_ARRAY && Z_TYPE(args[i]) != IS_STRING)) {
4939-
zend_wrong_parameter_type_error(i + 2, Z_EXPECTED_ARRAY_OR_STRING, &args[i]);
4940-
RETURN_THROWS();
4941-
}
4937+
zend_string **strings = do_alloca(sizeof(zend_string *) * num_args, use_heap);
4938+
4939+
for (uint32_t i = 0; i < num_args; i++) {
4940+
if (UNEXPECTED(Z_TYPE(args[i]) != IS_ARRAY && !zend_parse_arg_str(&args[i], &strings[i], false, i + 2))) {
4941+
zend_wrong_parameter_type_error(i + 2, Z_EXPECTED_ARRAY_OR_STRING, &args[i]);
4942+
goto out;
49424943
}
49434944
}
49444945

@@ -4948,24 +4949,29 @@ PHP_FUNCTION(setlocale)
49484949
ZEND_HASH_FOREACH_VAL(Z_ARRVAL(args[i]), elem) {
49494950
zend_string *result = try_setlocale_zval(cat, elem);
49504951
if (EG(exception)) {
4951-
RETURN_THROWS();
4952+
goto out;
49524953
}
49534954
if (result) {
4954-
RETURN_STR(result);
4955+
RETVAL_STR(result);
4956+
goto out;
49554957
}
49564958
} ZEND_HASH_FOREACH_END();
49574959
} else {
4958-
zend_string *result = try_setlocale_zval(cat, &args[i]);
4960+
zend_string *result = try_setlocale_str(cat, strings[i]);
49594961
if (EG(exception)) {
4960-
RETURN_THROWS();
4962+
goto out;
49614963
}
49624964
if (result) {
4963-
RETURN_STR(result);
4965+
RETVAL_STR(result);
4966+
goto out;
49644967
}
49654968
}
49664969
}
49674970

4968-
RETURN_FALSE;
4971+
RETVAL_FALSE;
4972+
4973+
out:
4974+
free_alloca(strings, use_heap);
49694975
}
49704976
/* }}} */
49714977

ext/standard/tests/strings/gh18823.phpt renamed to ext/standard/tests/strings/gh18823_strict.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
GH-18823 (setlocale's 2nd and 3rd argument ignores strict_types)
2+
GH-18823 (setlocale's 2nd and 3rd argument ignores strict_types) - strict mode
33
--FILE--
44
<?php
55
declare(strict_types=1);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
GH-18823 (setlocale's 2nd and 3rd argument ignores strict_types) - weak mode
3+
--INI--
4+
error_reporting=E_ALL
5+
--FILE--
6+
<?php
7+
class MyStringable {
8+
public function __toString(): string {
9+
return 'foo';
10+
}
11+
}
12+
13+
class MyStringableThrow {
14+
public function __toString(): string {
15+
throw new Error('no');
16+
}
17+
}
18+
19+
setlocale(LC_ALL, 0, "0");
20+
setlocale(LC_ALL, "0", 0);
21+
setlocale(LC_ALL, null);
22+
setlocale(LC_ALL, new MyStringable);
23+
24+
try {
25+
setlocale(LC_ALL, new MyStringableThrow);
26+
} catch (Error $e) {
27+
echo $e->getMessage(), "\n";
28+
}
29+
?>
30+
--EXPECTF--
31+
Deprecated: setlocale(): Passing null to parameter #2 ($locales) of type string is deprecated in %s on line %d
32+
no

0 commit comments

Comments
 (0)