From a11cae131adf37a2b6887620bc6cf10af60399ae Mon Sep 17 00:00:00 2001 From: jsing Date: Sat, 7 Jan 2023 16:17:29 +0000 Subject: [PATCH] Rewrite/simplify BN_free(). ok tb@ --- lib/libcrypto/bn/bn_lib.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/libcrypto/bn/bn_lib.c b/lib/libcrypto/bn/bn_lib.c index 70b6899a427..e8802300ce7 100644 --- a/lib/libcrypto/bn/bn_lib.c +++ b/lib/libcrypto/bn/bn_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_lib.c,v 1.70 2023/01/07 16:13:46 jsing Exp $ */ +/* $OpenBSD: bn_lib.c,v 1.71 2023/01/07 16:17:29 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -97,18 +97,20 @@ BN_clear(BIGNUM *a) } void -BN_free(BIGNUM *a) +BN_free(BIGNUM *bn) { - int i; + if (bn == NULL) + return; - if (a == NULL) + if (!BN_get_flags(bn, BN_FLG_STATIC_DATA)) + freezero(bn->d, bn->dmax * sizeof(bn->d[0])); + + if (!BN_get_flags(bn, BN_FLG_MALLOCED)) { + explicit_bzero(bn, sizeof(*bn)); return; - if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) - freezero(a->d, a->dmax * sizeof(a->d[0])); - i = BN_get_flags(a, BN_FLG_MALLOCED); - explicit_bzero(a, sizeof(BIGNUM)); - if (i) - free(a); + } + + freezero(bn, sizeof(*bn)); } void -- 2.20.1