From 8abc686f0e97c6739e0ce6d6bcb1a1ce028af1bd Mon Sep 17 00:00:00 2001 From: doublex Date: Wed, 18 Apr 2018 19:58:35 +0200 Subject: [PATCH] fix memory leak when using mbedtls backend (#158) _libssh2_bn_init_from_bin/_libssh2_bn_free would leak bignum from mbedtls_calloc(). --- src/mbedtls.c | 9 +++++++++ src/mbedtls.h | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/mbedtls.c b/src/mbedtls.c index e34282d9..585740b6 100644 --- a/src/mbedtls.c +++ b/src/mbedtls.c @@ -247,6 +247,15 @@ _libssh2_mbedtls_bignum_init(void) return bignum; } +void +_libssh2_mbedtls_bignum_free(_libssh2_bn *bn) +{ + if (bn) { + mbedtls_mpi_free(bn); + mbedtls_free(bn); + } +} + static int _libssh2_mbedtls_bignum_random(_libssh2_bn *bn, int bits, int top, int bottom) { diff --git a/src/mbedtls.h b/src/mbedtls.h index 6b98f521..d8d55807 100644 --- a/src/mbedtls.h +++ b/src/mbedtls.h @@ -307,7 +307,7 @@ #define _libssh2_bn_bits(bn) \ mbedtls_mpi_bitlen(bn) #define _libssh2_bn_free(bn) \ - mbedtls_mpi_free(bn) + _libssh2_mbedtls_bignum_free(bn) /*******************************************************************/