Skip to content

Commit 579fd5e

Browse files
committed
hash: Address PR comments
- Deref passed seed arg - Don't dup args array Signed-off-by: Anatol Belski <ab@php.net>
1 parent b880ff6 commit 579fd5e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

ext/hash/hash.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,12 @@ PHP_FUNCTION(hash_init)
643643
hash->context = context;
644644
hash->options = options;
645645
hash->key = NULL;
646-
hash->args = args && zend_hash_num_elements(args) > 0 ? zend_array_dup(args) : NULL;
646+
if (args && zend_hash_num_elements(args)) {
647+
hash->args = args;
648+
GC_TRY_ADDREF(hash->args);
649+
} else {
650+
hash->args = NULL;
651+
}
647652

648653
if (options & PHP_HASH_HMAC) {
649654
char *K = emalloc(ops->block_size);
@@ -1389,10 +1394,7 @@ static void php_hashcontext_dtor(zend_object *obj) {
13891394
}
13901395

13911396
if (hash->args) {
1392-
if (GC_REFCOUNT(hash->args) > 1) {
1393-
GC_DELREF(hash->args);
1394-
}
1395-
zend_array_destroy(hash->args);
1397+
zend_array_release(hash->args);
13961398
hash->args = NULL;
13971399
}
13981400
}
@@ -1487,7 +1489,7 @@ PHP_METHOD(HashContext, __serialize)
14871489

14881490
if (hash->args) {
14891491
ZVAL_ARR(&tmp, hash->args);
1490-
Z_ADDREF(tmp);
1492+
Z_TRY_ADDREF(tmp);
14911493
} else {
14921494
ZVAL_NULL(&tmp);
14931495
}
@@ -1559,7 +1561,7 @@ PHP_METHOD(HashContext, __unserialize)
15591561
if (args_zv && IS_ARRAY == Z_TYPE_P(args_zv) && zend_hash_num_elements(Z_ARRVAL_P(args_zv)) > 0) {
15601562
ops->hash_init(hash->context, Z_ARRVAL_P(args_zv));
15611563
hash->args = Z_ARRVAL_P(args_zv);
1562-
GC_ADDREF(hash->args);
1564+
GC_TRY_ADDREF(hash->args);
15631565
} else {
15641566
ops->hash_init(hash->context, NULL);
15651567
hash->args = NULL;

ext/hash/hash_murmur.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ const php_hash_ops php_hash_murmur3a_ops = {
3939
PHP_HASH_API void PHP_MURMUR3AInit(PHP_MURMUR3A_CTX *ctx, HashTable *args)
4040
{
4141
if (args) {
42-
zval *seed;
42+
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 ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) {
45+
if (seed && IS_LONG == Z_TYPE_P(seed)) {
4646
ctx->h = (uint32_t)Z_LVAL_P(seed);
4747
} else {
4848
ctx->h = 0;
@@ -96,10 +96,10 @@ const php_hash_ops php_hash_murmur3c_ops = {
9696
PHP_HASH_API void PHP_MURMUR3CInit(PHP_MURMUR3C_CTX *ctx, HashTable *args)
9797
{
9898
if (args) {
99-
zval *seed;
99+
zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
100100
/* This might be a bit too restrictive, but thinking that a seed might be set
101101
once and for all, it should be done a clean way. */
102-
if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) {
102+
if (seed && IS_LONG == Z_TYPE_P(seed)) {
103103
uint32_t _seed = (uint32_t)Z_LVAL_P(seed);
104104
ctx->h[0] = _seed;
105105
ctx->h[1] = _seed;
@@ -170,10 +170,10 @@ const php_hash_ops php_hash_murmur3f_ops = {
170170
PHP_HASH_API void PHP_MURMUR3FInit(PHP_MURMUR3F_CTX *ctx, HashTable *args)
171171
{
172172
if (args) {
173-
zval *seed;
173+
zval *seed = zend_hash_str_find_deref(args, "seed", sizeof("seed") - 1);
174174
/* This might be a bit too restrictive, but thinking that a seed might be set
175175
once and for all, it should be done a clean way. */
176-
if ((NULL != (seed = zend_hash_str_find(args, "seed", sizeof("seed") - 1))) && IS_LONG == Z_TYPE_P(seed)) {
176+
if (seed && IS_LONG == Z_TYPE_P(seed)) {
177177
uint64_t _seed = (uint64_t)Z_LVAL_P(seed);
178178
ctx->h[0] = _seed;
179179
ctx->h[1] = _seed;

0 commit comments

Comments
 (0)