Fix memory leak

This commit is contained in:
Tom Willemse 2013-11-16 00:52:49 +01:00
parent f6a9356dc3
commit 9b4461479e

27
sha1.c
View file

@ -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;
}