Skip to content

Commit cbf642b

Browse files
committed
Create object only after parameter checks
1 parent 22c9d19 commit cbf642b

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

ext/hash/hash.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -329,40 +329,42 @@ PHP_FUNCTION(hash_hmac_file)
329329
}
330330
/* }}} */
331331

332-
static void php_hashcontext_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *objval) {
332+
/* {{{ proto HashContext hash_init(string algo[, int options, string key])
333+
Initialize a hashing context */
334+
PHP_FUNCTION(hash_init)
335+
{
333336
zend_string *algo, *key = NULL;
334337
zend_long options = 0;
335338
int argc = ZEND_NUM_ARGS();
336339
void *context;
337340
const php_hash_ops *ops;
338-
php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(objval));
341+
php_hashcontext_object *hash;
339342

340343
if (zend_parse_parameters(argc, "S|lS", &algo, &options, &key) == FAILURE) {
341-
zval_ptr_dtor(return_value);
342344
RETURN_NULL();
343345
}
344346

345347
ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
346348
if (!ops) {
347349
php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
348-
zval_ptr_dtor(return_value);
349350
RETURN_FALSE;
350351
}
351352

352353
if (options & PHP_HASH_HMAC) {
353354
if (!ops->is_crypto) {
354355
php_error_docref(NULL, E_WARNING, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
355-
zval_ptr_dtor(return_value);
356356
RETURN_FALSE;
357357
}
358358
if (!key || (ZSTR_LEN(key) == 0)) {
359359
/* Note: a zero length key is no key at all */
360360
php_error_docref(NULL, E_WARNING, "HMAC requested without a key");
361-
zval_ptr_dtor(return_value);
362361
RETURN_FALSE;
363362
}
364363
}
365364

365+
object_init_ex(return_value, php_hashcontext_ce);
366+
hash = php_hashcontext_from_object(Z_OBJ_P(return_value));
367+
366368
context = emalloc(ops->context_size);
367369
ops->hash_init(context);
368370

@@ -396,14 +398,6 @@ static void php_hashcontext_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *objval) {
396398
hash->key = (unsigned char *) K;
397399
}
398400
}
399-
400-
/* {{{ proto HashContext hash_init(string algo[, int options, string key])
401-
Initialize a hashing context */
402-
PHP_FUNCTION(hash_init)
403-
{
404-
object_init_ex(return_value, php_hashcontext_ce);
405-
php_hashcontext_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, return_value);
406-
}
407401
/* }}} */
408402

409403
#define PHP_HASHCONTEXT_VERIFY(func, hash) { \

0 commit comments

Comments
 (0)