- add a few more safety's in the code to avoid memory issues

This commit is contained in:
Mark Vejvoda
2013-02-08 07:54:12 +00:00
parent e3c03b5916
commit 74d7841c4d
2 changed files with 16 additions and 6 deletions

View File

@@ -195,8 +195,7 @@ Mutex UPNP_Tools::mutexUPNP;
int nErrorID = 0 ) int nErrorID = 0 )
{ {
// Build basic error string // Build basic error string
static char acErrorBuffer[256]; static char acErrorBuffer[8096];
std::ostrstream outs(acErrorBuffer, (sizeof(acErrorBuffer) / sizeof(acErrorBuffer[0])));
outs << pcMessagePrefix << ": "; outs << pcMessagePrefix << ": ";
// Tack appropriate canned message onto end of supplied message // Tack appropriate canned message onto end of supplied message
@@ -218,7 +217,7 @@ Mutex UPNP_Tools::mutexUPNP;
// Finish error message off and return it. // Finish error message off and return it.
outs << std::ends; outs << std::ends;
acErrorBuffer[(sizeof(acErrorBuffer) / sizeof(acErrorBuffer[0])) - 1] = '\0'; acErrorBuffer[8095] = '\0';
return acErrorBuffer; return acErrorBuffer;
} }

View File

@@ -42,6 +42,11 @@ Thread::~Thread() {
MutexSafeWrapper safeMutex(&Thread::mutexthreadList); MutexSafeWrapper safeMutex(&Thread::mutexthreadList);
std::vector<Thread *>::iterator iterFind = std::find(Thread::threadList.begin(),Thread::threadList.end(),this); std::vector<Thread *>::iterator iterFind = std::find(Thread::threadList.begin(),Thread::threadList.end(),this);
if(iterFind == Thread::threadList.end()) {
char szBuf[1024]="";
snprintf(szBuf,1023,"In [%s::%s Line: %d] iterFind == Thread::threadList.end()",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
throw megaglest_runtime_error(szBuf);
}
Thread::threadList.erase(iterFind); Thread::threadList.erase(iterFind);
safeMutex.ReleaseLock(); safeMutex.ReleaseLock();
} }
@@ -428,7 +433,9 @@ void MasterSlaveThreadController::clearSlaves(bool clearListOnly) {
if(clearListOnly == false) { if(clearListOnly == false) {
for(unsigned int i = 0; i < this->slaveThreadList.size(); ++i) { for(unsigned int i = 0; i < this->slaveThreadList.size(); ++i) {
SlaveThreadControllerInterface *slave = this->slaveThreadList[i]; SlaveThreadControllerInterface *slave = this->slaveThreadList[i];
slave->setMasterController(NULL); if(slave != NULL) {
slave->setMasterController(NULL);
}
} }
} }
this->slaveThreadList.clear(); this->slaveThreadList.clear();
@@ -457,7 +464,9 @@ void MasterSlaveThreadController::setSlaves(std::vector<SlaveThreadControllerInt
if(this->slaveThreadList.empty() == false) { if(this->slaveThreadList.empty() == false) {
for(unsigned int i = 0; i < this->slaveThreadList.size(); ++i) { for(unsigned int i = 0; i < this->slaveThreadList.size(); ++i) {
SlaveThreadControllerInterface *slave = this->slaveThreadList[i]; SlaveThreadControllerInterface *slave = this->slaveThreadList[i];
slave->setMasterController(this); if(slave != NULL) {
slave->setMasterController(this);
}
} }
} }
} }
@@ -472,7 +481,9 @@ void MasterSlaveThreadController::signalSlaves(void *userdata) {
if(this->slaveThreadList.empty() == false) { if(this->slaveThreadList.empty() == false) {
for(unsigned int i = 0; i < this->slaveThreadList.size(); ++i) { for(unsigned int i = 0; i < this->slaveThreadList.size(); ++i) {
SlaveThreadControllerInterface *slave = this->slaveThreadList[i]; SlaveThreadControllerInterface *slave = this->slaveThreadList[i];
slave->signalSlave(userdata); if(slave != NULL) {
slave->signalSlave(userdata);
}
} }
} }