diff options
author | William Ahern <william@25thandClement.com> | 2015-12-17 16:34:56 +0800 |
---|---|---|
committer | William Ahern <william@25thandClement.com> | 2015-12-17 16:34:56 +0800 |
commit | 946d173bfed7d45c1413f51c890693a668615c88 (patch) | |
tree | 5ae9ae999dae511c96785d6c8effeadc5adfdc3e | |
parent | b18972627b8501f170d389b317f6339b98eb0b79 (diff) | |
download | luaossl-946d173bfed7d45c1413f51c890693a668615c88.tar.gz luaossl-946d173bfed7d45c1413f51c890693a668615c88.tar.bz2 luaossl-946d173bfed7d45c1413f51c890693a668615c88.zip |
free result of BN_bn2dec()
fixes issue #38
-rw-r--r-- | src/openssl.c | 24 |
1 files changed, 21 insertions, 3 deletions
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() */ |