Skip to content

Commit 7463e70

Browse files
nielsdosGirgias
authored andcommitted
Handle exceptions from __toString in XXH3's initialization
The initialization routine for XXH3 was not prepared for exceptions from seed. Fix this by using try_convert_to_string. For discussion, please see: GH-10305 Closes GH-10352 Signed-off-by: George Peter Banyard <girgias@php.net>
1 parent 398a10a commit 7463e70

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ PHP NEWS
2626
. Fixed bug #67244 (Wrong owner:group for listening unix socket).
2727
(Jakub Zelenka)
2828

29+
- Hash:
30+
. Handle exceptions from __toString in XXH3's initialization (nielsdos)
31+
2932
- LDAP:
3033
. Fixed bug GH-10112 (LDAP\Connection::__construct() refers to ldap_create()).
3134
(cmb)

ext/hash/hash_xxhash.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ zend_always_inline static void _PHP_XXH3_Init(PHP_XXH3_64_CTX *ctx, HashTable *a
174174
func_init_seed(&ctx->s, (XXH64_hash_t)Z_LVAL_P(_seed));
175175
return;
176176
} else if (_secret) {
177-
convert_to_string(_secret);
177+
if (!try_convert_to_string(_secret)) {
178+
return;
179+
}
178180
size_t len = Z_STRLEN_P(_secret);
179181
if (len < PHP_XXH3_SECRET_SIZE_MIN) {
180182
zend_throw_error(NULL, "%s: Secret length must be >= %u bytes, %zu bytes passed", algo_name, XXH3_SECRET_SIZE_MIN, len);

ext/hash/tests/xxhash_secret.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Hash: xxHash secret
33
--FILE--
44
<?php
55

6+
class StringableThrowingClass {
7+
public function __toString(): string {
8+
throw new Exception('exception in __toString');
9+
return '';
10+
}
11+
}
12+
613
foreach (["xxh3", "xxh128"] as $a) {
714

815
//$secret = random_bytes(256);
@@ -14,6 +21,12 @@ foreach (["xxh3", "xxh128"] as $a) {
1421
var_dump($e->getMessage());
1522
}
1623

24+
try {
25+
$ctx = hash_init($a, options: ["secret" => new StringableThrowingClass()]);
26+
} catch (Throwable $e) {
27+
var_dump($e->getMessage());
28+
}
29+
1730
try {
1831
$ctx = hash_init($a, options: ["secret" => str_repeat('a', 17)]);
1932
} catch (Throwable $e) {
@@ -35,8 +48,10 @@ foreach (["xxh3", "xxh128"] as $a) {
3548
?>
3649
--EXPECT--
3750
string(67) "xxh3: Only one of seed or secret is to be passed for initialization"
51+
string(23) "exception in __toString"
3852
string(57) "xxh3: Secret length must be >= 136 bytes, 17 bytes passed"
3953
8028aa834c03557a == 8028aa834c03557a == true
4054
string(69) "xxh128: Only one of seed or secret is to be passed for initialization"
55+
string(23) "exception in __toString"
4156
string(59) "xxh128: Secret length must be >= 136 bytes, 17 bytes passed"
4257
54279097795e7218093a05d4d781cbb9 == 54279097795e7218093a05d4d781cbb9 == true

0 commit comments

Comments
 (0)