Skip to content

Commit 74eff98

Browse files
authored
1 parent 7b32a14 commit 74eff98

File tree

8 files changed

+106
-23
lines changed

8 files changed

+106
-23
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ PHP NEWS
2020
. Deprecated DOM_PHP_ERR constant. (nielsdos)
2121
. Removed DOMImplementation::getFeature(). (nielsdos)
2222

23+
- Hash:
24+
. Deprecated passing incorrect data types for options to ext/hash functions.
25+
(nielsdos)
26+
2327
- PHPDBG:
2428
. array out of bounds, stack overflow handled for segfault handler on windows.
2529
(David Carlier)

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,10 @@ PHP 8.4 UPGRADE NOTES
413413
. Deprecated DOM_PHP_ERR constant.
414414
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_dom_php_err_constant
415415

416+
- Hash:
417+
. Deprecated passing incorrect data types for options to ext/hash functions.
418+
RFC: https://wiki.php.net/rfc/deprecations_php_8_4
419+
416420
- Intl:
417421
. Calling intlcal_set() as well as calling IntlCalendar::set() with
418422
more than 2 arguments is deprecated. Use either IntlCalendar::setDate()

ext/hash/hash_murmur.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx, HashTable *args)
4242
zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
4343
/* This might be a bit too restrictive, but thinking that a seed might be set
4444
once and for all, it should be done a clean way. */
45-
if (seed && IS_LONG == Z_TYPE_P(seed)) {
46-
ctx->h = (uint32_t)Z_LVAL_P(seed);
45+
if (seed) {
46+
if (IS_LONG == Z_TYPE_P(seed)) {
47+
ctx->h = (uint32_t) Z_LVAL_P(seed);
48+
} else {
49+
php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
50+
ctx->h = 0;
51+
}
4752
} else {
4853
ctx->h = 0;
4954
}
@@ -99,12 +104,17 @@ PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx, HashTable *args)
99104
zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
100105
/* This might be a bit too restrictive, but thinking that a seed might be set
101106
once and for all, it should be done a clean way. */
102-
if (seed && IS_LONG == Z_TYPE_P(seed)) {
103-
uint32_t _seed = (uint32_t)Z_LVAL_P(seed);
104-
ctx->h[0] = _seed;
105-
ctx->h[1] = _seed;
106-
ctx->h[2] = _seed;
107-
ctx->h[3] = _seed;
107+
if (seed) {
108+
if (IS_LONG == Z_TYPE_P(seed)) {
109+
uint32_t _seed = (uint32_t)Z_LVAL_P(seed);
110+
ctx->h[0] = _seed;
111+
ctx->h[1] = _seed;
112+
ctx->h[2] = _seed;
113+
ctx->h[3] = _seed;
114+
} else {
115+
php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
116+
memset(&ctx->h, 0, sizeof ctx->h);
117+
}
108118
} else {
109119
memset(&ctx->h, 0, sizeof ctx->h);
110120
}
@@ -173,10 +183,15 @@ PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx, HashTable *args)
173183
zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
174184
/* This might be a bit too restrictive, but thinking that a seed might be set
175185
once and for all, it should be done a clean way. */
176-
if (seed && IS_LONG == Z_TYPE_P(seed)) {
177-
uint64_t _seed = (uint64_t)Z_LVAL_P(seed);
178-
ctx->h[0] = _seed;
179-
ctx->h[1] = _seed;
186+
if (seed) {
187+
if (IS_LONG == Z_TYPE_P(seed)) {
188+
uint64_t _seed = (uint64_t) Z_LVAL_P(seed);
189+
ctx->h[0] = _seed;
190+
ctx->h[1] = _seed;
191+
} else {
192+
php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
193+
memset(&ctx->h, 0, sizeof ctx->h);
194+
}
180195
} else {
181196
memset(&ctx->h, 0, sizeof ctx->h);
182197
}

ext/hash/hash_xxhash.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ PHP_HASH_API void PHP_XXH32Init(PHP_XXH32_CTX *ctx, HashTable *args)
4646
zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
4747
/* This might be a bit too restrictive, but thinking that a seed might be set
4848
once and for all, it should be done a clean way. */
49-
if (seed && IS_LONG == Z_TYPE_P(seed)) {
50-
XXH32_reset(&ctx->s, (XXH32_hash_t)Z_LVAL_P(seed));
51-
} else {
52-
XXH32_reset(&ctx->s, 0);
49+
if (seed) {
50+
if (IS_LONG == Z_TYPE_P(seed)) {
51+
XXH32_reset(&ctx->s, (XXH32_hash_t)Z_LVAL_P(seed));
52+
return;
53+
} else {
54+
php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
55+
}
5356
}
54-
} else {
55-
XXH32_reset(&ctx->s, 0);
5657
}
58+
59+
XXH32_reset(&ctx->s, 0);
5760
}
5861

5962
PHP_HASH_API void PHP_XXH32Update(PHP_XXH32_CTX *ctx, const unsigned char *in, size_t len)
@@ -112,12 +115,13 @@ PHP_HASH_API void PHP_XXH64Init(PHP_XXH64_CTX *ctx, HashTable *args)
112115
once and for all, it should be done a clean way. */
113116
if (seed && IS_LONG == Z_TYPE_P(seed)) {
114117
XXH64_reset(&ctx->s, (XXH64_hash_t)Z_LVAL_P(seed));
118+
return;
115119
} else {
116-
XXH64_reset(&ctx->s, 0);
120+
php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0");
117121
}
118-
} else {
119-
XXH64_reset(&ctx->s, 0);
120122
}
123+
124+
XXH64_reset(&ctx->s, 0);
121125
}
122126

123127
PHP_HASH_API void PHP_XXH64Update(PHP_XXH64_CTX *ctx, const unsigned char *in, size_t len)
@@ -168,12 +172,19 @@ zend_always_inline static void _PHP_XXH3_Init(PHP_XXH3_64_CTX *ctx, HashTable *a
168172
return;
169173
}
170174

175+
if (_seed && IS_LONG != Z_TYPE_P(_seed)) {
176+
php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than int is deprecated because it is ignored");
177+
}
178+
171179
if (_seed && IS_LONG == Z_TYPE_P(_seed)) {
172180
/* This might be a bit too restrictive, but thinking that a seed might be set
173181
once and for all, it should be done a clean way. */
174182
func_init_seed(&ctx->s, (XXH64_hash_t)Z_LVAL_P(_seed));
175183
return;
176184
} else if (_secret) {
185+
if (IS_STRING != Z_TYPE_P(_secret)) {
186+
php_error_docref(NULL, E_DEPRECATED, "Passing a seed of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs");
187+
}
177188
zend_string *secret_string = zval_try_get_string(_secret);
178189
if (UNEXPECTED(!secret_string)) {
179190
ZEND_ASSERT(EG(exception));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Hash: murmur3 seed deprecation of edge cases
3+
--FILE--
4+
<?php
5+
6+
foreach (["murmur3a", "murmur3c", "murmur3f"] as $a) {
7+
hash_init($a, options: ["seed" => "42"]);
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Deprecated: hash_init(): Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0 in %s on line %d
13+
14+
Deprecated: hash_init(): Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0 in %s on line %d
15+
16+
Deprecated: hash_init(): Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0 in %s on line %d

ext/hash/tests/xxh3_convert_secret_to_string.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ try {
77
} catch (Throwable) {}
88
var_dump($x);
99
?>
10-
--EXPECT--
10+
--EXPECTF--
11+
Deprecated: hash_init(): Passing a seed of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs in %s on line %d
1112
array(1) {
1213
["secret"]=>
1314
int(4)

ext/hash/tests/xxhash_secret.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ foreach (["xxh3", "xxh128"] as $a) {
4646
}
4747

4848
?>
49-
--EXPECT--
49+
--EXPECTF--
5050
string(67) "xxh3: Only one of seed or secret is to be passed for initialization"
51+
52+
Deprecated: hash_init(): Passing a seed of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs in %s on line %d
5153
string(23) "exception in __toString"
5254
string(57) "xxh3: Secret length must be >= 136 bytes, 17 bytes passed"
5355
8028aa834c03557a == 8028aa834c03557a == true
5456
string(69) "xxh128: Only one of seed or secret is to be passed for initialization"
57+
58+
Deprecated: hash_init(): Passing a seed of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs in %s on line %d
5559
string(23) "exception in __toString"
5660
string(59) "xxh128: Secret length must be >= 136 bytes, 17 bytes passed"
5761
54279097795e7218093a05d4d781cbb9 == 54279097795e7218093a05d4d781cbb9 == true
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Hash: xxHash seed deprecation of edge cases
3+
--FILE--
4+
<?php
5+
6+
foreach (["xxh32", "xxh64", "xxh3", "xxh128"] as $a) {
7+
hash_init($a, options: ["seed" => "42"]);
8+
}
9+
10+
foreach (["xxh3", "xxh128"] as $a) {
11+
try {
12+
hash_init($a, options: ["secret" => 42]);
13+
} catch (Throwable) {}
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Deprecated: hash_init(): Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0 in %s on line %d
19+
20+
Deprecated: hash_init(): Passing a seed of a type other than int is deprecated because it is the same as setting the seed to 0 in %s on line %d
21+
22+
Deprecated: hash_init(): Passing a seed of a type other than int is deprecated because it is ignored in %s on line %d
23+
24+
Deprecated: hash_init(): Passing a seed of a type other than int is deprecated because it is ignored in %s on line %d
25+
26+
Deprecated: hash_init(): Passing a seed of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs in %s on line %d
27+
28+
Deprecated: hash_init(): Passing a seed of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs in %s on line %d

0 commit comments

Comments
 (0)