From 9b4461479e0f038391367e5afca1735b7e11c788 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Sat, 16 Nov 2013 00:52:49 +0100 Subject: Fix memory leak --- sha1.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/sha1.c b/sha1.c index 2a8fda1..7e988b2 100644 --- a/sha1.c +++ b/sha1.c @@ -12,6 +12,7 @@ base64encode(const void *data, int len) { /* Copied from http://stackoverflow.com/a/16511093/459915 */ BIO *b64_bio, *mem_bio; BUF_MEM *mem_bio_mem_ptr; + char *ret; b64_bio = BIO_new(BIO_f_base64()); mem_bio = BIO_new(BIO_s_mem()); @@ -26,8 +27,11 @@ base64encode(const void *data, int len) BIO_free_all(b64_bio); (*mem_bio_mem_ptr).data[(*mem_bio_mem_ptr).length] = '\0'; + ret = calloc((*mem_bio_mem_ptr).length, sizeof(char)); + strncpy(ret, (*mem_bio_mem_ptr).data, (*mem_bio_mem_ptr).length); + BUF_MEM_free(mem_bio_mem_ptr); - return (*mem_bio_mem_ptr).data; + return ret; } void @@ -89,9 +93,22 @@ dispass2(char *label, char *password, int len, int seqno) int main(int argc, char *argv[]) { - printf("%s\n", dispass1("test", "qqqqqqqq", 30, 0)); - printf("%s\n", dispass1("test2", "qqqqqqqq", 50, 0)); - printf("%s\n", dispass2("test", "qqqqqqqq", 30, 1)); - printf("%s\n", dispass2("test2", "qqqqqqqq", 50, 10)); + char *test1, *test2, *test3, *test4; + + test1 = dispass1("test", "qqqqqqqq", 30, 0); + test2 = dispass1("test2", "qqqqqqqq", 50, 0); + test3 = dispass2("test", "qqqqqqqq", 30, 1); + test4 = dispass2("test2", "qqqqqqqq", 50, 10); + + printf("%s\n", test1); + printf("%s\n", test2); + printf("%s\n", test3); + printf("%s\n", test4); + + free(test1); + free(test2); + free(test3); + free(test4); + return 0; } -- cgit v1.2.3-54-g00ecf