Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ PHP NEWS
. Fixed cloning IntlDateFormatter and MessageFormatter losing PHP-side state
such as dateType, timeType, calendar and the message pattern.
(Ilia Alshanetsky)
. Fixed Collator attribute and strength methods not rejecting an
unconstructed Collator. (Ilia Alshanetsky)

- Lexbor:
. Merge patches lexbor/lexbor@8a14bc0 and lexbor/lexbor@f67ce4b, fixing a
Expand Down
16 changes: 16 additions & 0 deletions ext/intl/collator/collator_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ PHP_FUNCTION( collator_get_attribute )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

value = ucol_getAttribute( co->ucoll, attribute, COLLATOR_ERROR_CODE_P( co ) );
COLLATOR_CHECK_STATUS( co, "Error getting attribute value" );

Expand All @@ -64,6 +68,10 @@ PHP_FUNCTION( collator_set_attribute )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

/* Set new value for the given attribute. */
ucol_setAttribute( co->ucoll, attribute, value, COLLATOR_ERROR_CODE_P( co ) );
COLLATOR_CHECK_STATUS( co, "Error setting attribute value" );
Expand All @@ -87,6 +95,10 @@ PHP_FUNCTION( collator_get_strength )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

/* Get current strength and return it. */
RETURN_LONG( ucol_getStrength( co->ucoll ) );
}
Expand All @@ -109,6 +121,10 @@ PHP_FUNCTION( collator_set_strength )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

/* Set given strength. */
ucol_setStrength( co->ucoll, strength );

Expand Down
16 changes: 16 additions & 0 deletions ext/intl/collator/collator_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ static inline Collator_object *php_intl_collator_fetch_object(zend_object *obj)
}
#define Z_INTL_COLLATOR_P(zv) php_intl_collator_fetch_object(Z_OBJ_P(zv))

static zend_always_inline zend_result collator_check_initialized(Collator_object *co)
{
ZEND_ASSERT(co != NULL);

if (UNEXPECTED(co->ucoll == NULL)) {
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
"Object not initialized", 0 );
zend_throw_error(NULL, "Object not initialized");

return FAILURE;
}

return SUCCESS;
}

void collator_register_Collator_symbols(int module_number);
void collator_object_init( Collator_object* co );
void collator_object_destroy( Collator_object* co );
Expand Down
7 changes: 1 addition & 6 deletions ext/intl/collator/collator_compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ PHP_FUNCTION( collator_compare )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (!co || !co->ucoll) {
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
"Object not initialized", 0 );
zend_throw_error(NULL, "Object not initialized");

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

Expand Down
7 changes: 1 addition & 6 deletions ext/intl/collator/collator_locale.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ PHP_FUNCTION( collator_get_locale )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (!co || !co->ucoll) {
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
"Object not initialized", 0 );
zend_throw_error(NULL, "Object not initialized");

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

Expand Down
17 changes: 3 additions & 14 deletions ext/intl/collator/collator_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (!co->ucoll) {
zend_throw_error(NULL, "Object not initialized");
if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

Expand Down Expand Up @@ -376,12 +375,7 @@ PHP_FUNCTION( collator_sort_with_sort_keys )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (!co || !co->ucoll) {
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
"Object not initialized", 0 );
zend_throw_error(NULL, "Object not initialized");

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

Expand Down Expand Up @@ -532,12 +526,7 @@ PHP_FUNCTION( collator_get_sort_key )
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;

if (!co || !co->ucoll) {
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
"Object not initialized", 0 );
zend_throw_error(NULL, "Object not initialized");

if (collator_check_initialized(co) == FAILURE) {
RETURN_THROWS();
}

Expand Down
55 changes: 55 additions & 0 deletions ext/intl/tests/collator_attribute_unconstructed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
--TEST--
Collator attribute and strength methods on unconstructed object
--EXTENSIONS--
intl
--FILE--
<?php

class Collator2 extends Collator {
public function __construct() {
// omitting parent::__construct($someLocale);
}
}

$c = new Collator2();

$methods = [
'getAttribute' => fn() => $c->getAttribute(Collator::NUMERIC_COLLATION),
'setAttribute' => fn() => $c->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON),
'getStrength' => fn() => $c->getStrength(),
'setStrength' => fn() => $c->setStrength(Collator::SECONDARY),
];

foreach ($methods as $method => $call) {
try {
$call();
} catch (Error $e) {
echo $method, ': ', $e::class, ': ', $e->getMessage(), PHP_EOL;
}
}

$functions = [
'collator_get_attribute' => fn() => collator_get_attribute($c, Collator::NUMERIC_COLLATION),
'collator_set_attribute' => fn() => collator_set_attribute($c, Collator::NUMERIC_COLLATION, Collator::ON),
'collator_get_strength' => fn() => collator_get_strength($c),
'collator_set_strength' => fn() => collator_set_strength($c, Collator::SECONDARY),
];

foreach ($functions as $function => $call) {
try {
$call();
} catch (Error $e) {
echo $function, ': ', $e::class, ': ', $e->getMessage(), PHP_EOL;
}
}

?>
--EXPECT--
getAttribute: Error: Object not initialized
setAttribute: Error: Object not initialized
getStrength: Error: Object not initialized
setStrength: Error: Object not initialized
collator_get_attribute: Error: Object not initialized
collator_set_attribute: Error: Object not initialized
collator_get_strength: Error: Object not initialized
collator_set_strength: Error: Object not initialized
Loading