diff --git a/libretroshare/src/dbase/fimonitor.cc b/libretroshare/src/dbase/fimonitor.cc index 91a8ffb9b..69ae9b109 100644 --- a/libretroshare/src/dbase/fimonitor.cc +++ b/libretroshare/src/dbase/fimonitor.cc @@ -29,6 +29,7 @@ #include "rsserver/p3face.h" #include "dbase/fimonitor.h" #include "util/rsdir.h" +#include "util/rsmemory.h" #include "pqi/authssl.h" #include "serialiser/rsserviceids.h" #include "retroshare/rsiface.h" @@ -128,13 +129,11 @@ HashCache::HashCache(const std::string& path) // read the binary stream into memory. // - void *buffer = malloc(file_size) ; + void *buffer = rs_safe_malloc(file_size) ; if(buffer == NULL) - { - std::cerr << "Cannot allocate memory for reading encrypted file cache, bytes=" << file_size << std::endl; return ; - } + FILE *F = fopen( (_path+".bin").c_str(),"rb") ; if (!F) { diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 3593977c8..259ec8889 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -39,6 +39,7 @@ #include "util/rsstring.h" #endif #include "util/rsdiscspace.h" +#include "util/rsmemory.h" #include "ft/ftcontroller.h" @@ -747,11 +748,10 @@ bool ftController::copyFile(const std::string& source,const std::string& dest) size_t T=0; static const int BUFF_SIZE = 10485760 ; // 10 MB buffer to speed things up. - void *buffer = malloc(BUFF_SIZE) ; + void *buffer = rs_safe_malloc(BUFF_SIZE) ; if(buffer == NULL) { - std::cerr << "(EE) Error while allocating memory for " << BUFF_SIZE << " bytes in " << __PRETTY_FUNCTION__ << std::endl; fclose (in); fclose (out); return false ; diff --git a/libretroshare/src/ft/ftdatamultiplex.cc b/libretroshare/src/ft/ftdatamultiplex.cc index 05c8265ef..1574f9519 100644 --- a/libretroshare/src/ft/ftdatamultiplex.cc +++ b/libretroshare/src/ft/ftdatamultiplex.cc @@ -35,6 +35,7 @@ #include "ft/ftfileprovider.h" #include "ft/ftsearch.h" #include "util/rsdir.h" +#include "util/rsmemory.h" #include #include @@ -878,13 +879,11 @@ bool ftDataMultiplex::locked_handleServerRequest(ftFileProvider *provider, const std::cerr << "Warning: peer " << peerId << " is asking a large chunk (s=" << chunksize << ") for hash " << hash << ", filesize=" << size << ". This is unexpected." << std::endl ; return false ; } - void *data = malloc(chunksize); + void *data = rs_safe_malloc(chunksize); if(data == NULL) - { - std::cerr << "WARNING: Could not allocate data for a chunksize of " << chunksize << std::endl ; return false ; - } + #ifdef MPLEX_DEBUG std::cerr << "ftDataMultiplex::locked_handleServerRequest()"; std::cerr << "\t peer: " << peerId << " hash: " << hash; diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 7ea0a57c0..5cfae3909 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -483,6 +483,8 @@ RsTurtleGenericTunnelItem *ftServer::deserialiseItem(void *data,uint32_t size) c catch(std::exception& e) { std::cerr << "(EE) deserialisation error in " << __PRETTY_FUNCTION__ << ": " << e.what() << std::endl; + + return NULL ; } } @@ -1100,11 +1102,10 @@ bool ftServer::sendData(const RsPeerId& peerId, const RsFileHash& hash, uint64_t item->chunk_offset = offset+baseoffset ; item->chunk_size = chunk; - item->chunk_data = malloc(chunk) ; + item->chunk_data = rs_safe_malloc(chunk) ; if(item->chunk_data == NULL) { - std::cerr << "p3turtle: Warning: failed malloc of " << chunk << " bytes for sending data packet." << std::endl ; delete item; return false; } diff --git a/libretroshare/src/ft/ftturtlefiletransferitem.cc b/libretroshare/src/ft/ftturtlefiletransferitem.cc index 65cfede70..463058798 100644 --- a/libretroshare/src/ft/ftturtlefiletransferitem.cc +++ b/libretroshare/src/ft/ftturtlefiletransferitem.cc @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -435,7 +436,7 @@ RsTurtleFileDataItem::RsTurtleFileDataItem(void *data,uint32_t pktsize) if(chunk_size > rssize || rssize - chunk_size < offset) throw std::runtime_error("RsTurtleFileDataItem::() error while deserializing.") ; - chunk_data = (void*)malloc(chunk_size) ; + chunk_data = (void*)rs_safe_malloc(chunk_size) ; if(chunk_data == NULL) throw std::runtime_error("RsTurtleFileDataItem::() cannot allocate memory.") ; diff --git a/libretroshare/src/grouter/grouteritems.cc b/libretroshare/src/grouter/grouteritems.cc index cb9c93083..27120167a 100644 --- a/libretroshare/src/grouter/grouteritems.cc +++ b/libretroshare/src/grouter/grouteritems.cc @@ -86,9 +86,8 @@ RsGRouterTransactionChunkItem *RsGRouterSerialiser::deserialise_RsGRouterTransac delete item; return NULL ; } - if( NULL == (item->chunk_data = (uint8_t*)malloc(item->chunk_size))) + if( NULL == (item->chunk_data = (uint8_t*)rs_safe_malloc(item->chunk_size))) { - std::cerr << __PRETTY_FUNCTION__ << ": Cannot allocate memory for chunk " << item->chunk_size << std::endl; delete item; return NULL ; } @@ -150,9 +149,8 @@ RsGRouterGenericDataItem *RsGRouterSerialiser::deserialise_RsGRouterGenericDataI return NULL ; } - if( NULL == (item->data_bytes = (uint8_t*)malloc(item->data_size))) + if( NULL == (item->data_bytes = (uint8_t*)rs_safe_malloc(item->data_size))) { - std::cerr << __PRETTY_FUNCTION__ << ": Cannot allocate memory for chunk " << item->data_size << std::endl; delete item; return NULL ; } @@ -348,13 +346,10 @@ RsGRouterGenericDataItem *RsGRouterGenericDataItem::duplicate() const // then duplicate the memory chunk - item->data_bytes = (uint8_t*)malloc(data_size) ; + item->data_bytes = (uint8_t*)rs_safe_malloc(data_size) ; if(item->data_bytes == NULL) - { - std::cerr << "(EE) memory allocation error for " << data_size << " bytes in " << __PRETTY_FUNCTION__ << std::endl; return NULL ; - } memcpy(item->data_bytes,data_bytes,data_size) ; diff --git a/libretroshare/src/grouter/grouteritems.h b/libretroshare/src/grouter/grouteritems.h index d72d78c4e..9da6d104b 100644 --- a/libretroshare/src/grouter/grouteritems.h +++ b/libretroshare/src/grouter/grouteritems.h @@ -25,6 +25,8 @@ #pragma once +#include "util/rsmemory.h" + #include "serialiser/rsserial.h" #include "serialiser/rstlvkeys.h" #include "serialiser/rsserviceids.h" @@ -194,13 +196,11 @@ class RsGRouterTransactionChunkItem: public RsGRouterTransactionItem, public RsG { RsGRouterTransactionChunkItem *item = new RsGRouterTransactionChunkItem ; *item = *this ; // copy all fields - item->chunk_data = (uint8_t*)malloc(chunk_size) ; // deep copy memory chunk + item->chunk_data = (uint8_t*)rs_safe_malloc(chunk_size) ; // deep copy memory chunk if(item->chunk_data == NULL) - { - std::cerr << "(EE) Memory allocation error in " << __PRETTY_FUNCTION__ << " for size " << chunk_size << std::endl; return NULL ; - } + memcpy(item->chunk_data,chunk_data,chunk_size) ; return item ; } diff --git a/libretroshare/src/grouter/p3grouter.cc b/libretroshare/src/grouter/p3grouter.cc index 4a42aa33b..65293b4a7 100644 --- a/libretroshare/src/grouter/p3grouter.cc +++ b/libretroshare/src/grouter/p3grouter.cc @@ -1121,13 +1121,11 @@ bool p3GRouter::locked_sendTransactionData(const RsPeerId& pid,const RsGRouterTr std::cerr << " sending to tunnel vpid " << pid << std::endl; #endif uint32_t turtle_data_size = trans_item.serial_size() ; - uint8_t *turtle_data = (uint8_t*)malloc(turtle_data_size) ; + uint8_t *turtle_data = (uint8_t*)rs_safe_malloc(turtle_data_size) ; if(turtle_data == NULL) - { - std::cerr << " ERROR: Cannot allocate turtle data memory for size " << turtle_data_size << std::endl; return false ; - } + if(!trans_item.serialise(turtle_data,turtle_data_size)) { std::cerr << " ERROR: cannot serialise RsGRouterTransactionChunkItem." << std::endl; @@ -1304,14 +1302,13 @@ bool p3GRouter::sliceDataItem(RsGRouterAbstractMsgItem *item,std::listtotal_size = size; chunk_item->chunk_start= offset; chunk_item->chunk_size = chunk_size ; - chunk_item->chunk_data = (uint8_t*)malloc(chunk_size) ; + chunk_item->chunk_data = (uint8_t*)rs_safe_malloc(chunk_size) ; #ifdef GROUTER_DEBUG std::cerr << " preparing to send a chunk [" << offset << " -> " << offset + chunk_size << " / " << size << "]" << std::endl; #endif if(chunk_item->chunk_data == NULL) { - std::cerr << " ERROR: Cannot allocate memory for size " << chunk_size << std::endl; delete chunk_item; throw ; } @@ -1921,13 +1918,11 @@ bool p3GRouter::sendData(const RsGxsId& destination,const GRouterServiceId& clie RsGRouterGenericDataItem *data_item = new RsGRouterGenericDataItem ; - data_item->data_bytes = (uint8_t*)malloc(data_size) ; + data_item->data_bytes = (uint8_t*)rs_safe_malloc(data_size) ; if(data_item->data_bytes == NULL) - { - std::cerr << "(EE) memory allocaiton error for " << data_size << " bytes in " << __PRETTY_FUNCTION__<< std::endl; return false ; - } + memcpy(data_item->data_bytes,data,data_size) ; data_item->data_size = data_size ; diff --git a/libretroshare/src/gxs/gxssecurity.cc b/libretroshare/src/gxs/gxssecurity.cc index 46e81315f..b310b6b64 100644 --- a/libretroshare/src/gxs/gxssecurity.cc +++ b/libretroshare/src/gxs/gxssecurity.cc @@ -444,13 +444,10 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u int out_offset = 0; int max_evp_key_size = EVP_PKEY_size(public_key); - ek = (unsigned char*)malloc(max_evp_key_size); + ek = (unsigned char*)rs_safe_malloc(max_evp_key_size); if(ek == NULL) - { - std::cerr << "(EE) memory allocation error for " << max_evp_key_size << " bytes in " << __PRETTY_FUNCTION__ << std::endl; return false ; - } const EVP_CIPHER *cipher = EVP_aes_128_cbc(); int cipher_block_size = EVP_CIPHER_block_size(cipher); @@ -462,13 +459,10 @@ bool GxsSecurity::encrypt(uint8_t *& out, uint32_t &outlen, const uint8_t *in, u if(!EVP_SealInit(&ctx, EVP_aes_128_cbc(), &ek, &eklen, iv, &public_key, 1)) return false; // now assign memory to out accounting for data, and cipher block size, key length, and key length val - out = (uint8_t*)malloc(inlen + cipher_block_size + size_net_ekl + eklen + EVP_MAX_IV_LENGTH); + out = (uint8_t*)rs_safe_malloc(inlen + cipher_block_size + size_net_ekl + eklen + EVP_MAX_IV_LENGTH); if(out == NULL) - { - std::cerr << "gxssecurity::encrypt(): cnnot allocate memory of size " << inlen + cipher_block_size + size_net_ekl + eklen + EVP_MAX_IV_LENGTH << " to encrypt data." << std::endl; return false ; - } net_ekl = htonl(eklen); memcpy((unsigned char*)out + out_offset, &net_ekl, size_net_ekl); @@ -547,13 +541,11 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, EVP_CIPHER_CTX ctx; int eklen = 0, net_ekl = 0; - unsigned char *ek = (unsigned char*)malloc(EVP_PKEY_size(privateKey)); + unsigned char *ek = (unsigned char*)rs_safe_malloc(EVP_PKEY_size(privateKey)); if(ek == NULL) - { - std::cerr << "Memory allocation error in " << __PRETTY_FUNCTION__ << " for " << EVP_PKEY_size(privateKey) << " bytes." << std::endl; return false ; - } + unsigned char iv[EVP_MAX_IV_LENGTH]; EVP_CIPHER_CTX_init(&ctx); @@ -587,13 +579,10 @@ bool GxsSecurity::decrypt(uint8_t *& out, uint32_t & outlen, const uint8_t *in, std::cerr << "Severe error in " << __PRETTY_FUNCTION__ << ": cannot encrypt. " << std::endl; return false ; } - out = (uint8_t*)malloc(inlen - in_offset); + out = (uint8_t*)rs_safe_malloc(inlen - in_offset); if(out == NULL) - { - std::cerr << "gxssecurity::decrypt(): cannot allocate memory of size " << inlen - in_offset << " to decrypt data." << std::endl; return false; - } if(!EVP_OpenUpdate(&ctx, (unsigned char*) out, &out_currOffset, (unsigned char*)in + in_offset, inlen - in_offset)) { diff --git a/libretroshare/src/gxstunnel/p3gxstunnel.cc b/libretroshare/src/gxstunnel/p3gxstunnel.cc index ecb130974..bc858b5bf 100644 --- a/libretroshare/src/gxstunnel/p3gxstunnel.cc +++ b/libretroshare/src/gxstunnel/p3gxstunnel.cc @@ -1035,7 +1035,15 @@ bool p3GxsTunnelService::locked_sendDHPublicKey(const DH *dh,const RsGxsId& own_ uint32_t error_status ; uint32_t size = BN_num_bytes(dhitem->public_key) ; - unsigned char *data = (unsigned char *)malloc(size) ; + + RsTemporaryMemory data(size) ; + + if(data == NULL) + { + delete(dhitem); + return false ; + } + BN_bn2bin(dhitem->public_key, data) ; if(!mGixs->signData((unsigned char*)data,size,own_gxs_id,signature,error_status)) @@ -1047,11 +1055,9 @@ bool p3GxsTunnelService::locked_sendDHPublicKey(const DH *dh,const RsGxsId& own_ default: std::cerr << "(EE) Unknown error when signing" << std::endl; break ; } - free(data) ; delete(dhitem); return false; } - free(data) ; if(!mGixs->getKey(own_gxs_id,signature_key_public)) { @@ -1135,11 +1141,10 @@ bool p3GxsTunnelService::locked_sendClearTunnelData(RsGxsTunnelDHPublicKeyItem * uint32_t rssize = item->serial_size() ; gitem->data_size = rssize + 8 ; - gitem->data_bytes = malloc(rssize+8) ; + gitem->data_bytes = rs_safe_malloc(rssize+8) ; if(gitem->data_bytes == NULL) { - std::cerr << "(EE) could not allocate " << rssize+8 << " bytes of data in " << __PRETTY_FUNCTION__ << std::endl; delete gitem ; return NULL ; } @@ -1226,13 +1231,11 @@ bool p3GxsTunnelService::locked_sendEncryptedTunnelData(RsGxsTunnelItem *item) RsTurtleGenericDataItem *gitem = new RsTurtleGenericDataItem ; gitem->data_size = encrypted_size + GXS_TUNNEL_ENCRYPTION_IV_SIZE + GXS_TUNNEL_ENCRYPTION_HMAC_SIZE ; - gitem->data_bytes = malloc(gitem->data_size) ; + gitem->data_bytes = rs_safe_malloc(gitem->data_size) ; if(gitem->data_bytes == NULL) - { - std::cerr << "(EE) cannot allocate " << gitem->data_size << " bytes of memory in " << __PRETTY_FUNCTION__<< std::endl; return false ; - } + memcpy(& ((uint8_t*)gitem->data_bytes)[0] ,&IV,8) ; unsigned int md_len = GXS_TUNNEL_ENCRYPTION_HMAC_SIZE ; @@ -1326,13 +1329,11 @@ bool p3GxsTunnelService::sendData(const RsGxsTunnelId &tunnel_id, uint32_t servi item->flags = 0; // not used yet. item->service_id = service_id; item->data_size = size; // encrypted data size - item->data = (uint8_t*)malloc(size); // encrypted data + item->data = (uint8_t*)rs_safe_malloc(size); // encrypted data if(item->data == NULL) - { - std::cerr << "(EE) Cannot allocate " << size << " bytes of memory in " << __PRETTY_FUNCTION__ << std::endl; delete item ; - } + item->PeerId(RsPeerId(tunnel_id)) ; memcpy(item->data,data,size) ; diff --git a/libretroshare/src/gxstunnel/rsgxstunnelitems.cc b/libretroshare/src/gxstunnel/rsgxstunnelitems.cc index 21c7f0333..37c03a237 100644 --- a/libretroshare/src/gxstunnel/rsgxstunnelitems.cc +++ b/libretroshare/src/gxstunnel/rsgxstunnelitems.cc @@ -29,6 +29,7 @@ #include "serialiser/rsbaseserial.h" #include "serialiser/rstlvbase.h" #include "util/rsprint.h" +#include "util/rsmemory.h" #include "gxstunnel/rsgxstunnelitems.h" @@ -393,7 +394,7 @@ RsGxsTunnelDataItem *RsGxsTunnelSerialiser::deserialise_RsGxsTunnelDataItem(void delete item ; return NULL ; } - item->data = (unsigned char*)malloc(item->data_size) ; + item->data = (unsigned char*)rs_safe_malloc(item->data_size) ; if(item->data == NULL) { diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index c6e64cd7e..d01d4c9dd 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -361,7 +361,6 @@ HEADERS += chat/distantchat.h \ HEADERS += pqi/authssl.h \ pqi/authgpg.h \ - pqi/rsmemory.h \ pgp/pgphandler.h \ pgp/pgpkeyutil.h \ pgp/rsaes.h \ @@ -475,6 +474,7 @@ HEADERS += turtle/p3turtle.h \ HEADERS += util/folderiterator.h \ util/rsdebug.h \ + util/rsmemory.h \ util/rscompress.h \ util/smallobject.h \ util/rsdir.h \ @@ -631,6 +631,7 @@ SOURCES += util/folderiterator.cc \ util/rscompress.cc \ util/smallobject.cc \ util/rsdir.cc \ + util/rsmemory.cc \ util/rsdiscspace.cc \ util/rsnet.cc \ util/rsnet_ss.cc \ diff --git a/libretroshare/src/pgp/pgphandler.cc b/libretroshare/src/pgp/pgphandler.cc index 685a2f739..1c7fa311d 100644 --- a/libretroshare/src/pgp/pgphandler.cc +++ b/libretroshare/src/pgp/pgphandler.cc @@ -26,6 +26,7 @@ extern "C" { #include "retroshare/rspeers.h" // For rsicontrol. #include "util/rsdir.h" #include "util/rsdiscspace.h" +#include "util/rsmemory.h" #include "pgp/pgpkeyutil.h" static const uint32_t PGP_CERTIFICATE_LIMIT_MAX_NAME_SIZE = 64 ; @@ -39,13 +40,11 @@ PassphraseCallback PGPHandler::_passphrase_callback = NULL ; ops_keyring_t *PGPHandler::allocateOPSKeyring() { - ops_keyring_t *kr = (ops_keyring_t*)malloc(sizeof(ops_keyring_t)) ; + ops_keyring_t *kr = (ops_keyring_t*)rs_safe_malloc(sizeof(ops_keyring_t)) ; if(kr == NULL) - { - std::cerr << "(EE) Cannot allocate memory for " << sizeof(ops_keyring_t) << " bytes in " << __PRETTY_FUNCTION__ << std::endl; return NULL ; - } + kr->nkeys = 0 ; kr->nkeys_allocated = 0 ; kr->keys = 0 ; diff --git a/libretroshare/src/pqi/authssl.cc b/libretroshare/src/pqi/authssl.cc index 27cae90f6..cfdae9e61 100644 --- a/libretroshare/src/pqi/authssl.cc +++ b/libretroshare/src/pqi/authssl.cc @@ -117,10 +117,10 @@ static struct CRYPTO_dynlock_value *dyn_create_function(const char */*file*/, in { struct CRYPTO_dynlock_value *value; - value = (struct CRYPTO_dynlock_value*) malloc(sizeof(struct CRYPTO_dynlock_value)); - if (!value) { + value = (struct CRYPTO_dynlock_value*) rs_safe_malloc(sizeof(struct CRYPTO_dynlock_value)); + if (!value) return NULL; - } + pthread_mutex_init(&value->mutex, NULL); return value; @@ -166,10 +166,10 @@ static void dyn_destroy_function(struct CRYPTO_dynlock_value *l, const char */*f bool tls_init() { /* static locks area */ - mutex_buf = (pthread_mutex_t*) malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); - if (mutex_buf == NULL) { + mutex_buf = (pthread_mutex_t*) rs_safe_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); + if (mutex_buf == NULL) return false; - } + for (int i = 0; i < CRYPTO_num_locks(); i++) { pthread_mutex_init(&mutex_buf[i], NULL); } @@ -1259,7 +1259,11 @@ bool AuthSSLimpl::encrypt(void *&out, int &outlen, const void *in, int inlen, int out_offset = 0; int max_evp_key_size = EVP_PKEY_size(public_key); - ek = (unsigned char*)malloc(max_evp_key_size); + ek = (unsigned char*)rs_safe_malloc(max_evp_key_size); + + if(ek == NULL) + return false ; + const EVP_CIPHER *cipher = EVP_aes_128_cbc(); int cipher_block_size = EVP_CIPHER_block_size(cipher); int size_net_ekl = sizeof(net_ekl); @@ -1273,8 +1277,13 @@ bool AuthSSLimpl::encrypt(void *&out, int &outlen, const void *in, int inlen, } // now assign memory to out accounting for data, and cipher block size, key length, and key length val - out = (unsigned char*)malloc(inlen + cipher_block_size + size_net_ekl + eklen + EVP_MAX_IV_LENGTH); + out = (unsigned char*)rs_safe_malloc(inlen + cipher_block_size + size_net_ekl + eklen + EVP_MAX_IV_LENGTH); + if(out == NULL) + { + free(ek) ; + return false ; + } net_ekl = htonl(eklen); memcpy((unsigned char*)out + out_offset, &net_ekl, size_net_ekl); out_offset += size_net_ekl; @@ -1343,6 +1352,12 @@ bool AuthSSLimpl::decrypt(void *&out, int &outlen, const void *in, int inlen) unsigned char iv[EVP_MAX_IV_LENGTH]; int ek_mkl = EVP_PKEY_size(mOwnPrivateKey); ek = (unsigned char*)malloc(ek_mkl); + + if(ek == NULL) + { + std::cerr << "(EE) Cannot allocate memory for " << ek_mkl << " bytes in " << __PRETTY_FUNCTION__ << std::endl; + return false ; + } EVP_CIPHER_CTX_init(&ctx); int in_offset = 0, out_currOffset = 0; @@ -1380,8 +1395,13 @@ bool AuthSSLimpl::decrypt(void *&out, int &outlen, const void *in, int inlen) return false; } - out = (unsigned char*)malloc(inlen - in_offset); + out = (unsigned char*)rs_safe_malloc(inlen - in_offset); + if(out == NULL) + { + free(ek) ; + return false ; + } if(!EVP_OpenUpdate(&ctx, (unsigned char*) out, &out_currOffset, (unsigned char*)in + in_offset, inlen - in_offset)) { free(ek); free(out) ; diff --git a/libretroshare/src/pqi/pqibin.cc b/libretroshare/src/pqi/pqibin.cc index 2aaa6e34a..a07303cb0 100644 --- a/libretroshare/src/pqi/pqibin.cc +++ b/libretroshare/src/pqi/pqibin.cc @@ -27,6 +27,7 @@ #include "pqi/authssl.h" #include "util/rsnet.h" #include "util/rsdir.h" +#include "util/rsmemory.h" // #define DEBUG_PQIBIN @@ -314,7 +315,13 @@ BinMemInterface::BinMemInterface(int defsize, int flags) :bin_flags(flags), buf(NULL), size(defsize), recvsize(0), readloc(0), hash(NULL), bcount(0) { - buf = malloc(defsize); + buf = rs_safe_malloc(defsize); + + if(buf == NULL) + { + close() ; + return ; + } if (bin_flags & BIN_FLAGS_HASH_DATA) { hash = new pqihash(); @@ -326,7 +333,13 @@ BinMemInterface::BinMemInterface(const void *data, const int defsize, int flags) :bin_flags(flags), buf(NULL), size(defsize), recvsize(0), readloc(0), hash(NULL), bcount(0) { - buf = malloc(defsize); + buf = rs_safe_malloc(defsize); + + if(buf == NULL) + { + close() ; + return ; + } if (bin_flags & BIN_FLAGS_HASH_DATA) { hash = new pqihash(); diff --git a/libretroshare/src/pqi/pqinetwork.cc b/libretroshare/src/pqi/pqinetwork.cc index 7f43068c9..e00a08e4c 100644 --- a/libretroshare/src/pqi/pqinetwork.cc +++ b/libretroshare/src/pqi/pqinetwork.cc @@ -290,7 +290,11 @@ bool getLocalAddresses(std::list & addrs) #ifdef WINDOWS_SYS // Seems strange to me but M$ documentation suggests to allocate this way... DWORD bf_size = 16000; - IP_ADAPTER_ADDRESSES* adapter_addresses = (IP_ADAPTER_ADDRESSES*) malloc(bf_size); + IP_ADAPTER_ADDRESSES* adapter_addresses = (IP_ADAPTER_ADDRESSES*) rs_safe_malloc(bf_size); + + if(adapter_addresses == NULL) + return false ; + DWORD error = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | diff --git a/libretroshare/src/pqi/pqistore.cc b/libretroshare/src/pqi/pqistore.cc index 217ebdfb4..7b364e663 100644 --- a/libretroshare/src/pqi/pqistore.cc +++ b/libretroshare/src/pqi/pqistore.cc @@ -46,6 +46,7 @@ #include #include "util/rsdebug.h" +#include "util/rsmemory.h" #include "util/rsstring.h" // @@ -194,7 +195,12 @@ int pqistore::writePkt(RsItem *pqi) #endif uint32_t pktsize = rsSerialiser->size(pqi); - void *ptr = malloc(pktsize); + + RsTemporaryMemory ptr(pktsize) ; + + if(ptr == NULL) + return 0 ; + if (!(rsSerialiser->serialise(pqi, ptr, &pktsize))) { #ifdef PQISTORE_DEBUG @@ -203,7 +209,6 @@ int pqistore::writePkt(RsItem *pqi) pqioutput(PQL_ALERT, pqistorezone, out); #endif - free(ptr); if (!(bio_flags & BIN_FLAGS_NO_DELETE)) delete pqi; return 0; @@ -218,7 +223,6 @@ int pqistore::writePkt(RsItem *pqi) pqi -> print_string(out); pqioutput(PQL_ALERT, pqistorezone, out); - free(ptr); if (!(bio_flags & BIN_FLAGS_NO_DELETE)) delete pqi; return 0; @@ -232,7 +236,6 @@ int pqistore::writePkt(RsItem *pqi) pqi -> print_string(out); pqioutput(PQL_ALERT, pqistorezone, out); - free(ptr); if (!(bio_flags & BIN_FLAGS_NO_DELETE)) delete pqi; @@ -250,7 +253,6 @@ int pqistore::writePkt(RsItem *pqi) pqioutput(PQL_ALERT, pqistorezone, out); #endif - free(ptr); if (!(bio_flags & BIN_FLAGS_NO_DELETE)) delete pqi; @@ -262,7 +264,6 @@ int pqistore::writePkt(RsItem *pqi) pqioutput(PQL_DEBUG_BASIC, pqistorezone, out); #endif - free(ptr); if (!(bio_flags & BIN_FLAGS_NO_DELETE)) delete pqi; @@ -288,7 +289,10 @@ int pqistore::readPkt(RsItem **item_out) // initial read size: basic packet. int blen = getRsPktBaseSize(); - void *block = malloc(blen); + void *block = rs_safe_malloc(blen); + + if(block == NULL) + return false ; int tmplen; /* we have the header */ @@ -495,7 +499,10 @@ int pqiSSLstore::readPkt(RsItem **item_out) // initial read size: basic packet. int blen = getRsPktBaseSize(); - void *block = malloc(blen); + void *block = rs_safe_malloc(blen); + + if(block == NULL) + return false ; int tmplen; /* we have the header */ diff --git a/libretroshare/src/pqi/pqistreamer.cc b/libretroshare/src/pqi/pqistreamer.cc index 437c4d8fb..29fea90db 100644 --- a/libretroshare/src/pqi/pqistreamer.cc +++ b/libretroshare/src/pqi/pqistreamer.cc @@ -315,7 +315,11 @@ int pqistreamer::queue_outpqi_locked(RsItem *pqi,uint32_t& pktsize) /* decide which type of packet it is */ pktsize = mRsSerialiser->size(pqi); - void *ptr = malloc(pktsize); + void *ptr = rs_safe_malloc(pktsize); + + if(ptr == NULL) + return 0 ; + #ifdef DEBUG_PQISTREAMER std::cerr << "pqistreamer::queue_outpqi() serializing packet with packet size : " << pktsize << std::endl; @@ -1040,7 +1044,10 @@ void pqistreamer::allocate_rpend_locked() return; mPkt_rpend_size = getRsPktMaxSize(); - mPkt_rpending = malloc(mPkt_rpend_size); + mPkt_rpending = rs_safe_malloc(mPkt_rpend_size); + + if(mPkt_rpending == NULL) + return ; // avoid uninitialized (and random) memory read. memset(mPkt_rpending,0,mPkt_rpend_size) ; diff --git a/libretroshare/src/retroshare/rsplugin.h b/libretroshare/src/retroshare/rsplugin.h index 579b7694c..6b8363c37 100644 --- a/libretroshare/src/retroshare/rsplugin.h +++ b/libretroshare/src/retroshare/rsplugin.h @@ -160,7 +160,7 @@ class RsPlugin // creates a new resource api handler object. ownership is transferred to the caller. // the caller should supply a statetokenserver, and keep it valid until destruction // the plugin should return a entry point name. this is to make the entry point name independent from file names - virtual resource_api::ResourceRouter* new_resource_api_handler(const RsPlugInInterfaces& ifaces, resource_api::StateTokenServer* sts, std::string &entrypoint) const { return 0;} + virtual resource_api::ResourceRouter* new_resource_api_handler(const RsPlugInInterfaces& /* ifaces */, resource_api::StateTokenServer* /* sts */, std::string & /*entrypoint*/) const { return 0;} // Shutdown virtual void stop() {} diff --git a/libretroshare/src/rsserver/rsloginhandler.cc b/libretroshare/src/rsserver/rsloginhandler.cc index 4236709d7..7ab28eeee 100644 --- a/libretroshare/src/rsserver/rsloginhandler.cc +++ b/libretroshare/src/rsserver/rsloginhandler.cc @@ -273,7 +273,13 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd fseek(fp, 0, SEEK_END); datalen = ftell(fp); fseek(fp, 0, SEEK_SET); - dataptr = (char *) malloc(datalen); + dataptr = (char *) rs_safe_malloc(datalen); + + if(data_ptr == NULL) + { + fclose(fp); + return false; + } fread(dataptr, 1, datalen, fp); fclose(fp); diff --git a/libretroshare/src/serialiser/rsserial.h b/libretroshare/src/serialiser/rsserial.h index dadf9a3d6..ee91394b0 100644 --- a/libretroshare/src/serialiser/rsserial.h +++ b/libretroshare/src/serialiser/rsserial.h @@ -34,6 +34,7 @@ #include #include +#include "util/rsmemory.h" #include "retroshare/rstypes.h" /******************************************************************* @@ -181,7 +182,7 @@ class RsRawItem: public RsItem RsRawItem(uint32_t t, uint32_t size) :RsItem(t), len(size) { - data = malloc(len); + data = rs_safe_malloc(len); } virtual ~RsRawItem() diff --git a/libretroshare/src/serialiser/rstlvbinary.cc b/libretroshare/src/serialiser/rstlvbinary.cc index f27f97fcf..0ce41df26 100644 --- a/libretroshare/src/serialiser/rstlvbinary.cc +++ b/libretroshare/src/serialiser/rstlvbinary.cc @@ -24,6 +24,7 @@ * */ +#include "util/rsmemory.h" #include "serialiser/rstlvbinary.h" #include "serialiser/rstlvbase.h" @@ -74,7 +75,11 @@ bool RsTlvBinaryData::setBinData(const void *data, uint32_t size) return true; } - bin_data = malloc(bin_len); + bin_data = rs_safe_malloc(bin_len); + + if(bin_data == NULL) + return false ; + memcpy(bin_data, data, bin_len); return true; } diff --git a/libretroshare/src/services/p3gxscommon.cc b/libretroshare/src/services/p3gxscommon.cc index 1f98072fa..25f348c2c 100644 --- a/libretroshare/src/services/p3gxscommon.cc +++ b/libretroshare/src/services/p3gxscommon.cc @@ -98,7 +98,8 @@ void RsGxsImage::take(uint8_t *data, uint32_t size) // NB Must make sure that we always use malloc/free for this data. uint8_t *RsGxsImage::allocate(uint32_t size) { - uint8_t *val = (uint8_t *) malloc(size); + uint8_t *val = (uint8_t *) rs_safe_malloc(size); + #ifdef DEBUG_GXSCOMMON std::cerr << "RsGxsImage()::allocate(" << (void *) val << ")"; std::cerr << std::endl; diff --git a/libretroshare/src/services/p3photoservice.cc b/libretroshare/src/services/p3photoservice.cc index 1821c8d69..f002290aa 100644 --- a/libretroshare/src/services/p3photoservice.cc +++ b/libretroshare/src/services/p3photoservice.cc @@ -26,7 +26,11 @@ bool RsPhotoThumbnail::copyFrom(const RsPhotoThumbnail &nail) size = nail.size; type = nail.type; - data = (uint8_t *) malloc(size); + data = (uint8_t *) rs_safe_malloc(size); + + if(data == NULL) + return false ; + memcpy(data, nail.data, size); return true; diff --git a/libretroshare/src/tcponudp/tcppacket.cc b/libretroshare/src/tcponudp/tcppacket.cc index 23b007d5b..fee71e1bc 100644 --- a/libretroshare/src/tcponudp/tcppacket.cc +++ b/libretroshare/src/tcponudp/tcppacket.cc @@ -36,6 +36,7 @@ #include #include +#include #include @@ -79,8 +80,10 @@ TcpPacket::TcpPacket(uint8 *ptr, int size) if (size > 0) { datasize = size; - data = (uint8 *) malloc(datasize); - memcpy(data, (void *) ptr, size); + data = (uint8 *) rs_safe_malloc(datasize); + + if(data != NULL) + memcpy(data, (void *) ptr, size); } return; } @@ -185,7 +188,10 @@ int TcpPacket::readPacket(void *buf, int size) free(data); } datasize = size - TCP_PSEUDO_HDR_SIZE; - data = (uint8 *) malloc(datasize); + data = (uint8 *) rs_safe_malloc(datasize); + + if(data == NULL) + return 0 ; /* now the data */ memcpy(data, (void *) &(((uint8 *) buf)[20]), datasize); diff --git a/libretroshare/src/tcponudp/udprelay.cc b/libretroshare/src/tcponudp/udprelay.cc index cc339f07d..7ca78b69c 100644 --- a/libretroshare/src/tcponudp/udprelay.cc +++ b/libretroshare/src/tcponudp/udprelay.cc @@ -26,6 +26,7 @@ #include "udprelay.h" #include #include +#include /* * #define DEBUG_UDP_RELAY 1 @@ -70,7 +71,7 @@ UdpRelayReceiver::UdpRelayReceiver(UdpPublisher *pub) setRelayClassMax(UDP_RELAY_CLASS_GENERAL, UDP_RELAY_DEFAULT_GENERAL, UDP_RELAY_DEFAULT_BANDWIDTH); /* only allocate this space once */ - mTmpSendPkt = malloc(MAX_RELAY_UDP_PACKET_SIZE); + mTmpSendPkt = rs_safe_malloc(MAX_RELAY_UDP_PACKET_SIZE); mTmpSendSize = MAX_RELAY_UDP_PACKET_SIZE; clearDataTransferred(); diff --git a/libretroshare/src/tcponudp/udpstunner.cc b/libretroshare/src/tcponudp/udpstunner.cc index 06e1748ad..9add379ed 100644 --- a/libretroshare/src/tcponudp/udpstunner.cc +++ b/libretroshare/src/tcponudp/udpstunner.cc @@ -29,6 +29,7 @@ #include "util/rsrandom.h" #include "util/rsprint.h" +#include "util/rsmemory.h" #include "util/rsstring.h" static const int STUN_TTL = 64; @@ -535,7 +536,11 @@ bool UdpStun_generate_stun_pkt(void *stun_pkt, int *len) void *UdpStun_generate_stun_reply(struct sockaddr_in *stun_addr, int *len) { /* just the header */ - void *stun_pkt = malloc(28); + void *stun_pkt = rs_safe_malloc(28); + + if(!stun_pkt) + return NULL ; + ((uint16_t *) stun_pkt)[0] = (uint16_t) htons(0x0101); ((uint16_t *) stun_pkt)[1] = (uint16_t) htons(28); /* only header + 8 byte addr */ /* transaction id - should be random */ diff --git a/libretroshare/src/turtle/rsturtleitem.cc b/libretroshare/src/turtle/rsturtleitem.cc index b6b9b831a..6cae770d7 100644 --- a/libretroshare/src/turtle/rsturtleitem.cc +++ b/libretroshare/src/turtle/rsturtleitem.cc @@ -547,7 +547,7 @@ RsTurtleGenericDataItem::RsTurtleGenericDataItem(void *data,uint32_t pktsize) if(data_size > rssize || rssize - data_size < offset) throw std::runtime_error("RsTurtleTunnelOkItem::() wrong data_size (exceeds rssize).") ; - data_bytes = malloc(data_size) ; + data_bytes = rs_safe_malloc(data_size) ; if(data_bytes != NULL) { @@ -555,10 +555,7 @@ RsTurtleGenericDataItem::RsTurtleGenericDataItem(void *data,uint32_t pktsize) offset += data_size ; } else - { - std::cerr << "(EE) RsTurtleGenericDataItem: Error. Cannot allocate data for a size of " << data_size << " bytes." < #include "rscompress.h" #include "zlib.h" +#include "util/rsmemory.h" // 16K buffer size. // @@ -42,7 +43,10 @@ bool RsCompress::compress_memory_chunk(const uint8_t *input_mem,const uint32_t i uint32_t output_offset = 0 ; uint32_t input_offset = 0 ; output_size = 1024 ; - output_mem = (uint8_t*)malloc(output_size) ; + output_mem = (uint8_t*)rs_safe_malloc(output_size) ; + + if(!output_mem) + return false ; int ret, flush; unsigned have; @@ -113,8 +117,11 @@ bool RsCompress::uncompress_memory_chunk(const uint8_t *input_mem,const uint32_t output_size = input_size ; uint32_t output_offset = 0 ; uint32_t input_offset = 0 ; - output_mem = (uint8_t*)malloc(output_size) ; + output_mem = (uint8_t*)rs_safe_malloc(output_size) ; + if(!output_mem) + return false ; + int ret; unsigned have; z_stream strm; diff --git a/libretroshare/src/util/rsdir.cc b/libretroshare/src/util/rsdir.cc index b396b1672..bc9de2c43 100644 --- a/libretroshare/src/util/rsdir.cc +++ b/libretroshare/src/util/rsdir.cc @@ -33,6 +33,7 @@ #include "util/rsdir.h" #include "util/rsstring.h" #include "util/rsrandom.h" +#include "util/rsmemory.h" #include "retroshare/rstypes.h" #include "rsthreads.h" #include @@ -267,7 +268,14 @@ bool RsDirUtil::copyFile(const std::string& source,const std::string& dest) size_t T=0; static const int BUFF_SIZE = 10485760 ; // 10 MB buffer to speed things up. - void *buffer = malloc(BUFF_SIZE) ; + RsTemporaryMemory buffer(BUFF_SIZE) ; + + if(!buffer) + { + fclose(in) ; + fclose(out) ; + return false ; + } bool bRet = true; @@ -286,8 +294,6 @@ bool RsDirUtil::copyFile(const std::string& source,const std::string& dest) fclose(in) ; fclose(out) ; - free(buffer) ; - return true ; #endif diff --git a/libretroshare/src/util/rsmemory.cc b/libretroshare/src/util/rsmemory.cc new file mode 100644 index 000000000..0d68bbad4 --- /dev/null +++ b/libretroshare/src/util/rsmemory.cc @@ -0,0 +1,32 @@ +#include "util/rsmemory.h" + +void *rs_safe_malloc(size_t size) +{ + static const size_t SAFE_MEMALLOC_THRESHOLD = 1024*1024*1024 ; // 1Gb should be enough for everything! + + if(size == 0) + { + std::cerr << "(EE) Memory allocation error. A chunk of size 0 was requested. Callstack:" << std::endl; + print_stacktrace() ; + return NULL ; + } + + if(size > SAFE_MEMALLOC_THRESHOLD) + { + std::cerr << "(EE) Memory allocation error. A chunk of size 0 was requested. Callstack:" << std::endl; + print_stacktrace() ; + return NULL ; + } + + void *mem = malloc(size) ; + + if(mem == NULL) + { + std::cerr << "(EE) Memory allocation error for a chunk of " << size << " bytes. Callstack:" << std::endl; + print_stacktrace() ; + return NULL ; + } + + return mem ; +} + diff --git a/libretroshare/src/util/rsmemory.h b/libretroshare/src/util/rsmemory.h index c6b88ed2b..e12a5bc5a 100644 --- a/libretroshare/src/util/rsmemory.h +++ b/libretroshare/src/util/rsmemory.h @@ -1,6 +1,10 @@ #pragma once #include +#include +#include + +void *rs_safe_malloc(size_t size) ; // This is a scope guard to release the memory block when going of of the current scope. // Can be very useful to auto-delete some memory on quit without the need to call free each time. @@ -24,7 +28,7 @@ class RsTemporaryMemory public: RsTemporaryMemory(size_t s) { - _mem = (unsigned char *)malloc(s) ; + _mem = (unsigned char *)rs_safe_malloc(s) ; if(_mem) _size = s ; @@ -53,5 +57,3 @@ private: RsTemporaryMemory& operator=(const RsTemporaryMemory&) { return *this ;} RsTemporaryMemory(const RsTemporaryMemory&) {} }; - - diff --git a/libretroshare/src/util/rsrecogn.cc b/libretroshare/src/util/rsrecogn.cc index 6712608a4..0f5fc0504 100644 --- a/libretroshare/src/util/rsrecogn.cc +++ b/libretroshare/src/util/rsrecogn.cc @@ -145,7 +145,12 @@ bool RsRecogn::loadSigningKeys(std::map &signM /* store in */ uint32_t datalen = recognSerialiser.size(item); - uint8_t *data = (uint8_t *) malloc(datalen); + + RsTemporaryMemory data(datalen) ; + + if(!data) + return false ; + uint32_t pktlen = datalen; int signOk = 0; @@ -181,8 +186,6 @@ bool RsRecogn::loadSigningKeys(std::map &signM #endif // DEBUG_RECOGN delete item; } - - free(data); } /* clean up */ @@ -233,7 +236,12 @@ bool RsRecogn::validateTagSignature(RsGxsRecognSignerItem *signer, RsGxsRecognTa RsGxsRecognSerialiser serialiser; uint32_t datalen = serialiser.size(item); - uint8_t *data = (uint8_t *) malloc(datalen); + + RsTemporaryMemory data(datalen) ; + + if(!data) + return false ; + int signOk = 0; uint32_t pktlen = datalen; @@ -262,8 +270,6 @@ bool RsRecogn::validateTagSignature(RsGxsRecognSignerItem *signer, RsGxsRecognTa EVP_MD_CTX_destroy(mdctx); EVP_PKEY_free(signKey); - - free(data); return (signOk == 1); } diff --git a/libretroshare/src/util/smallobject.cc b/libretroshare/src/util/smallobject.cc index 3d91e99f9..e075b4821 100644 --- a/libretroshare/src/util/smallobject.cc +++ b/libretroshare/src/util/smallobject.cc @@ -1,6 +1,7 @@ #include #include "smallobject.h" #include "util/rsthreads.h" +#include "util/rsmemory.h" using namespace RsMemoryManagement ; @@ -206,7 +207,7 @@ SmallObjectAllocator::~SmallObjectAllocator() void *SmallObjectAllocator::allocate(size_t bytes) { if(bytes > _maxObjectSize) - return malloc(bytes) ; + return rs_safe_malloc(bytes) ; else if(_lastAlloc != NULL && _lastAlloc->blockSize() == bytes) return _lastAlloc->allocate() ; else diff --git a/plugins/VOIP/gui/SpeexProcessor.cpp b/plugins/VOIP/gui/SpeexProcessor.cpp index 4322a1912..078fbacba 100644 --- a/plugins/VOIP/gui/SpeexProcessor.cpp +++ b/plugins/VOIP/gui/SpeexProcessor.cpp @@ -1,5 +1,6 @@ #include "SpeexProcessor.h" +#include #include #include @@ -350,7 +351,11 @@ void SpeexOutputProcessor::putNetworkPacket(QString name, QByteArray packet) { if (userJitterHash.contains(name)) { userJitter = userJitterHash.value(name); } else { - userJitter = (SpeexJitter*)malloc(sizeof(SpeexJitter)); + userJitter = (SpeexJitter*)rs_safe_malloc(sizeof(SpeexJitter)); + + if(!userJitter) + return ; + speex_jitter_init(userJitter, speex_decoder_init(&speex_wb_mode), SAMPLING_RATE); int on = 1; speex_decoder_ctl(userJitter->dec, SPEEX_SET_ENH, &on); diff --git a/plugins/VOIP/gui/VideoProcessor.cpp b/plugins/VOIP/gui/VideoProcessor.cpp index ad0a217ec..7ecd380ea 100644 --- a/plugins/VOIP/gui/VideoProcessor.cpp +++ b/plugins/VOIP/gui/VideoProcessor.cpp @@ -10,6 +10,8 @@ #include #include +#include "util/rsmemory.h" + #include "VideoProcessor.h" #include "QVideoDevice.h" @@ -384,7 +386,10 @@ bool JPEGVideo::encodeData(const QImage& image,uint32_t /* size_hint */,RsVOIPDa buffer.open(QIODevice::WriteOnly) ; encoded_frame.save(&buffer,"JPEG") ; - voip_chunk.data = malloc(HEADER_SIZE + qb.size()); + voip_chunk.data = rs_safe_malloc(HEADER_SIZE + qb.size()); + + if(!voip_chunk.data) + return false ; // build header uint32_t flags = differential_frame ? JPEG_VIDEO_FLAGS_DIFFERENTIAL_FRAME : 0x0 ; @@ -679,7 +684,11 @@ bool FFmpegVideo::encodeData(const QImage& image, uint32_t target_encoding_bitra if(got_output) { - voip_chunk.data = malloc(pkt.size + HEADER_SIZE) ; + voip_chunk.data = rs_safe_malloc(pkt.size + HEADER_SIZE) ; + + if(!voip_chunk.data) + return false ; + uint32_t flags = 0; ((unsigned char *)voip_chunk.data)[0] = VideoProcessor::VIDEO_PROCESSOR_CODEC_ID_MPEG_VIDEO & 0xff ; diff --git a/plugins/VOIP/services/p3VOIP.cc b/plugins/VOIP/services/p3VOIP.cc index a17854379..421a727f5 100644 --- a/plugins/VOIP/services/p3VOIP.cc +++ b/plugins/VOIP/services/p3VOIP.cc @@ -280,11 +280,10 @@ int p3VOIP::sendVoipData(const RsPeerId& peer_id,const RsVOIPDataChunk& chunk) std::cerr << "Cannot allocate RsVOIPDataItem !" << std::endl; return false ; } - item->voip_data = malloc(chunk.size) ; + item->voip_data = rs_safe_malloc(chunk.size) ; if(item->voip_data == NULL) { - std::cerr << "Cannot allocate RsVOIPDataItem.voip_data of size " << chunk.size << " !" << std::endl; delete item ; return false ; } @@ -432,11 +431,10 @@ bool p3VOIP::getIncomingData(const RsPeerId& peer_id,std::vectordata_size ; - chunk.data = malloc((*it2)->data_size) ; + chunk.data = rs_safe_malloc((*it2)->data_size) ; if(chunk.data == NULL) { - std::cerr << "(EE) p3VOIP::getIncomingData(): error. Cannot allocate memory for chunk of size " << chunk.size << std::endl; delete *it2 ; continue ; } diff --git a/plugins/VOIP/services/rsVOIPItems.cc b/plugins/VOIP/services/rsVOIPItems.cc index 9ba20868e..1beab94b7 100644 --- a/plugins/VOIP/services/rsVOIPItems.cc +++ b/plugins/VOIP/services/rsVOIPItems.cc @@ -450,7 +450,11 @@ RsVOIPDataItem::RsVOIPDataItem(void *data, uint32_t pktsize) if(data_size > rssize || rssize - data_size < offset) throw std::runtime_error("Not enough space.") ; - voip_data = malloc(data_size) ; + voip_data = rs_safe_malloc(data_size) ; + + if(!voip_data) + throw std::runtime_error("Serialization error.") ; + memcpy(voip_data,&((uint8_t*)data)[offset],data_size) ; offset += data_size ; diff --git a/retroshare-gui/src/gui/notifyqt.cpp b/retroshare-gui/src/gui/notifyqt.cpp index 7c19da271..1cbff3dcc 100644 --- a/retroshare-gui/src/gui/notifyqt.cpp +++ b/retroshare-gui/src/gui/notifyqt.cpp @@ -151,11 +151,25 @@ class SignatureEventData { // We need a new memory chnk because there's no guarranty _sign nor _signlen are not in the stack - sign = (unsigned char *)malloc(_signlen) ; + sign = (unsigned char *)rs_safe_malloc(_signlen) ; + + if(!sign) + { + signlen = NULL ; + signature_result = SELF_SIGNATURE_RESULT_FAILED ; + return ; + } + signlen = new unsigned int ; *signlen = _signlen ; signature_result = SELF_SIGNATURE_RESULT_PENDING ; - data = malloc(_len) ; + data = rs_safe_malloc(_len) ; + + if(!data) + { + len = 0 ; + return ; + } len = _len ; memcpy(data,_data,len) ; }