Skip to content

Fix GH-18823: setlocale's 2nd and 3rd argument ignores strict_types #18828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -4926,13 +4926,22 @@ PHP_FUNCTION(setlocale)
{
zend_long cat;
zval *args = NULL;
int num_args;
uint32_t num_args;

ZEND_PARSE_PARAMETERS_START(2, -1)
Z_PARAM_LONG(cat)
Z_PARAM_VARIADIC('+', args, num_args)
ZEND_PARSE_PARAMETERS_END();

if (ZEND_ARG_USES_STRICT_TYPES()) {
for (uint32_t i = 0; i < num_args; i++) {
if (UNEXPECTED(Z_TYPE(args[i]) != IS_ARRAY && Z_TYPE(args[i]) != IS_STRING)) {
zend_wrong_parameter_type_error(i + 2, Z_EXPECTED_ARRAY_OR_STRING, &args[i]);
RETURN_THROWS();
}
}
}
Copy link
Member

@Girgias Girgias Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't cover the case where a non-stringable object is passed as argument, nor a resource, or null. (in weak mode)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I first was confused by your comment, but you mean the coercions in weak mode are not done a priori and thus there is also no proper error handling of that.
I suppose we can call zend_parse_arg_str ourselves and do the coercions upfront instead of in try_setlocale_zval.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That and also types that should be rejected by our normal semantics were not :)


for (uint32_t i = 0; i < num_args; i++) {
if (Z_TYPE(args[i]) == IS_ARRAY) {
zval *elem;
Expand Down
19 changes: 19 additions & 0 deletions ext/standard/tests/strings/gh18823.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
GH-18823 (setlocale's 2nd and 3rd argument ignores strict_types)
--FILE--
<?php
declare(strict_types=1);
try {
setlocale(LC_ALL, 0, "0");
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
try {
setlocale(LC_ALL, "0", 0);
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
setlocale(): Argument #2 ($locales) must be of type array|string, int given
setlocale(): Argument #3 must be of type array|string, int given
Loading