Skip to content

standard: Validate sort_type argument in array_unique()#21110

Open
arshidkv12 wants to merge 11 commits intophp:masterfrom
arshidkv12:array_unique
Open

standard: Validate sort_type argument in array_unique()#21110
arshidkv12 wants to merge 11 commits intophp:masterfrom
arshidkv12:array_unique

Conversation

@arshidkv12
Copy link
Contributor

The array_unique() now validates the sort_type argument and throws ValueError when an unsupported value is provided.

@iluuu1994 iluuu1994 requested a review from Girgias February 2, 2026 15:30
Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

It looks like this is also missing handling for SORT_FLAG_CASE, which looks to be untested. https://3v4l.org/dqSln This is just from a very quick look, there might be other issues.

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

Marking as "Request changes" for visibility.

arshidkv12 and others added 3 commits February 2, 2026 21:10
Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
@arshidkv12
Copy link
Contributor Author

It looks like this is also missing handling for SORT_FLAG_CASE, which looks to be untested. https://3v4l.org/dqSln This is just from a very quick look, there might be other issues.

I don’t see SORT_FLAG_CASE on https://www.php.net/manual/en/function.array-unique.php.

@iluuu1994
Copy link
Member

It's not documented, but it's there, as demonstrated by the 3v4l.

@arshidkv12 arshidkv12 requested a review from iluuu1994 February 2, 2026 16:44
Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

That's all the issues I could find. Please somebody else verify. I'm not code owner.

if (base_sort != PHP_SORT_REGULAR
&& base_sort != PHP_SORT_NUMERIC
&& base_sort != PHP_SORT_STRING
&& base_sort != PHP_SORT_LOCALE_STRING) {
Copy link
Member

Choose a reason for hiding this comment

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

There's also SORT_NATURAL. I don't know whether it would differ in behavior from SORT_STRING, but even if it doesn't it should continue to be accepted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It works with sort_type & ~PHP_SORT_FLAG_CASE only in php_get_data_compare_func_unstable.

Ref:
https://github.com/php/php-src/blob/master/ext/standard/array.c#L495-L556

@Girgias Girgias self-assigned this Feb 3, 2026
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

I wonder if it doesn't make more sense to handle this error in php_get_data_compare_func_unstable() (and possibly the related functions)` to also deal with invalid flags for all the sort functions.

@arshidkv12 arshidkv12 marked this pull request as draft February 4, 2026 18:17
@arshidkv12
Copy link
Contributor Author

@Girgias array_multisort() already checks the sort flags while parsing arguments in ext/standard/array.c, before calling the compare-function selector, so invalid flags are handled at the user-facing level.

Ref:
https://github.com/php/php-src/blob/master/ext/standard/array.c#L6021-L6025

@arshidkv12 arshidkv12 marked this pull request as ready for review February 5, 2026 03:25
@Girgias
Copy link
Member

Girgias commented Feb 5, 2026

@arshidkv12 array_multisort() is a very strange function that doesn't follow "usual" PHP userland semantics, so I wouldn't look at that function for how to implement something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants