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
9 changes: 9 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ PHP NEWS
className/classList writes and attribute removals. (Ilia Alshanetsky)

- Intl:
. Fixed grapheme_strpos() and grapheme_strrpos() with an empty needle
returning UTF-16 offsets instead of grapheme offsets. (Ilia Alshanetsky)
. Fixed a memory leak when dumping IntlCalendar instances. (Ilia Alshanetsky)
. Fixed Collator::sortWithSortKeys() allocating fixed 2MiB buffers
regardless of array size. (Ilia Alshanetsky)
Expand All @@ -22,6 +24,13 @@ PHP NEWS
read. (iliaal)
. Fixed a use-after-free when IntlRuleBasedBreakIterator is constructed
from compiled rules. (iliaal)
. Fixed Spoofchecker methods not recording the ICU error code when an ICU
call fails. (Ilia Alshanetsky)

- MBString:
. Fixed mb_ereg_replace() emitting a NUL or out-of-bounds bytes in the
replacement when a \k<name> backref has no closing delimiter.
(Ilia Alshanetsky)

- PDO_PGSQL:
. Added Pdo\Pgsql::ATTR_CHUNK_SIZE to fetch a result set in chunks of the
Expand Down
67 changes: 38 additions & 29 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -3258,7 +3258,12 @@ PHP_FUNCTION(date_format)
}
/* }}} */

static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{ */
typedef enum {
PHP_DATE_MODIFY_WARNING,
PHP_DATE_MODIFY_THROW
} php_date_modify_error_mode;

static bool php_date_modify(zval *object, char *modify, size_t modify_len, const php_date_modify_error_mode error_mode) /* {{{ */
{
php_date_obj *dateobj;
timelib_time *tmp_time;
Expand All @@ -3278,10 +3283,22 @@ static bool php_date_modify(zval *object, char *modify, size_t modify_len) /* {{

if (err && err->error_count) {
/* spit out the first library error message, at least */
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
if (error_mode == PHP_DATE_MODIFY_THROW) {
zend_string *func_name = get_active_function_or_method_name();
zend_throw_exception_ex(date_ce_date_malformed_string_exception, 0,
"%s(): Failed to parse time string (%s) at position %d (%c): %s",
ZSTR_VAL(func_name),
modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
zend_string_release_ex(func_name, false);
} else {
php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
err->error_messages[0].position,
err->error_messages[0].character ? err->error_messages[0].character : ' ',
err->error_messages[0].message);
}
timelib_time_dtor(tmp_time);
return false;
}
Expand Down Expand Up @@ -3350,7 +3367,7 @@ PHP_FUNCTION(date_modify)
RETURN_THROWS();
}

if (!php_date_modify(object, modify, modify_len)) {
if (!php_date_modify(object, modify, modify_len, PHP_DATE_MODIFY_WARNING)) {
RETURN_FALSE;
}

Expand All @@ -3364,21 +3381,16 @@ PHP_METHOD(DateTime, modify)
zval *object;
char *modify;
size_t modify_len;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(modify, modify_len)
ZEND_PARSE_PARAMETERS_END();

zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
if (!php_date_modify(object, modify, modify_len)) {
zend_restore_error_handling(&zeh);
if (!php_date_modify(object, modify, modify_len, PHP_DATE_MODIFY_THROW)) {
RETURN_THROWS();
}

zend_restore_error_handling(&zeh);

RETURN_OBJ_COPY(Z_OBJ_P(object));
}
/* }}} */
Expand All @@ -3389,7 +3401,6 @@ PHP_METHOD(DateTimeImmutable, modify)
zval *object, new_object;
char *modify;
size_t modify_len;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -3398,15 +3409,11 @@ PHP_METHOD(DateTimeImmutable, modify)

date_clone_immutable(object, &new_object);

zend_replace_error_handling(EH_THROW, date_ce_date_malformed_string_exception, &zeh);
if (!php_date_modify(&new_object, modify, modify_len)) {
if (!php_date_modify(&new_object, modify, modify_len, PHP_DATE_MODIFY_THROW)) {
zval_ptr_dtor(&new_object);
zend_restore_error_handling(&zeh);
RETURN_THROWS();
}

zend_restore_error_handling(&zeh);

RETURN_OBJ(Z_OBJ(new_object));
}
/* }}} */
Expand Down Expand Up @@ -3463,7 +3470,7 @@ PHP_METHOD(DateTimeImmutable, add)
}
/* }}} */

static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
static void php_date_sub(zval *object, zval *interval, zval *return_value, const bool should_throw) /* {{{ */
{
php_date_obj *dateobj;
php_interval_obj *intobj;
Expand All @@ -3475,7 +3482,15 @@ static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{
DATE_CHECK_INITIALIZED(intobj->initialized, Z_OBJCE_P(interval));

if (intobj->diff->have_weekday_relative || intobj->diff->have_special_relative) {
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
if (should_throw) {
zend_string *func_name = get_active_function_or_method_name();
zend_throw_exception_ex(date_ce_date_invalid_operation_exception, 0,
"%s(): Only non-special relative time specifications are supported for subtraction",
ZSTR_VAL(func_name));
zend_string_release_ex(func_name, false);
} else {
php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
}
return;
}

Expand All @@ -3497,7 +3512,7 @@ PHP_FUNCTION(date_sub)
RETURN_THROWS();
}

php_date_sub(object, interval, return_value);
php_date_sub(object, interval, return_value, false);
RETURN_OBJ_COPY(Z_OBJ_P(object));
}
/* }}} */
Expand All @@ -3506,15 +3521,12 @@ PHP_FUNCTION(date_sub)
PHP_METHOD(DateTime, sub)
{
zval *object, *interval;
zend_error_handling zeh;

if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
RETURN_THROWS();
}

zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
php_date_sub(object, interval, return_value);
zend_restore_error_handling(&zeh);
php_date_sub(object, interval, return_value, true);

RETURN_OBJ_COPY(Z_OBJ_P(object));
}
Expand All @@ -3524,7 +3536,6 @@ PHP_METHOD(DateTime, sub)
PHP_METHOD(DateTimeImmutable, sub)
{
zval *object, *interval, new_object;
zend_error_handling zeh;

object = ZEND_THIS;
ZEND_PARSE_PARAMETERS_START(1, 1)
Expand All @@ -3533,9 +3544,7 @@ PHP_METHOD(DateTimeImmutable, sub)

date_clone_immutable(object, &new_object);

zend_replace_error_handling(EH_THROW, date_ce_date_invalid_operation_exception, &zeh);
php_date_sub(&new_object, interval, return_value);
zend_restore_error_handling(&zeh);
php_date_sub(&new_object, interval, return_value, true);

RETURN_OBJ(Z_OBJ(new_object));
}
Expand Down
5 changes: 2 additions & 3 deletions ext/hash/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <config.h>
#endif

#include <math.h>
#include "php_hash.h"
#include "ext/standard/info.h"
#include "ext/standard/file.h"
Expand Down Expand Up @@ -1033,10 +1032,10 @@ PHP_FUNCTION(hash_pbkdf2)
}
digest_length = length;
if (!raw_output) {
digest_length = (zend_long) ceil((float) length / 2.0);
digest_length = length / 2 + (length % 2);
}

loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
loops = (digest_length - 1) / ops->digest_size + 1;

result = safe_emalloc(loops, ops->digest_size, 0);

Expand Down
19 changes: 19 additions & 0 deletions ext/hash/tests/hash_pbkdf2_large_length.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Hash: hash_pbkdf2() function : large output length
--FILE--
<?php

$length = 33554433;
$hash = hash_pbkdf2('md5', 'password', 'salt', 1, $length);

/* The last hexit comes from the first byte of the final PBKDF2 block. */
$block = intdiv(intdiv($length + 1, 2) - 1, 16) + 1;
$expected = bin2hex(hash_hmac('md5', 'salt' . pack('N', $block), 'password', true));

var_dump(strlen($hash));
var_dump($hash[$length - 1] === $expected[0]);

?>
--EXPECT--
int(33554433)
bool(true)
17 changes: 17 additions & 0 deletions ext/hash/tests/hash_pbkdf2_max_length.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Hash: hash_pbkdf2() function : output length of PHP_INT_MAX
--SKIPIF--
<?php
if (PHP_INT_SIZE == 4) die("skip this test is not for 32bit platforms");
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
?>
--INI--
memory_limit=128M
--FILE--
<?php

hash_pbkdf2('md5', 'password', 'salt', 1, PHP_INT_MAX);

?>
--EXPECTF--
Fatal error: Allowed memory size of 134217728 bytes exhausted%s(tried to allocate 4611686018427387904 bytes) in %s on line %d
17 changes: 17 additions & 0 deletions ext/hash/tests/hash_pbkdf2_max_length_raw.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Hash: hash_pbkdf2() function : raw output length of PHP_INT_MAX
--SKIPIF--
<?php
if (PHP_INT_SIZE == 4) die("skip this test is not for 32bit platforms");
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
?>
--INI--
memory_limit=128M
--FILE--
<?php

hash_pbkdf2('md5', 'password', 'salt', 1, PHP_INT_MAX, true);

?>
--EXPECTF--
Fatal error: Allowed memory size of 134217728 bytes exhausted%s(tried to allocate 9223372036854775808 bytes) in %s on line %d
6 changes: 5 additions & 1 deletion ext/intl/grapheme/grapheme_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ U_CFUNC int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char
ret_pos = -1;
goto finish;
}
ret_pos = last && offset >= 0 ? uhaystack_len : offset_pos;
if (last && offset >= 0) {
ret_pos = grapheme_count_graphemes(bi, uhaystack, uhaystack_len);
} else {
ret_pos = grapheme_count_graphemes(bi, uhaystack, offset_pos);
}
goto finish;
}

Expand Down
6 changes: 6 additions & 0 deletions ext/intl/spoofchecker/spoofchecker_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ U_CFUNC PHP_METHOD(Spoofchecker, isSuspicious)
ret = intl_icu_compat_uspoof_check_utf8(co->uspoof, ZSTR_VAL(text), ZSTR_LEN(text), co->uspoofres, SPOOFCHECKER_ERROR_CODE_P(co));

if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));

if (intl_icu_compat_uspoof_check_result_mismatch(co->uspoofres, ret, &errmask, SPOOFCHECKER_ERROR_CODE_P(co))) {
Expand Down Expand Up @@ -83,6 +84,7 @@ U_CFUNC PHP_METHOD(Spoofchecker, areConfusable)
ret = uspoof_areConfusableUTF8(co->uspoof, ZSTR_VAL(s1), (int32_t)ZSTR_LEN(s1), ZSTR_VAL(s2), (int32_t)ZSTR_LEN(s2), SPOOFCHECKER_ERROR_CODE_P(co));
}
if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
RETURN_TRUE;
}
Expand All @@ -109,6 +111,7 @@ U_CFUNC PHP_METHOD(Spoofchecker, setAllowedLocales)
uspoof_setAllowedLocales(co->uspoof, ZSTR_VAL(locales), SPOOFCHECKER_ERROR_CODE_P(co));

if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
return;
}
Expand All @@ -130,6 +133,7 @@ U_CFUNC PHP_METHOD(Spoofchecker, setChecks)
uspoof_setChecks(co->uspoof, checks, SPOOFCHECKER_ERROR_CODE_P(co));

if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
}
}
Expand Down Expand Up @@ -220,6 +224,7 @@ U_CFUNC PHP_METHOD(Spoofchecker, setAllowedChars)
efree(upattern);

if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
}
}
Expand Down Expand Up @@ -355,6 +360,7 @@ U_CFUNC PHP_METHOD(Spoofchecker, areBidiConfusable)
ret = uspoof_areBidiConfusableUTF8(co->uspoof, (UBiDiDirection)direction, ZSTR_VAL(s1), (int32_t)ZSTR_LEN(s1), ZSTR_VAL(s2), (int32_t)ZSTR_LEN(s2), SPOOFCHECKER_ERROR_CODE_P(co));
}
if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
intl_error_set_code(NULL, SPOOFCHECKER_ERROR_CODE(co));
php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
RETURN_TRUE;
}
Expand Down
34 changes: 34 additions & 0 deletions ext/intl/tests/grapheme_empty_offset_multibyte.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
grapheme_strpos() family with empty needle and offset on multi-code-unit graphemes
--EXTENSIONS--
intl
--FILE--
<?php

var_dump(grapheme_strpos("😀x", ""));
var_dump(grapheme_strpos("😀x", "", 0));
var_dump(grapheme_strpos("😀x", "", 1));
var_dump(grapheme_stripos("😀x", "", 1));
var_dump(grapheme_strpos("😀x", "", -1));
var_dump(grapheme_strrpos("😀x", ""));
var_dump(grapheme_strrpos("😀x", "", 1));
var_dump(grapheme_strripos("😀x", "", 1));
var_dump(grapheme_strrpos("😀x", "", -1));
try {
var_dump(grapheme_strpos("😀x", "", 5));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

?>
--EXPECT--
int(0)
int(0)
int(1)
int(1)
int(1)
int(2)
int(2)
int(2)
int(1)
ValueError: grapheme_strpos(): Argument #3 ($offset) must be contained in argument #1 ($haystack)
18 changes: 18 additions & 0 deletions ext/intl/tests/spoofchecker_setchecks_error_code.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Spoofchecker::setChecks() records the ICU error code
--EXTENSIONS--
intl
--SKIPIF--
<?php if (!class_exists("Spoofchecker")) print 'skip'; ?>
--FILE--
<?php

$s = new Spoofchecker();
$s->setChecks(1 << 20);
var_dump(intl_get_error_code(), intl_get_error_message());

?>
--EXPECTF--
Warning: Spoofchecker::setChecks(): (1) U_ILLEGAL_ARGUMENT_ERROR in %s on line %d
int(1)
string(24) "U_ILLEGAL_ARGUMENT_ERROR"
Loading