From ba56f5f611f0a62d406bf97c738c82dca2e53635 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 14 May 2012 20:01:00 +0000 Subject: [PATCH] fixed memory leaks git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5167 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/pgp/pgphandler.cc | 14 +++++++------- openpgpsdk/include/openpgpsdk/configure.h | 8 ++++++++ openpgpsdk/src/create.c | 15 +++++---------- openpgpsdk/src/keyring.c | 6 ++---- openpgpsdk/src/readerwriter.c | 12 ++---------- openpgpsdk/src/signature.c | 6 ++---- openpgpsdk/src/validate.c | 15 +++++++-------- 7 files changed, 33 insertions(+), 43 deletions(-) diff --git a/libretroshare/src/pgp/pgphandler.cc b/libretroshare/src/pgp/pgphandler.cc index 213833087..391e71547 100644 --- a/libretroshare/src/pgp/pgphandler.cc +++ b/libretroshare/src/pgp/pgphandler.cc @@ -23,12 +23,12 @@ std::string PGPIdType::toStdString() const { static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ; - std::string res ; + std::string res(KEY_ID_SIZE*2,' ') ; for(int j = 0; j < KEY_ID_SIZE; j++) { - res += out[ (bytes[j]>>4) ] ; - res += out[ bytes[j] & 0xf ] ; + res[2*j ] = out[ (bytes[j]>>4) ] ; + res[2*j+1] = out[ bytes[j] & 0xf ] ; } return res ; @@ -37,12 +37,12 @@ std::string PGPFingerprintType::toStdString() const { static const char out[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' } ; - std::string res ; + std::string res(KEY_FINGERPRINT_SIZE*2,' ') ; for(int j = 0; j < KEY_FINGERPRINT_SIZE; j++) { - res += out[ (bytes[j]>>4) ] ; - res += out[ bytes[j] & 0xf ] ; + res[2*j ] = out[ (bytes[j]>>4) ] ; + res[2*j+1] = out[ bytes[j] & 0xf ] ; } return res ; @@ -449,7 +449,7 @@ bool PGPHandler::GeneratePGPCertificate(const std::string& name, const std::stri // 7 - validate own signature and update certificate. - validateAndUpdateSignatures(_public_keyring_map[ pgpId.toStdString() ],getPublicKey(pgpId)) ; +// validateAndUpdateSignatures(_public_keyring_map[ pgpId.toStdString() ],getPublicKey(pgpId)) ; return true ; } diff --git a/openpgpsdk/include/openpgpsdk/configure.h b/openpgpsdk/include/openpgpsdk/configure.h index 38240da1a..63051b316 100644 --- a/openpgpsdk/include/openpgpsdk/configure.h +++ b/openpgpsdk/include/openpgpsdk/configure.h @@ -5,3 +5,11 @@ /* for silencing unused parameter warnings */ #define OPS_USED(x) (x)=(x) + +/* for tests, flag to tell gpg not to use blocking randomness */ +#define GNUPG_QUICK_RANDOM "--quick-random" + +/* Avoid a bunch of #ifs */ +#ifndef O_BINARY +# define O_BINARY 0 +#endif diff --git a/openpgpsdk/src/create.c b/openpgpsdk/src/create.c index 7341b6124..320d527fa 100644 --- a/openpgpsdk/src/create.c +++ b/openpgpsdk/src/create.c @@ -1124,11 +1124,9 @@ ops_write_literal_data_from_file(const char *filename, unsigned char buf[1024]; ops_memory_t* mem=NULL; size_t len=0; -#ifdef WINDOWS_SYS + fd=open(filename,O_RDONLY | O_BINARY); -#else - fd=open(filename,O_RDONLY); -#endif + if (fd < 0) return ops_false; @@ -1180,11 +1178,9 @@ ops_memory_t* ops_write_mem_from_file(const char *filename, int* errnum) ops_memory_t* mem=NULL; *errnum=0; -#ifdef WINDOWS_SYS + fd=open(filename,O_RDONLY | O_BINARY); -#else - fd=open(filename,O_RDONLY); -#endif + if (fd < 0) { *errnum=errno; @@ -1234,9 +1230,8 @@ int ops_write_file_from_buf(const char *filename, const char* buf, flags |= O_TRUNC; else flags |= O_EXCL; -#ifdef WINDOWS_SYS + flags |= O_BINARY; -#endif fd=open(filename,flags, 0600); if (fd < 0) diff --git a/openpgpsdk/src/keyring.c b/openpgpsdk/src/keyring.c index b4d2141cb..f3419bf7b 100644 --- a/openpgpsdk/src/keyring.c +++ b/openpgpsdk/src/keyring.c @@ -691,11 +691,9 @@ ops_boolean_t ops_keyring_read_from_file(ops_keyring_t *keyring, const ops_boole // ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_RAW); ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); -#ifdef WINDOWS_SYS + fd=open(filename,O_RDONLY | O_BINARY); -#else - fd=open(filename,O_RDONLY); -#endif + if(fd < 0) { ops_parse_info_delete(pinfo); diff --git a/openpgpsdk/src/readerwriter.c b/openpgpsdk/src/readerwriter.c index e47f458a6..16291b7d2 100644 --- a/openpgpsdk/src/readerwriter.c +++ b/openpgpsdk/src/readerwriter.c @@ -139,9 +139,7 @@ int ops_setup_file_write(ops_create_info_t **cinfo, const char* filename, ops_bo flags |= O_TRUNC; else flags |= O_EXCL; -#ifdef WINDOWS_SYS flags |= O_BINARY; -#endif fd=open(filename, flags, 0600); if(fd < 0) @@ -181,11 +179,8 @@ int ops_setup_file_append(ops_create_info_t **cinfo, const char* filename) * initialise needed structures for writing to file */ -#ifdef WINDOWS_SYS fd=open(filename,O_WRONLY | O_APPEND | O_BINARY, 0600); -#else - fd=open(filename,O_WRONLY | O_APPEND , 0600); -#endif + if(fd < 0) { perror(filename); @@ -230,11 +225,8 @@ int ops_setup_file_read(ops_parse_info_t **pinfo, const char *filename, * initialise needed structures for reading */ -#ifdef WINDOWS_SYS fd=open(filename,O_RDONLY | O_BINARY); -#else - fd=open(filename,O_RDONLY ); -#endif + if (fd < 0) { perror(filename); diff --git a/openpgpsdk/src/signature.c b/openpgpsdk/src/signature.c index d257e1bf4..2ca463074 100644 --- a/openpgpsdk/src/signature.c +++ b/openpgpsdk/src/signature.c @@ -958,11 +958,9 @@ ops_boolean_t ops_sign_file_as_cleartext(const char* input_filename, ops_boolean_t use_armour=ops_true; // open file to sign -#ifdef WINDOWS_SYS + fd_in=open(input_filename, O_RDONLY | O_BINARY); -#else - fd_in=open(input_filename, O_RDONLY ); -#endif + if(fd_in < 0) { return ops_false; diff --git a/openpgpsdk/src/validate.c b/openpgpsdk/src/validate.c index 450cec83c..147aaa46d 100644 --- a/openpgpsdk/src/validate.c +++ b/openpgpsdk/src/validate.c @@ -116,9 +116,11 @@ static int keydata_reader(void *dest,size_t length,ops_error_t **errors, return length; } -static void free_signature_info(ops_signature_info_t *sig) +static void free_signature_info(ops_signature_info_t *sig,int n) { - free (sig->v4_hashed_data); + int i ; + for(i=0;ivalid_sigs=realloc(result->valid_sigs, newsize); // copy key ptr to array - start=(sizeof *sig) * (result->valid_count-1); copy_signature_info(&result->valid_sigs[result->valid_count-1],sig); } @@ -165,7 +166,6 @@ static void add_sig_to_invalid_list(ops_validate_result_t * result, const ops_si result->invalid_sigs=realloc(result->invalid_sigs, newsize); // copy key ptr to array - start=(sizeof *sig) * (result->invalid_count-1); copy_signature_info(&result->invalid_sigs[result->invalid_count-1],sig); } @@ -185,7 +185,6 @@ static void add_sig_to_unknown_list(ops_validate_result_t * result, const ops_si result->unknown_sigs=realloc(result->unknown_sigs, newsize); // copy key id to array - start=OPS_KEY_ID_SIZE * (result->unknown_signer_count-1); copy_signature_info(&result->unknown_sigs[result->unknown_signer_count-1],sig); } @@ -607,11 +606,11 @@ void ops_validate_result_free(ops_validate_result_t *result) return; if (result->valid_sigs) - free_signature_info(result->valid_sigs); + free_signature_info(result->valid_sigs,result->valid_count); if (result->invalid_sigs) - free_signature_info(result->invalid_sigs); + free_signature_info(result->invalid_sigs,result->invalid_count); if (result->unknown_sigs) - free_signature_info(result->unknown_sigs); + free_signature_info(result->unknown_sigs,result->unknown_signer_count); free(result); result=NULL;