Browse Source

[Crypto] Delete mbedtls ctx in deconstructor.

Would cause memory leak when the context was `start`ed but not
`finish`ed.
Fabio Alessandrelli 4 years ago
parent
commit
a28d25c441
3 changed files with 9 additions and 0 deletions
  1. 1 0
      core/crypto/crypto.h
  2. 7 0
      modules/mbedtls/crypto_mbedtls.cpp
  3. 1 0
      modules/mbedtls/crypto_mbedtls.h

+ 1 - 0
core/crypto/crypto.h

@@ -82,6 +82,7 @@ public:
 	virtual PackedByteArray finish() = 0;
 
 	HMACContext() {}
+	virtual ~HMACContext() {}
 };
 
 class Crypto : public RefCounted {

+ 7 - 0
modules/mbedtls/crypto_mbedtls.cpp

@@ -249,6 +249,13 @@ PackedByteArray HMACContextMbedTLS::finish() {
 	return out;
 }
 
+HMACContextMbedTLS::~HMACContextMbedTLS() {
+	if (ctx != nullptr) {
+		mbedtls_md_free((mbedtls_md_context_t *)ctx);
+		memfree((mbedtls_md_context_t *)ctx);
+	}
+}
+
 Crypto *CryptoMbedTLS::create() {
 	return memnew(CryptoMbedTLS);
 }

+ 1 - 0
modules/mbedtls/crypto_mbedtls.h

@@ -119,6 +119,7 @@ public:
 	virtual PackedByteArray finish();
 
 	HMACContextMbedTLS() {}
+	~HMACContextMbedTLS();
 };
 
 class CryptoMbedTLS : public Crypto {