From 946d173bfed7d45c1413f51c890693a668615c88 Mon Sep 17 00:00:00 2001 From: William Ahern Date: Thu, 17 Dec 2015 16:34:56 +0800 Subject: free result of BN_bn2dec() fixes issue #38 --- src/openssl.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index a5e1a52..8d1435a 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1898,16 +1898,34 @@ static int bn__gc(lua_State *L) { } /* bn__gc() */ +static BIO *getbio(lua_State *); + static int bn__tostring(lua_State *L) { BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); - char *txt; + char *txt = NULL; + BIO *bio; + BUF_MEM *buf; if (!(txt = BN_bn2dec(bn))) - return auxL_error(L, auxL_EOPENSSL, "bignum:__tostring"); + goto sslerr; - lua_pushstring(L, txt); + /* use GC-visible BIO as temporary buffer */ + bio = getbio(L); + + if (BIO_puts(bio, txt) < 0) + goto sslerr; + + OPENSSL_free(txt); + txt = NULL; + + BIO_get_mem_ptr(bio, &buf); + lua_pushlstring(L, buf->data, buf->length); return 1; +sslerr: + OPENSSL_free(txt); + + return auxL_error(L, auxL_EOPENSSL, "bignum:__tostring"); } /* bn__tostring() */ -- cgit v1.2.3-59-g8ed1b