ext/intl: prevent creation of invalid UConverter instance (#19396)

This commit is contained in:
Gina Peter Banyard 2025-08-07 13:46:58 +01:00 committed by GitHub
parent abe75ca850
commit ca5667bc14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 41 additions and 13 deletions

View file

@ -24,6 +24,7 @@
#include "../intl_error.h"
#include "../intl_common.h"
#include "converter_arginfo.h"
#include "php_intl.h"
typedef struct _php_converter_object {
UConverter *src, *dest;
@ -370,7 +371,10 @@ static bool php_converter_set_encoding(php_converter_object *objval,
/* Should never happen */
actual_encoding = "(unknown)";
}
php_error_docref(NULL, E_WARNING, "Ambiguous encoding specified, using %s", actual_encoding);
char *msg;
spprintf(&msg, 0, "Ambiguous encoding specified, using %s", actual_encoding);
intl_error_set(NULL, error, msg);
efree(msg);
} else if (U_FAILURE(error)) {
if (objval) {
THROW_UFAILURE(objval, error);
@ -530,10 +534,23 @@ PHP_METHOD(UConverter, __construct) {
Z_PARAM_STRING_OR_NULL(src, src_len)
ZEND_PARSE_PARAMETERS_END();
php_converter_set_encoding(objval, &(objval->src), src, src_len );
php_converter_set_encoding(objval, &(objval->dest), dest, dest_len);
const bool old_use_exception = INTL_G(use_exceptions);
const zend_long old_error_level = INTL_G(error_level);
INTL_G(use_exceptions) = true;
INTL_G(error_level) = 0;
if (UNEXPECTED(!php_converter_set_encoding(objval, &(objval->src), src, src_len))) {
ZEND_ASSERT(EG(exception));
goto cleanup;
}
if (UNEXPECTED(!php_converter_set_encoding(objval, &(objval->dest), dest, dest_len))) {
ZEND_ASSERT(EG(exception));
goto cleanup;
}
php_converter_resolve_callback(&objval->to_cache, Z_OBJ_P(ZEND_THIS), ZEND_STRL("toUCallback"));
php_converter_resolve_callback(&objval->from_cache, Z_OBJ_P(ZEND_THIS), ZEND_STRL("fromUCallback"));
cleanup:
INTL_G(use_exceptions) = old_use_exception;
INTL_G(error_level) = old_error_level;
}
/* }}} */

View file

@ -1,5 +1,5 @@
--TEST--
Bug #75317 (UConverter::setDestinationEncoding changes source instead of destinatination)
Bug #75317 (UConverter::setDestinationEncoding changes source instead of destination)
--EXTENSIONS--
intl
--FILE--

View file

@ -1,15 +1,22 @@
--TEST--
Basic UConverter::convert() usage
--INI--
intl.error_level = E_WARNING
--EXTENSIONS--
intl
--FILE--
<?php
$c = new UConverter('utf-8', "\x80");
var_dump($c);
?>
--EXPECTF--
Warning: UConverter::__construct(): returned error 4: U_FILE_ACCESS_ERROR in %s on line %d
object(UConverter)#%d (0) {
try {
$c = new UConverter("\x80", 'utf-8');
var_dump($c);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
try {
$c = new UConverter('utf-8', "\x80");
var_dump($c);
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
IntlException: UConverter::__construct(): returned error 4: U_FILE_ACCESS_ERROR
IntlException: UConverter::__construct(): returned error 4: U_FILE_ACCESS_ERROR

View file

@ -4,9 +4,13 @@ Bug #66873 - crash in UConverter with invalid encoding
intl
--FILE--
<?php
try {
$o = new UConverter(1, 1);
$o->toUCallback(1, 1, 1, $b);
var_dump($o->getErrorCode());
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
int(27)
IntlException: UConverter::__construct(): returned error 4: U_FILE_ACCESS_ERROR