Skip to content

Commit 4e3e752

Browse files
committed
ext/intl: Fix argument positions in transliterator_transliterate()
1 parent 0c52780 commit 4e3e752

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ PHP NEWS
3333

3434
- Intl:
3535
. Fix incorrect argument positions for uninitialized calendar arguments in
36-
IntlCalendar::equals(), ::before(), ::after(), and ::isEquivalentTo().
36+
IntlCalendar::equals(), ::before(), ::after(), and ::isEquivalentTo(), and
37+
for invalid start/end arguments in transliterator_transliterate().
3738
(Weilin Du)
3839
. Fixed IntlTimeZone::getDisplayName() to synchronize object error state
3940
for invalid display types. (Weilin Du)

ext/intl/tests/transliterator_transliterate_error.phpt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,36 @@ ini_set("intl.error_level", E_WARNING);
99

1010
$tr = Transliterator::create("latin");
1111

12+
function dump_value_error(callable $callback): void {
13+
try {
14+
$callback();
15+
} catch (ValueError $exception) {
16+
echo $exception->getMessage() . "\n";
17+
}
18+
}
19+
1220
//Arguments
1321
var_dump(transliterator_transliterate($tr,"str",7));
1422

15-
try {
23+
dump_value_error(function() use ($tr) {
1624
transliterator_transliterate($tr,"str",7,6);
17-
} catch (ValueError $exception) {
18-
echo $exception->getMessage() . "\n";
19-
}
25+
});
26+
27+
dump_value_error(function() use ($tr) {
28+
transliterator_transliterate($tr,"str", 0, -2);
29+
});
30+
31+
dump_value_error(function() use ($tr) {
32+
transliterator_transliterate($tr,"str", -1);
33+
});
34+
35+
dump_value_error(function() {
36+
transliterator_transliterate("latin","str", -1);
37+
});
38+
39+
dump_value_error(function() use ($tr) {
40+
$tr->transliterate("str", 7, 6);
41+
});
2042

2143
//bad UTF-8
2244
transliterator_transliterate($tr, "\x80\x03");
@@ -26,7 +48,11 @@ echo "Done.\n";
2648
--EXPECTF--
2749
Warning: transliterator_transliterate(): transliterator_transliterate: Neither "start" nor the "end" arguments can exceed the number of UTF-16 code units (in this case, 3) in %s on line %d
2850
bool(false)
29-
transliterator_transliterate(): Argument #2 ($string) must be less than or equal to argument #3 ($end)
51+
transliterator_transliterate(): Argument #3 ($start) must be less than or equal to argument #4 ($end)
52+
transliterator_transliterate(): Argument #4 ($end) must be greater than or equal to -1
53+
transliterator_transliterate(): Argument #3 ($start) must be greater than or equal to 0
54+
transliterator_transliterate(): Argument #3 ($start) must be greater than or equal to 0
55+
Transliterator::transliterate(): Argument #2 ($start) must be less than or equal to argument #3 ($end)
3056

3157
Warning: transliterator_transliterate(): String conversion of string to UTF-16 failed in %s on line %d
3258
Done.

ext/intl/transliterator/transliterator_methods.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,16 @@ PHP_FUNCTION( transliterator_transliterate )
271271
zend_long start = 0,
272272
limit = -1;
273273
int success = 0;
274+
bool is_method;
274275
zval tmp_object;
275276
TRANSLITERATOR_METHOD_INIT_VARS;
276277

277278
object = getThis();
279+
is_method = object != NULL;
278280

279281
ZVAL_UNDEF(&tmp_object);
280282

281-
if (object == NULL) {
283+
if (!is_method) {
282284
/* in non-OOP version, accept both a transliterator and a string */
283285
zend_string *arg1_str;
284286
zend_object *arg1_obj;
@@ -315,17 +317,17 @@ PHP_FUNCTION( transliterator_transliterate )
315317
}
316318

317319
if (limit < -1) {
318-
zend_argument_value_error(object ? 3 : 4, "must be greater than or equal to -1");
320+
zend_argument_value_error(is_method ? 3 : 4, "must be greater than or equal to -1");
319321
goto cleanup_object;
320322
}
321323

322324
if (start < 0) {
323-
zend_argument_value_error(object ? 2 : 3, "must be greater than or equal to 0");
325+
zend_argument_value_error(is_method ? 2 : 3, "must be greater than or equal to 0");
324326
goto cleanup_object;
325327
}
326328

327329
if (limit != -1 && start > limit) {
328-
zend_argument_value_error(object ? 2 : 3, "must be less than or equal to argument #%d ($end)", object ? 3 : 4);
330+
zend_argument_value_error(is_method ? 2 : 3, "must be less than or equal to argument #%d ($end)", is_method ? 3 : 4);
329331
goto cleanup_object;
330332
}
331333

0 commit comments

Comments
 (0)