From 3cf30f1e96c6267bf699240db1bf926f33cbcbb2 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 1 Aug 2019 12:36:08 -0700 Subject: [PATCH] Fix to allow proper calling with counts for `wolfCrypt_Init` and `wolfCrypt_Cleanup`. Old code was not tracking case such as `wolfCrypt_Init` -> `wolfCrypt_Init` -> `wolfCrypt_Cleanup` and was causing cleanup to be performed, when it shouldn't. --- wolfcrypt/src/wc_port.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 7f9b5688c..2dc70afbf 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -206,9 +206,8 @@ int wolfCrypt_Init(void) return ret; } #endif - - initRefCount = 1; } + initRefCount++; return ret; } @@ -219,7 +218,11 @@ int wolfCrypt_Cleanup(void) { int ret = 0; - if (initRefCount == 1) { + initRefCount--; + if (initRefCount < 0) + initRefCount = 0; + + if (initRefCount == 0) { WOLFSSL_ENTER("wolfCrypt_Cleanup"); #ifdef HAVE_ECC @@ -250,7 +253,6 @@ int wolfCrypt_Cleanup(void) #if defined(WOLFSSL_CRYPTOCELL) cc310_Free(); #endif - initRefCount = 0; /* allow re-init */ } return ret;