Skip to content

Commit a99cf8e

Browse files
committed
Merge branch 'PHP-8.6'
* PHP-8.6: ext/intl: Reject unconstructed Collator in attribute and strength methods
2 parents 5a6c223 + fe96b7a commit a99cf8e

7 files changed

Lines changed: 94 additions & 20 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.7.0alpha1
44

5+
- Intl:
6+
. Fixed Collator attribute and strength methods not rejecting an
7+
unconstructed Collator. (Ilia Alshanetsky)
8+
59
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

‎ext/intl/collator/collator_attr.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ U_CFUNC PHP_FUNCTION( collator_get_attribute )
4747
/* Fetch the object. */
4848
COLLATOR_METHOD_FETCH_OBJECT;
4949

50+
if (collator_check_initialized(co) == FAILURE) {
51+
RETURN_THROWS();
52+
}
53+
5054
value = ucol_getAttribute( co->ucoll, static_cast<UColAttribute>(attribute), COLLATOR_ERROR_CODE_P( co ) );
5155
COLLATOR_CHECK_STATUS( co, "Error getting attribute value" );
5256

@@ -71,6 +75,10 @@ U_CFUNC PHP_FUNCTION( collator_set_attribute )
7175
/* Fetch the object. */
7276
COLLATOR_METHOD_FETCH_OBJECT;
7377

78+
if (collator_check_initialized(co) == FAILURE) {
79+
RETURN_THROWS();
80+
}
81+
7482
/* Set new value for the given attribute. */
7583
ucol_setAttribute( co->ucoll, static_cast<UColAttribute>(attribute), static_cast<UColAttributeValue>(value), COLLATOR_ERROR_CODE_P( co ) );
7684
COLLATOR_CHECK_STATUS( co, "Error setting attribute value" );
@@ -94,6 +102,10 @@ U_CFUNC PHP_FUNCTION( collator_get_strength )
94102
/* Fetch the object. */
95103
COLLATOR_METHOD_FETCH_OBJECT;
96104

105+
if (collator_check_initialized(co) == FAILURE) {
106+
RETURN_THROWS();
107+
}
108+
97109
/* Get current strength and return it. */
98110
RETURN_LONG( ucol_getStrength( co->ucoll ) );
99111
}
@@ -116,6 +128,10 @@ U_CFUNC PHP_FUNCTION( collator_set_strength )
116128
/* Fetch the object. */
117129
COLLATOR_METHOD_FETCH_OBJECT;
118130

131+
if (collator_check_initialized(co) == FAILURE) {
132+
RETURN_THROWS();
133+
}
134+
119135
/* Set given strength. */
120136
ucol_setStrength( co->ucoll, static_cast<UColAttributeValue>(strength) );
121137

‎ext/intl/collator/collator_class.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ typedef struct {
5050
#define php_intl_collator_fetch_object(obj) ZEND_CONTAINER_OF(obj, Collator_object, zo)
5151
#define Z_INTL_COLLATOR_P(zv) php_intl_collator_fetch_object(Z_OBJ_P(zv))
5252

53+
static zend_always_inline zend_result collator_check_initialized(Collator_object *co)
54+
{
55+
ZEND_ASSERT(co != NULL);
56+
57+
if (UNEXPECTED(co->ucoll == NULL)) {
58+
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
59+
intl_errors_set_custom_msg(COLLATOR_ERROR_P( co ), "Object not initialized");
60+
zend_throw_error(NULL, "Object not initialized");
61+
62+
return FAILURE;
63+
}
64+
65+
return SUCCESS;
66+
}
67+
5368
#ifdef __cplusplus
5469
extern "C" {
5570
#endif

‎ext/intl/collator/collator_compare.cpp‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ U_CFUNC PHP_FUNCTION( collator_compare )
5555
/* Fetch the object. */
5656
COLLATOR_METHOD_FETCH_OBJECT;
5757

58-
if (!co || !co->ucoll) {
59-
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
60-
intl_errors_set_custom_msg(COLLATOR_ERROR_P( co ), "Object not initialized");
61-
zend_throw_error(NULL, "Object not initialized");
62-
58+
if (collator_check_initialized(co) == FAILURE) {
6359
RETURN_THROWS();
6460
}
6561

‎ext/intl/collator/collator_locale.cpp‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ U_CFUNC PHP_FUNCTION( collator_get_locale )
4848
/* Fetch the object. */
4949
COLLATOR_METHOD_FETCH_OBJECT;
5050

51-
if (!co || !co->ucoll) {
52-
intl_error_set_code( nullptr, COLLATOR_ERROR_CODE( co ) );
53-
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ), "Object not initialized");
54-
zend_throw_error(nullptr, "Object not initialized");
55-
51+
if (collator_check_initialized(co) == FAILURE) {
5652
RETURN_THROWS();
5753
}
5854

‎ext/intl/collator/collator_sort.cpp‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,7 @@ U_CFUNC PHP_FUNCTION( collator_sort_with_sort_keys )
433433
/* Fetch the object. */
434434
COLLATOR_METHOD_FETCH_OBJECT;
435435

436-
if (!co || !co->ucoll) {
437-
intl_error_set_code( nullptr, COLLATOR_ERROR_CODE( co ) );
438-
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ), "Object not initialized");
439-
zend_throw_error(NULL, "Object not initialized");
440-
436+
if (collator_check_initialized(co) == FAILURE) {
441437
RETURN_THROWS();
442438
}
443439

@@ -598,11 +594,7 @@ U_CFUNC PHP_FUNCTION( collator_get_sort_key )
598594
/* Fetch the object. */
599595
COLLATOR_METHOD_FETCH_OBJECT;
600596

601-
if (!co || !co->ucoll) {
602-
intl_error_set_code( nullptr, COLLATOR_ERROR_CODE( co ) );
603-
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ), "Object not initialized");
604-
zend_throw_error(NULL, "Object not initialized");
605-
597+
if (collator_check_initialized(co) == FAILURE) {
606598
RETURN_THROWS();
607599
}
608600

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
Collator attribute and strength methods on unconstructed object
3+
--EXTENSIONS--
4+
intl
5+
--FILE--
6+
<?php
7+
8+
class Collator2 extends Collator {
9+
public function __construct() {
10+
// omitting parent::__construct($someLocale);
11+
}
12+
}
13+
14+
$c = new Collator2();
15+
16+
$methods = [
17+
'getAttribute' => fn() => $c->getAttribute(Collator::NUMERIC_COLLATION),
18+
'setAttribute' => fn() => $c->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON),
19+
'getStrength' => fn() => $c->getStrength(),
20+
'setStrength' => fn() => $c->setStrength(Collator::SECONDARY),
21+
];
22+
23+
foreach ($methods as $method => $call) {
24+
try {
25+
$call();
26+
} catch (Error $e) {
27+
echo $method, ': ', $e::class, ': ', $e->getMessage(), PHP_EOL;
28+
}
29+
}
30+
31+
$functions = [
32+
'collator_get_attribute' => fn() => collator_get_attribute($c, Collator::NUMERIC_COLLATION),
33+
'collator_set_attribute' => fn() => collator_set_attribute($c, Collator::NUMERIC_COLLATION, Collator::ON),
34+
'collator_get_strength' => fn() => collator_get_strength($c),
35+
'collator_set_strength' => fn() => collator_set_strength($c, Collator::SECONDARY),
36+
];
37+
38+
foreach ($functions as $function => $call) {
39+
try {
40+
$call();
41+
} catch (Error $e) {
42+
echo $function, ': ', $e::class, ': ', $e->getMessage(), PHP_EOL;
43+
}
44+
}
45+
46+
?>
47+
--EXPECT--
48+
getAttribute: Error: Object not initialized
49+
setAttribute: Error: Object not initialized
50+
getStrength: Error: Object not initialized
51+
setStrength: Error: Object not initialized
52+
collator_get_attribute: Error: Object not initialized
53+
collator_set_attribute: Error: Object not initialized
54+
collator_get_strength: Error: Object not initialized
55+
collator_set_strength: Error: Object not initialized

0 commit comments

Comments
 (0)