Fixed memory leaks

This commit is contained in:
John Jones 2016-12-01 13:05:14 -05:00
parent a2daf88b26
commit 6cb4e3a04e
6 changed files with 40 additions and 5 deletions

View file

@ -636,7 +636,9 @@ base32_decode_alloc_ctx (struct base32_decode_context *ctx,
*/ */
int libp2p_crypto_encoding_base32_encode(const unsigned char* incoming, size_t incoming_length, int libp2p_crypto_encoding_base32_encode(const unsigned char* incoming, size_t incoming_length,
unsigned char* results, size_t* results_length) { unsigned char* results, size_t* results_length) {
memset(results, 0, *results_length);
base32_encode(incoming, incoming_length, results, *results_length); base32_encode(incoming, incoming_length, results, *results_length);
if (results[ (*results_length) -1] == 0)
*results_length = strlen(results); *results_length = strlen(results);
return 1; return 1;
} }
@ -649,7 +651,7 @@ int libp2p_crypto_encoding_base32_encode(const unsigned char* incoming, size_t i
size_t libp2p_crypto_encoding_base32_encode_size(size_t incoming_length) { size_t libp2p_crypto_encoding_base32_encode_size(size_t incoming_length) {
incoming_length -= 5; incoming_length -= 5;
incoming_length /= 5; incoming_length /= 5;
return incoming_length * 8; return incoming_length * 8 + 1;
} }
/** /**

View file

@ -7,6 +7,7 @@
// //
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "mbedtls/rsa.h" #include "mbedtls/rsa.h"
@ -92,7 +93,10 @@ int libp2p_crypto_encoding_x509_der_to_private_key(unsigned char* der, size_t de
} }
// now put the public DER format in. // now put the public DER format in.
private_key->der = der; private_key->der = malloc(sizeof(char) * der_length);
if (private_key->der == NULL)
return 0;
memcpy(private_key->der, der, der_length);
private_key->der_length = der_length; private_key->der_length = der_length;
//NOTE: the public DER stuff is done in rsa.c //NOTE: the public DER stuff is done in rsa.c

View file

@ -204,18 +204,20 @@ int libp2p_crypto_rsa_private_key_fill_public_key(struct RsaPrivateKey* private_
// generate public key der // generate public key der
int retVal = libp2p_crypto_rsa_write_public_key_der(&ctx, buffer, &buffer_size); int retVal = libp2p_crypto_rsa_write_public_key_der(&ctx, buffer, &buffer_size);
if (retVal == 0) {
mbedtls_pk_free(&ctx); mbedtls_pk_free(&ctx);
if (retVal == 0) {
return 0; return 0;
} }
// allocate memory for the public key der // allocate memory for the public key der
private_key->public_key_length = buffer_size; private_key->public_key_length = buffer_size;
private_key->public_key_der = malloc(sizeof(char) * buffer_size); private_key->public_key_der = malloc(sizeof(char) * buffer_size);
if (private_key->public_key_der == NULL) {
return 0;
}
//copy it into the struct //copy it into the struct
memcpy(private_key->public_key_der, &buffer[1600-buffer_size], buffer_size); memcpy(private_key->public_key_der, &buffer[1600-buffer_size], buffer_size);
mbedtls_pk_free(&ctx);
return 1; return 1;
} }

View file

@ -24,3 +24,5 @@ all: all_others testit_libp2p
clean: clean:
rm -f *.o rm -f *.o
rm -f testit_libp2p rm -f testit_libp2p
test: clean testit_libp2p

21
test/crypto/test_base32.h Normal file
View file

@ -0,0 +1,21 @@
#include "libp2p/crypto/encoding/base32.h"
int test_crypto_encoding_base32_encode() {
size_t input_size = 1000;
unsigned char input[input_size];
int minus = 0;
for(int i = 0; i < input_size; i++) {
if (input_size > 0 && input_size % 255 == 0) {
minus += 255;
}
input[i] = input_size - minus;
}
size_t results_size = libp2p_crypto_encoding_base32_encode_size(input_size);
unsigned char results[results_size];
int retVal = libp2p_crypto_encoding_base32_encode(input, input_size, &results[0], &results_size);
if (retVal == 0)
return 0;
return 1;
}

View file

@ -4,6 +4,7 @@
#include "crypto/test_rsa.h" #include "crypto/test_rsa.h"
#include "crypto/test_base58.h" #include "crypto/test_base58.h"
#include "crypto/test_base32.h"
#include "test_mbedtls.h" #include "test_mbedtls.h"
int testit(const char* name, int (*func)(void)) { int testit(const char* name, int (*func)(void)) {
@ -17,6 +18,7 @@ int testit(const char* name, int (*func)(void)) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
/*
testit("test_public_der_to_private_der", test_public_der_to_private_der); testit("test_public_der_to_private_der", test_public_der_to_private_der);
testit("test_mbedtls_varint_128_binary", test_mbedtls_varint_128_binary); testit("test_mbedtls_varint_128_binary", test_mbedtls_varint_128_binary);
testit("test_mbedtls_varint_128_string", test_mbedtls_varint_128_string); testit("test_mbedtls_varint_128_string", test_mbedtls_varint_128_string);
@ -34,6 +36,8 @@ int main(int argc, char** argv) {
testit("test_base58_max_size", test_base58_max_size); testit("test_base58_max_size", test_base58_max_size);
testit("test_base58_peer_address", test_base58_peer_address); testit("test_base58_peer_address", test_base58_peer_address);
//testit("test_mbedtls_pk_write_key_der", test_mbedtls_pk_write_key_der); //testit("test_mbedtls_pk_write_key_der", test_mbedtls_pk_write_key_der);
*/
testit("test_crypto_encoding_base32_encode", test_crypto_encoding_base32_encode);
return 1; return 1;
} }