Skip to content

Commit 27ad19c

Browse files
committed
Validate collator earlier during sort
Check this once before the sort, instead of on every compare. Also directly store the UCollator to make things more obvious.
1 parent 7eec281 commit 27ad19c

File tree

3 files changed

+15
-28
lines changed

3 files changed

+15
-28
lines changed

ext/intl/collator/collator_sort.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static const size_t DEF_UTF16_BUF_SIZE = 1024;
5050
/* {{{ collator_regular_compare_function */
5151
static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
5252
{
53-
Collator_object* co = NULL;
5453
int rc = SUCCESS;
5554
zval str1, str2;
5655
zval num1, num2;
@@ -70,21 +69,10 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
7069
( str1_p == ( num1_p = collator_convert_string_to_number_if_possible( str1_p, &num1 ) ) ||
7170
str2_p == ( num2_p = collator_convert_string_to_number_if_possible( str2_p, &num2 ) ) ) )
7271
{
73-
/* Fetch collator object. */
74-
co = Z_INTL_COLLATOR_P(&INTL_G(current_collator));
75-
76-
if (!co || !co->ucoll) {
77-
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
78-
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
79-
"Object not initialized", 0 );
80-
zend_throw_error(NULL, "Object not initialized");
81-
rc = FAILURE;
82-
goto cleanup;
83-
}
84-
8572
/* Compare the strings using ICU. */
73+
ZEND_ASSERT(INTL_G(current_collator) != NULL);
8674
ZVAL_LONG(result, ucol_strcoll(
87-
co->ucoll,
75+
INTL_G(current_collator),
8876
INTL_Z_STRVAL_P(str1_p), INTL_Z_STRLEN_P(str1_p),
8977
INTL_Z_STRVAL_P(str2_p), INTL_Z_STRLEN_P(str2_p) ));
9078
}
@@ -129,7 +117,6 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
129117
zval_ptr_dtor( norm2_p );
130118
}
131119

132-
cleanup:
133120
if( num1_p )
134121
zval_ptr_dtor( num1_p );
135122

@@ -182,19 +169,16 @@ static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)
182169
{
183170
zval str1, str2;
184171
int rc = SUCCESS;
185-
Collator_object* co = NULL;
186172
zval *str1_p = NULL;
187173
zval *str2_p = NULL;
188174

189175
str1_p = collator_make_printable_zval( op1, &str1);
190176
str2_p = collator_make_printable_zval( op2, &str2 );
191177

192-
/* Fetch collator object. */
193-
co = Z_INTL_COLLATOR_P(&INTL_G(current_collator));
194-
195178
/* Compare the strings using ICU. */
179+
ZEND_ASSERT(INTL_G(current_collator) != NULL);
196180
ZVAL_LONG(result, ucol_strcoll(
197-
co->ucoll,
181+
INTL_G(current_collator),
198182
INTL_Z_STRVAL_P(str1_p), INTL_Z_STRLEN_P(str1_p),
199183
INTL_Z_STRVAL_P(str2_p), INTL_Z_STRLEN_P(str2_p) ));
200184

@@ -276,7 +260,7 @@ static collator_compare_func_t collator_get_compare_function( const zend_long so
276260
/* {{{ Common code shared by collator_sort() and collator_asort() API functions. */
277261
static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
278262
{
279-
zval saved_collator;
263+
UCollator* saved_collator;
280264
zval* array = NULL;
281265
HashTable* hash = NULL;
282266
zend_long sort_flags = COLLATOR_SORT_REGULAR;
@@ -293,6 +277,11 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
293277
/* Fetch the object. */
294278
COLLATOR_METHOD_FETCH_OBJECT;
295279

280+
if (!co->ucoll) {
281+
zend_throw_error(NULL, "Object not initialized");
282+
RETURN_THROWS();
283+
}
284+
296285
/* Set 'compare function' according to sort flags. */
297286
INTL_G(compare_func) = collator_get_compare_function( sort_flags );
298287

@@ -303,14 +292,14 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
303292
COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-8 to UTF-16" );
304293

305294
/* Save specified collator in the request-global (?) variable. */
306-
ZVAL_COPY_VALUE(&saved_collator, &INTL_G( current_collator ));
307-
ZVAL_OBJ(&INTL_G( current_collator ), Z_OBJ_P(object));
295+
saved_collator = INTL_G( current_collator );
296+
INTL_G( current_collator ) = co->ucoll;
308297

309298
/* Sort specified array. */
310299
zend_hash_sort(hash, collator_compare_func, renumber);
311300

312301
/* Restore saved collator. */
313-
ZVAL_COPY_VALUE(&INTL_G( current_collator ), &saved_collator);
302+
INTL_G( current_collator ) = saved_collator;
314303

315304
/* Convert strings in the specified array back to UTF-8. */
316305
collator_convert_hash_from_utf16_to_utf8( hash, COLLATOR_ERROR_CODE_P( co ) );

ext/intl/php_intl.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,7 @@ PHP_RINIT_FUNCTION( intl )
267267
/* {{{ PHP_RSHUTDOWN_FUNCTION */
268268
PHP_RSHUTDOWN_FUNCTION( intl )
269269
{
270-
if(!Z_ISUNDEF(INTL_G(current_collator))) {
271-
ZVAL_UNDEF(&INTL_G(current_collator));
272-
}
270+
INTL_G(current_collator) = NULL;
273271
if (INTL_G(grapheme_iterator)) {
274272
grapheme_close_global_iterator( );
275273
INTL_G(grapheme_iterator) = NULL;

ext/intl/php_intl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extern zend_module_entry intl_module_entry;
4444
#endif
4545

4646
ZEND_BEGIN_MODULE_GLOBALS(intl)
47-
zval current_collator;
47+
struct UCollator *current_collator;
4848
char* default_locale;
4949
collator_compare_func_t compare_func;
5050
UBreakIterator* grapheme_iterator;

0 commit comments

Comments
 (0)