-
Notifications
You must be signed in to change notification settings - Fork 8.1k
ext/intl: Add collator sort conversion error handling #22893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LamentXU123
wants to merge
7
commits into
php:master
Choose a base branch
from
LamentXU123:dep
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f511a2f
ext/intl: Fix Collator sort conversion error handling
LamentXU123 4a4244f
add tests
LamentXU123 5f842ce
feedback
LamentXU123 a5dd051
feedback
LamentXU123 524c5f3
better logic
LamentXU123 94939d1
feedback
LamentXU123 317f61d
fix CI
LamentXU123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,17 @@ static const size_t DEF_SORT_KEYS_INDX_BUF_INCREMENT = 1048576; | |
|
|
||
| static const size_t DEF_UTF16_BUF_SIZE = 1024; | ||
|
|
||
| static void collator_report_sort_error(Collator_object *co, const intl_error *sort_error) | ||
| { | ||
| if (sort_error->custom_error_message) { | ||
| intl_errors_set( | ||
| COLLATOR_ERROR_P(co), sort_error->code, | ||
| ZSTR_VAL(sort_error->custom_error_message)); | ||
| } else { | ||
| intl_errors_set_code(COLLATOR_ERROR_P(co), sort_error->code); | ||
| } | ||
| } | ||
|
|
||
| /* {{{ collator_regular_compare_function */ | ||
| static int collator_regular_compare_function(zval *result, zval *op1, zval *op2) | ||
| { | ||
|
|
@@ -59,12 +70,20 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2) | |
| zval norm1, norm2; | ||
| zval *num1_p = nullptr, *num2_p = nullptr; | ||
| zval *norm1_p = nullptr, *norm2_p = nullptr; | ||
| zval *str1_p, *str2_p; | ||
| zval *str1_p = nullptr, *str2_p = nullptr; | ||
|
|
||
| ZVAL_NULL(&str1); | ||
| str1_p = collator_convert_object_to_string( op1, &str1 ); | ||
| if( str1_p == nullptr ) { | ||
| return FAILURE; | ||
| } | ||
|
|
||
| ZVAL_NULL(&str2); | ||
| str2_p = collator_convert_object_to_string( op2, &str2 ); | ||
| if( str2_p == nullptr ) { | ||
| rc = FAILURE; | ||
| goto cleanup; | ||
| } | ||
|
|
||
| /* If both args are strings AND either of args is not numeric string | ||
| * then use ICU-compare. Otherwise PHP-compare. */ | ||
|
|
@@ -90,9 +109,17 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2) | |
| * just convert it to utf8. | ||
| */ | ||
| norm1_p = collator_convert_zstr_utf16_to_utf8( str1_p, &norm1 ); | ||
| if( norm1_p == nullptr ) { | ||
| rc = FAILURE; | ||
| goto cleanup; | ||
| } | ||
|
|
||
| /* num2 is not set but str2 is string => do normalization. */ | ||
| norm2_p = collator_normalize_sort_argument( str2_p, &norm2 ); | ||
| if( norm2_p == nullptr ) { | ||
| rc = FAILURE; | ||
| goto cleanup; | ||
| } | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -109,25 +136,40 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2) | |
| { | ||
| /* num1 is not set if str1 or str2 is not a string => do normalization. */ | ||
| norm1_p = collator_normalize_sort_argument( str1_p, &norm1 ); | ||
| if( norm1_p == nullptr ) { | ||
| rc = FAILURE; | ||
| goto cleanup; | ||
| } | ||
|
|
||
| /* if num1 is not set then num2 is not set as well => do normalization. */ | ||
| norm2_p = collator_normalize_sort_argument( str2_p, &norm2 ); | ||
| if( norm2_p == nullptr ) { | ||
| rc = FAILURE; | ||
| goto cleanup; | ||
| } | ||
| } | ||
|
|
||
| rc = compare_function( result, norm1_p, norm2_p ); | ||
| } | ||
|
|
||
| cleanup: | ||
| if( norm1_p ) | ||
| zval_ptr_dtor( norm1_p ); | ||
|
|
||
| if( norm2_p ) | ||
| zval_ptr_dtor( norm2_p ); | ||
| } | ||
|
|
||
| if( num1_p ) | ||
| zval_ptr_dtor( num1_p ); | ||
|
|
||
| if( num2_p ) | ||
| zval_ptr_dtor( num2_p ); | ||
|
|
||
| zval_ptr_dtor( str1_p ); | ||
| zval_ptr_dtor( str2_p ); | ||
| if( str1_p ) | ||
| zval_ptr_dtor( str1_p ); | ||
|
|
||
| if( str2_p ) | ||
| zval_ptr_dtor( str2_p ); | ||
|
|
||
| return rc; | ||
| } | ||
|
|
@@ -172,7 +214,15 @@ static int collator_icu_compare_function(zval *result, zval *op1, zval *op2) | |
| { | ||
| int rc = SUCCESS; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: no goto whatsoever so it seems useless |
||
| zend_string *str1 = collator_zval_to_string(op1); | ||
| if( str1 == nullptr ) { | ||
| return FAILURE; | ||
| } | ||
|
|
||
| zend_string *str2 = collator_zval_to_string(op2); | ||
| if( str2 == nullptr ) { | ||
| zend_string_release(str1); | ||
| return FAILURE; | ||
| } | ||
|
|
||
| /* Compare the strings using ICU. */ | ||
| ZEND_ASSERT(INTL_G(current_collator) != nullptr); | ||
|
|
@@ -197,6 +247,11 @@ static int collator_compare_func(Bucket *f, Bucket *s) | |
| zval *first = &f->val; | ||
| zval *second = &s->val; | ||
|
|
||
| ZEND_ASSERT(INTL_G(current_collator_error) != nullptr); | ||
| if( EG(exception) || U_FAILURE( INTL_ERROR_CODE(*INTL_G(current_collator_error)) ) ) { | ||
| return 0; | ||
| } | ||
|
|
||
| if( INTL_G(compare_func)( &result, first, second) == FAILURE ) | ||
| return 0; | ||
|
|
||
|
|
@@ -260,6 +315,9 @@ static collator_compare_func_t collator_get_compare_function( const zend_long so | |
| static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS ) | ||
| { | ||
| UCollator* saved_collator; | ||
| intl_error* saved_collator_error; | ||
| intl_error sort_error; | ||
| collator_compare_func_t saved_compare_func; | ||
| zval* array = nullptr; | ||
| HashTable* hash = nullptr; | ||
| zend_array* sorted = nullptr; | ||
|
|
@@ -282,9 +340,6 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS ) | |
| RETURN_THROWS(); | ||
| } | ||
|
|
||
| /* Set 'compare function' according to sort flags. */ | ||
| INTL_G(compare_func) = collator_get_compare_function( sort_flags ); | ||
|
|
||
| hash = Z_ARRVAL_P( array ); | ||
|
|
||
| /* Copy array, so the in-place modifications will not be visible to the callback function */ | ||
|
|
@@ -297,15 +352,38 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS ) | |
| } | ||
| COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-8 to UTF-16" ); | ||
|
|
||
| intl_error_init( &sort_error ); | ||
|
|
||
| /* Save specified collator in the request-global (?) variable. */ | ||
| saved_collator = INTL_G( current_collator ); | ||
| saved_collator_error = INTL_G( current_collator_error ); | ||
| saved_compare_func = INTL_G( compare_func ); | ||
| INTL_G( current_collator ) = co->ucoll; | ||
| INTL_G( current_collator_error ) = &sort_error; | ||
| INTL_G( compare_func ) = collator_get_compare_function( sort_flags ); | ||
|
|
||
| /* Sort specified array. */ | ||
| zend_hash_sort( sorted, collator_compare_func, renumber ); | ||
|
|
||
| /* Restore saved collator. */ | ||
| INTL_G( current_collator ) = saved_collator; | ||
| INTL_G( current_collator_error ) = saved_collator_error; | ||
| INTL_G( compare_func ) = saved_compare_func; | ||
|
|
||
| if( EG(exception) ) { | ||
| zend_array_destroy( sorted ); | ||
| intl_error_reset( &sort_error ); | ||
| RETURN_THROWS(); | ||
| } | ||
|
|
||
| if( U_FAILURE( INTL_ERROR_CODE(sort_error) ) ) { | ||
| zend_array_destroy( sorted ); | ||
| collator_report_sort_error(co, &sort_error); | ||
| intl_error_reset( &sort_error ); | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| intl_error_reset( &sort_error ); | ||
|
|
||
| /* Convert strings in the specified array back to UTF-8. */ | ||
| collator_convert_hash_from_utf16_to_utf8( sorted, COLLATOR_ERROR_CODE_P( co ) ); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --TEST-- | ||
| Collator::sort() reports conversion errors from comparison callbacks | ||
| --EXTENSIONS-- | ||
| intl | ||
| --FILE-- | ||
| <?php | ||
| class BadString { | ||
| public function __toString(): string { | ||
| return "\xFF"; | ||
| } | ||
| } | ||
|
|
||
| $coll = new Collator('en_US'); | ||
| $array = ['b', new BadString(), 'a']; | ||
|
|
||
| var_dump($coll->sort($array, Collator::SORT_STRING)); | ||
| var_dump(intl_get_error_code() === U_INVALID_CHAR_FOUND); | ||
| echo intl_get_error_message(), "\n"; | ||
| var_dump($coll->getErrorCode() === U_INVALID_CHAR_FOUND); | ||
| echo $coll->getErrorMessage(), "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| bool(false) | ||
| bool(true) | ||
| Collator::sort(): Error converting string from UTF-8 to UTF-16: U_INVALID_CHAR_FOUND | ||
| bool(true) | ||
| Collator::sort(): Error converting string from UTF-8 to UTF-16: U_INVALID_CHAR_FOUND |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now,
rvneeds to be taken care of.