@@ -59,12 +59,19 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
5959 zval norm1, norm2;
6060 zval *num1_p = nullptr , *num2_p = nullptr ;
6161 zval *norm1_p = nullptr , *norm2_p = nullptr ;
62- zval *str1_p, *str2_p;
62+ zval *str1_p = nullptr , *str2_p = nullptr ;
6363
6464 ZVAL_NULL (&str1);
6565 str1_p = collator_convert_object_to_string ( op1, &str1 );
66+ if ( str1_p == nullptr )
67+ return FAILURE ;
68+
6669 ZVAL_NULL (&str2);
6770 str2_p = collator_convert_object_to_string ( op2, &str2 );
71+ if ( str2_p == nullptr ) {
72+ rc = FAILURE ;
73+ goto cleanup;
74+ }
6875
6976 /* If both args are strings AND either of args is not numeric string
7077 * then use ICU-compare. Otherwise PHP-compare. */
@@ -90,9 +97,17 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
9097 * just convert it to utf8.
9198 */
9299 norm1_p = collator_convert_zstr_utf16_to_utf8 ( str1_p, &norm1 );
100+ if ( norm1_p == nullptr ) {
101+ rc = FAILURE ;
102+ goto cleanup;
103+ }
93104
94105 /* num2 is not set but str2 is string => do normalization. */
95106 norm2_p = collator_normalize_sort_argument ( str2_p, &norm2 );
107+ if ( norm2_p == nullptr ) {
108+ rc = FAILURE ;
109+ goto cleanup;
110+ }
96111 }
97112 else
98113 {
@@ -109,25 +124,40 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
109124 {
110125 /* num1 is not set if str1 or str2 is not a string => do normalization. */
111126 norm1_p = collator_normalize_sort_argument ( str1_p, &norm1 );
127+ if ( norm1_p == nullptr ) {
128+ rc = FAILURE ;
129+ goto cleanup;
130+ }
112131
113132 /* if num1 is not set then num2 is not set as well => do normalization. */
114133 norm2_p = collator_normalize_sort_argument ( str2_p, &norm2 );
134+ if ( norm2_p == nullptr ) {
135+ rc = FAILURE ;
136+ goto cleanup;
137+ }
115138 }
116139
117140 rc = compare_function ( result, norm1_p, norm2_p );
141+ }
118142
143+ cleanup:
144+ if ( norm1_p )
119145 zval_ptr_dtor ( norm1_p );
146+
147+ if ( norm2_p )
120148 zval_ptr_dtor ( norm2_p );
121- }
122149
123150 if ( num1_p )
124151 zval_ptr_dtor ( num1_p );
125152
126153 if ( num2_p )
127154 zval_ptr_dtor ( num2_p );
128155
129- zval_ptr_dtor ( str1_p );
130- zval_ptr_dtor ( str2_p );
156+ if ( str1_p )
157+ zval_ptr_dtor ( str1_p );
158+
159+ if ( str2_p )
160+ zval_ptr_dtor ( str2_p );
131161
132162 return rc;
133163}
@@ -172,7 +202,14 @@ static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)
172202{
173203 int rc = SUCCESS ;
174204 zend_string *str1 = collator_zval_to_string (op1);
205+ if ( str1 == nullptr )
206+ return FAILURE ;
207+
175208 zend_string *str2 = collator_zval_to_string (op2);
209+ if ( str2 == nullptr ) {
210+ zend_string_release (str1);
211+ return FAILURE ;
212+ }
176213
177214 /* Compare the strings using ICU. */
178215 ZEND_ASSERT (INTL_G (current_collator) != nullptr );
@@ -260,6 +297,7 @@ static collator_compare_func_t collator_get_compare_function( const zend_long so
260297static void collator_sort_internal ( int renumber, INTERNAL_FUNCTION_PARAMETERS )
261298{
262299 UCollator* saved_collator;
300+ intl_error* saved_collator_error;
263301 zval* array = nullptr ;
264302 HashTable* hash = nullptr ;
265303 zend_array* sorted = nullptr ;
@@ -299,13 +337,21 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
299337
300338 /* Save specified collator in the request-global (?) variable. */
301339 saved_collator = INTL_G ( current_collator );
340+ saved_collator_error = INTL_G ( current_collator_error );
302341 INTL_G ( current_collator ) = co->ucoll ;
342+ INTL_G ( current_collator_error ) = COLLATOR_ERROR_P ( co );
303343
304344 /* Sort specified array. */
305345 zend_hash_sort ( sorted, collator_compare_func, renumber );
306346
307347 /* Restore saved collator. */
308348 INTL_G ( current_collator ) = saved_collator;
349+ INTL_G ( current_collator_error ) = saved_collator_error;
350+
351+ if ( U_FAILURE ( COLLATOR_ERROR_CODE ( co ) ) ) {
352+ zend_array_destroy ( sorted );
353+ }
354+ COLLATOR_CHECK_STATUS ( co, " Error comparing array values" );
309355
310356 /* Convert strings in the specified array back to UTF-8. */
311357 collator_convert_hash_from_utf16_to_utf8 ( sorted, COLLATOR_ERROR_CODE_P ( co ) );
0 commit comments