diff --git a/ext/standard/array.c b/ext/standard/array.c index f8dd7d891dd3..570e9e30e0da 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -4863,6 +4863,18 @@ PHP_FUNCTION(array_unique) return; } + zend_long base_sort = sort_type & ~PHP_SORT_FLAG_CASE; + + if (base_sort != PHP_SORT_REGULAR + && base_sort != PHP_SORT_NUMERIC + && base_sort != PHP_SORT_STRING + && base_sort != PHP_SORT_LOCALE_STRING) { + + zend_argument_value_error( + 2, "must be one of SORT_REGULAR, SORT_NUMERIC, SORT_STRING, or SORT_LOCALE_STRING"); + RETURN_THROWS(); + } + if (sort_type == PHP_SORT_STRING) { HashTable seen; zend_long num_key; diff --git a/ext/standard/tests/array/array_unique_invalid_sort_type.phpt b/ext/standard/tests/array/array_unique_invalid_sort_type.phpt new file mode 100644 index 000000000000..e431b66700ef --- /dev/null +++ b/ext/standard/tests/array/array_unique_invalid_sort_type.phpt @@ -0,0 +1,20 @@ +--TEST-- +array_unique() throws ValueError on invalid sort_type +--FILE-- +getMessage(), PHP_EOL; +} + +try { + array_unique([1, 2, 3], -1); +} catch (ValueError $e) { + echo $e->getMessage(), PHP_EOL; +} +?> +--EXPECT-- +array_unique(): Argument #2 ($flags) must be one of SORT_REGULAR, SORT_NUMERIC, SORT_STRING, or SORT_LOCALE_STRING +array_unique(): Argument #2 ($flags) must be one of SORT_REGULAR, SORT_NUMERIC, SORT_STRING, or SORT_LOCALE_STRING +