Skip to content
Merged
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 @@ -50,6 +50,8 @@ PHP NEWS
IntlCalendar::fromDateTime() fails after adopting a TimeZone.
(Ilia Alshanetsky)
. Added IntlNumberFormatter::createFromSkeleton() (GH-24165). (David Carlier)
. Fixed UConverter::setSubstChars() truncating the substitution string
length. (Weilin Du)

- Lexbor:
. Merge patches lexbor/lexbor@8a14bc0 and lexbor/lexbor@f67ce4b, fixing a
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ PHP 8.7 UPGRADE NOTES
curl_setopt() return false, instead of being silently interpreted as a
combination of redirect types.

- Intl:
. UConverter::setSubstChars() now rejects substitution strings longer than
127 bytes. Previously, the length could be truncated and only a prefix of
the string used for substitution.

- PDO:
. PDOStatement::bindColumn() now throws a ValueError when the column name is
not present in the result set. It previously reported the condition
Expand Down
29 changes: 14 additions & 15 deletions ext/intl/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,22 +519,31 @@ PHP_METHOD(UConverter, __construct) {
}
/* }}} */

static void php_converter_set_subst_chars(UConverter *cnv, const zend_string *subst, UErrorCode *error)
{
if (UNEXPECTED(ZSTR_LEN(subst) > SCHAR_MAX)) {
*error = U_ILLEGAL_ARGUMENT_ERROR;
return;
}

ucnv_setSubstChars(cnv, ZSTR_VAL(subst), (int8_t) ZSTR_LEN(subst), error);
}

/* {{{ */
PHP_METHOD(UConverter, setSubstChars) {
php_converter_object *objval = CONV_GET(ZEND_THIS);
char *chars;
size_t chars_len;
zend_string *chars;
int ret = 1;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(chars, chars_len)
Z_PARAM_STR(chars)
ZEND_PARSE_PARAMETERS_END();

intl_errors_reset(&objval->error);

if (objval->src) {
UErrorCode error = U_ZERO_ERROR;
ucnv_setSubstChars(objval->src, chars, chars_len, &error);
php_converter_set_subst_chars(objval->src, chars, &error);
if (U_FAILURE(error)) {
THROW_UFAILURE(objval, error);
ret = 0;
Expand All @@ -546,7 +555,7 @@ PHP_METHOD(UConverter, setSubstChars) {

if (objval->dest) {
UErrorCode error = U_ZERO_ERROR;
ucnv_setSubstChars(objval->dest, chars, chars_len, &error);
php_converter_set_subst_chars(objval->dest, chars, &error);
if (U_FAILURE(error)) {
THROW_UFAILURE(objval, error);
ret = 0;
Expand Down Expand Up @@ -652,16 +661,6 @@ static zend_string* php_converter_do_convert(UConverter *dest_cnv,
}
/* }}} */

static void php_converter_set_subst_chars(UConverter *cnv, const zend_string *subst, UErrorCode *error)
{
if (UNEXPECTED(ZSTR_LEN(subst) > SCHAR_MAX)) {
*error = U_ILLEGAL_ARGUMENT_ERROR;
return;
}

ucnv_setSubstChars(cnv, ZSTR_VAL(subst), (int8_t) ZSTR_LEN(subst), error);
}

/* {{{ */
#define UCNV_REASON_CASE(v) case (UCNV_ ## v) : RETURN_STRINGL( "REASON_" #v , sizeof( "REASON_" #v ) - 1);
PHP_METHOD(UConverter, reasonText) {
Expand Down
30 changes: 30 additions & 0 deletions ext/intl/tests/uconverter_setSubstChars_overlong.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
UConverter::setSubstChars() rejects an overlong substitution string
--CREDITS--
Wong Wai Kin
--EXTENSIONS--
intl
--INI--
intl.use_exceptions=false
--FILE--
<?php
$converter = new UConverter('US-ASCII', 'UTF-8');
var_dump($converter->setSubstChars('?'));
var_dump($converter->setSubstChars(str_repeat('X', 257)));
var_dump($converter->getErrorCode() === U_ILLEGAL_ARGUMENT_ERROR);
var_dump($converter->getSubstChars());
var_dump($converter->convert("\xE2\x98\x83"));

$converter = (new ReflectionClass(UConverter::class))->newInstanceWithoutConstructor();
$converter->setSourceEncoding('UTF-8');
var_dump($converter->setSubstChars(str_repeat('X', 128)));
var_dump($converter->getErrorCode() === U_INVALID_STATE_ERROR);
?>
--EXPECT--
bool(true)
bool(false)
bool(true)
string(1) "?"
string(1) "?"
bool(false)
bool(true)
Loading