Skip to content

setlocale's 2nd and 3rd argument ignores strict_types #18823

Closed

Description

@divinity76

Description

The following code:

<?php
declare(strict_types=1);
try{
setlocale(LC_ALL, 0,"0"); // 2nd argument is a strict_types violation
}finally{
setlocale(LC_ALL, "0",0); // 3rd argument is a strict_types violation
}

Resulted in this output:

But I expected this output instead:

Uncaught TypeError: setlocale(): Argument #2 ($locales) must be of type string, int given in /in/dqfQT:4
Uncaught TypeError: setlocale(): Argument #3 ($rest) must be of type string, int given in /in/dqfQT:6

PHP Version

PHP8.4.8

Operating System

WSL Ubuntu 24.04

Activity

  1. divinity76 commented on Jun 10, 2025

    @divinity76
    ContributorAuthor

    fwiw it seems the varargs api is currently incapable of checking types at compile time, until the varargs api is upgraded,
    i think if(ZEND_ARG_USES_STRICT_TYPES()){TypeError...} is the solution for the vararg strings 🤔

    • i am not working on this, i was just looking for a string ...$args example in php-src for another thing. setlocale was the only such function i found, and turns out it doesn't respect strict_types, which is exactly what i was looking for: how to respect strict_types in string ...$args in C. seems the answer is ZEND_ARG_USES_STRICT_TYPES()
  2. divinity76 commented on Jun 10, 2025

    @divinity76
    ContributorAuthor

    probably related to #18826

  3. ndossche commented on Jun 10, 2025

    @ndossche
    Member

    The second and third argument are handled in the same way (i.e. via variadics), which explains why they are both inconsistent.
    An implicit conversion to string happens here:

    zend_string *loc_str = zval_try_get_tmp_string(loc_zv, &tmp_loc_str);

    To fix this, a manual type check must be added in the for loop here (I think, on first sight):
    for (uint32_t i = 0; i < num_args; i++) {

  4. self-assigned this
    on Jun 10, 2025
  5. added a commit that references this issue on Jun 10, 2025
    017bcac
  6. added a commit that references this issue on Jun 13, 2025
    43c18f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions