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) { | |||
1898 | } /* bn__gc() */ | 1898 | } /* bn__gc() */ |
1899 | 1899 | ||
1900 | 1900 | ||
1901 | static BIO *getbio(lua_State *); | ||
1902 | |||
1901 | static int bn__tostring(lua_State *L) { | 1903 | static int bn__tostring(lua_State *L) { |
1902 | BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); | 1904 | BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); |
1903 | char *txt; | 1905 | char *txt = NULL; |
1906 | BIO *bio; | ||
1907 | BUF_MEM *buf; | ||
1904 | 1908 | ||
1905 | if (!(txt = BN_bn2dec(bn))) | 1909 | if (!(txt = BN_bn2dec(bn))) |
1906 | return auxL_error(L, auxL_EOPENSSL, "bignum:__tostring"); | 1910 | goto sslerr; |
1907 | 1911 | ||
1908 | lua_pushstring(L, txt); | 1912 | /* use GC-visible BIO as temporary buffer */ |
1913 | bio = getbio(L); | ||
1914 | |||
1915 | if (BIO_puts(bio, txt) < 0) | ||
1916 | goto sslerr; | ||
1917 | |||
1918 | OPENSSL_free(txt); | ||
1919 | txt = NULL; | ||
1920 | |||
1921 | BIO_get_mem_ptr(bio, &buf); | ||
1922 | lua_pushlstring(L, buf->data, buf->length); | ||
1909 | 1923 | ||
1910 | return 1; | 1924 | return 1; |
1925 | sslerr: | ||
1926 | OPENSSL_free(txt); | ||
1927 | |||
1928 | return auxL_error(L, auxL_EOPENSSL, "bignum:__tostring"); | ||
1911 | } /* bn__tostring() */ | 1929 | } /* bn__tostring() */ |
1912 | 1930 | ||
1913 | 1931 | ||