Browse Source

Merge pull request #7: la_mojo: fix memory leaks and heap corruption

Fixes memory leaks and heap corruption in Projects/la_mojo (Debug86). See PR #7 for details.
pull/8/head
ClaudeAI 22 hours ago
parent
commit
cd74a030af
  1. 2
      lib/App/common/SQLite_dataBinder.cpp
  2. 1
      lib/App/common/deviceNode2D.cpp
  3. 10
      lib/App/common/deviceNodeFactory.cpp
  4. 4
      lib/App/common/kernelModuleFactory.cpp
  5. 1
      lib/App/common/kernelModuleFlyHunter.cpp
  6. 1
      lib/App/common/kernelModuleModemManager.cpp
  7. 1
      lib/App/common/kernelModuleReportBackupper.cpp
  8. 26
      lib/App/common/streamHandler.cpp
  9. 21
      lib/App/mojo/mojoRemoteClient.cpp
  10. 46
      lib/components/base/basicDualThread.cpp
  11. 10
      lib/components/base/basicDualThread.h
  12. 22
      lib/components/base/basicEvent.cpp
  13. 2
      lib/components/base/basicFunctionality.cpp
  14. 30
      lib/components/base/basicThread.cpp
  15. 6
      lib/components/base/basicThread.h
  16. 28
      lib/data/curl/curlRequest.cpp
  17. 3
      lib/data/curl/curlRequestsManager.cpp
  18. 6
      lib/data/media/video/videoAVLIBSource.cpp
  19. 33
      lib/data/protocol/protocol.cpp
  20. 10
      lib/data/protocol/protocolBVMS.cpp
  21. 10
      lib/data/protocol/protocolKAFKA.cpp
  22. 35
      lib/data/sqlite/sqlite_class.cpp
  23. 5
      lib/data/sqlite/sqlite_class.h

2
lib/App/common/SQLite_dataBinder.cpp

@ -962,10 +962,10 @@ bool sqliteDBC::dropSession()
#else
sprintf(tQBuf, "SELECT `ts`, `ts_ms` FROM `events` WHERE `evt_type`=%d ORDER BY `ts` DESC,`ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION);
#endif
m_pDataReports->Query(tQBuf);
#ifdef DEBUG_ALL
LOG(tQBuf);
#endif
// (a duplicate Query() call whose result was never freed was removed here)
result = m_pDataReports->Query(tQBuf);
if (result != NULL)
{

1
lib/App/common/deviceNode2D.cpp

@ -3076,6 +3076,7 @@ void device2DSensor::proceedDBCommands()
m_pDBC->updateLRFUpload(tH->db_id, 1);
m_sGetFileTransfers.helpers.push_back(tH);
};
free(tData); // returned by getLRFUploadData(), owned by the caller
}
else
{

10
lib/App/common/deviceNodeFactory.cpp

@ -1,4 +1,6 @@
#include "deviceNodeFactory.h"
#include <algorithm>
#ifdef USE_MYSQL
#include "MySQL_dataBinder.h"
#else
@ -432,13 +434,19 @@ deviceNode * deviceNodeFactory::createNode(uint32_t id)
#endif
m_vDeviceNodes.push_back(tVal);
m_mDeviceNodesIDS[id] = tVal;
// Node is alive now - it is no longer pending
m_nPendingNodes.erase(std::remove(m_nPendingNodes.begin(), m_nPendingNodes.end(), (int) id), m_nPendingNodes.end());
}
else
{
#ifdef DEBUG_EXTRA
LOG("Node Creation Failure -> Push to Pending Mode!");
#endif
m_nPendingNodes.push_back(id);
// Add only once: this runs on every retry and the list was never trimmed
if (std::find(m_nPendingNodes.begin(), m_nPendingNodes.end(), (int) id) == m_nPendingNodes.end())
{
m_nPendingNodes.push_back(id);
}
m_nPendingInitCounter = FACTORY_NODE_INIT_PENDING_COUNT;
};
return tVal;

4
lib/App/common/kernelModuleFactory.cpp

@ -278,8 +278,10 @@ kernelModule * kernelModuleFactory::createModule(int id)
case KERNEL_MODULE_MODEM_MANAGER: delete((kernelModuleModemManager *) tVal); break;
case KERNEL_MODULE_BOSCH_REST: delete((kernelModuleBosch *) tVal); break;
case KERNEL_MODULE_ATTENTION_CLOUD: delete((kernelAttentionCloud *) tVal); break;
// kernelModule has a virtual destructor: plain delete is correct for any other type
default: delete(tVal); break;
};
delete(tVal);
// (tVal was deleted above - a second delete here was a double free)
tVal = NULL;
LOG_ERROR("Error Starting Module Service!");
};

1
lib/App/common/kernelModuleFlyHunter.cpp

@ -49,6 +49,7 @@ bool kernelModuleFlyHunter::startService()
m_sGCSlocation.lat = tData->lat;
m_sGCSlocation.lon = tData->lon;
m_sGCSlocation.alt = tData->alt;
free(tData); // returned by getFlyHunterData(), owned by the caller
m_bWorkingMode = FH_WM_NONE;
#ifdef SIMULATE_FH

1
lib/App/common/kernelModuleModemManager.cpp

@ -98,6 +98,7 @@ bool kernelModuleModemManager::reloadSettings()
{
moduleActive = false;
}
delete (pList); // list returned by listPhonebook() is owned by the caller
};
return true;
};

1
lib/App/common/kernelModuleReportBackupper.cpp

@ -21,6 +21,7 @@ kernelModuleReportBackupper::kernelModuleReportBackupper(uint32_t moduleID) :
if (tData)
{
memcpy(&m_sBackupSettings, tData, sizeof(DBC_REPORT_BACKUP_SETTINGS));
free(tData); // returned by getReportBackupData(), owned by the caller
}
else
{

26
lib/App/common/streamHandler.cpp

@ -150,6 +150,32 @@ streamHandler::~streamHandler()
closeAll();
#endif // EVE_ENABLE_VIDEO
};
#ifdef EVE_ENABLE_VIDEO
// Encoder thread is stopped at this point - release frames still waiting in the queue
waitThreadEnd(true);
m_mEncodingQueueProtector.lock();
while (! m_qEncodingQueue.empty())
{
ENCODING_BUFFER * tData = m_qEncodingQueue.front();
m_qEncodingQueue.pop();
if (tData)
{
#if defined(DIRECT_MJPEG_COMPOSING) || defined(OPENCV_VIDEO_OUTPUT)
if (tData->data)
{
free(tData->data);
}
#else
if (tData->data[0])
{
free(tData->data[0]);
}
#endif
free(tData);
};
};
m_mEncodingQueueProtector.unlock();
#endif // EVE_ENABLE_VIDEO
}
bool streamHandler::create(dataBinder * ptr, uint32_t stremID)

21
lib/App/mojo/mojoRemoteClient.cpp

@ -54,7 +54,9 @@ mojoRemoteClient::~mojoRemoteClient()
{
for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++)
{
delete(*idx);
// VTR_HELPER is malloc'ed, the helper object inside is new'ed
delete ((*idx)->ptr);
free(*idx);
}
m_vVTRHelpers.clear();
};
@ -137,13 +139,18 @@ protocolPackage * mojoRemoteClient::packetExtractor(U8 * Buffer, size_t BufferSi
tPack->m_nSize = (tPack->m_nSize << 8) | Buffer[7];
tPack->m_nSize = (tPack->m_nSize << 8) | Buffer[8];
tPack->m_nSize = tPack->m_nSize + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE;
tPack->m_pData = (U8 *) malloc(tPack->m_nSize);
if (tPack->m_pData && ( tPack->m_nSize <= BufferSize ) )
tPack->m_pData = NULL;
if (tPack->m_nSize <= BufferSize)
{
tPack->m_pData = (U8 *) malloc(tPack->m_nSize);
}
if (tPack->m_pData)
{
memcpy(tPack->m_pData, Buffer, tPack->m_nSize);
}
else
{
// m_pData was leaked here when the size check failed
free(tPack);
tPack = NULL;
};
@ -263,7 +270,7 @@ bool mojoRemoteClient::packetHandler(protocolPackage * Package)
{
tCMD->DATA = NULL;
tCMD->DATA_SIZE = 0;
if (! m_qCMD->push(tCMD))
if (m_qCMD->push(tCMD) == -1) // push() returns the element ID (may be 0) or -1 on failure
{
free(tCMD);
};
@ -302,7 +309,7 @@ bool mojoRemoteClient::packetHandler(protocolPackage * Package)
free( tCMD->DATA );
*/
// Send Via Queue
if (m_qCMD->push(tCMD))
if (m_qCMD->push(tCMD) != -1) // push() returns the element ID (may be 0) or -1 on failure
{
m_mCurrentVTRProtector.lock();
m_vVTRHelpers.at(0)->ts = time(NULL);
@ -963,7 +970,7 @@ void mojoRemoteClient::m_fThreadProc(void)
{
memcpy(tPack->DATA, m_vVTRHelpers.at(0)->ptr->getVideoInfo(), tPack->DATA_SIZE);
tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
if (m_qCMD->push(tPack))
if (m_qCMD->push(tPack) != -1) // push() returns the element ID (may be 0) or -1 on failure
{
m_vVTRHelpers.at(0)->state = 1;
m_vVTRHelpers.at(0)->ts = cTimeTSeconds;
@ -1188,7 +1195,7 @@ bool mojoRemoteClient::addEvent(EVENT_CONTAINER * Evt)
{
tPack->CMD = ARGUS_DATA_MSG_EVT;
tPack->DATA_SIZE = sizeof(ARGUS_DATA_EVENT);
if (m_qCMD->push(tPack))
if (m_qCMD->push(tPack) != -1) // push() returns the element ID (may be 0) or -1 on failure
{
return true;
}

46
lib/components/base/basicDualThread.cpp

@ -15,14 +15,17 @@ bool basicDualThread::init(void)
m_bThreadAStatus = TH_ARMED;
#if defined(__linux__) || defined(WIN_PTHREAD)
int RetVal = pthread_create(&m_sThreadAHandler, NULL, m_fThreadAEntryPoint, (void *) this);
if (RetVal == -1)
if (RetVal != 0) // pthread_create returns an error number, not -1
{
LOG_ERROR("Thread A Creation Faled.");
m_bThreadAStatus = TH_PRE_INIT;
return false;
};
m_bThreadAJoinable = true;
#else
m_hThreadAHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadAEntryPoint, (LPVOID) this, 0, &m_dwThreadAID);
// TODO: Add thread creation check
m_bThreadAJoinable = true;
#endif
}
else
@ -35,14 +38,17 @@ bool basicDualThread::init(void)
m_bThreadBStatus = TH_ARMED;
#if defined(__linux__) || defined(WIN_PTHREAD)
int RetVal = pthread_create(&m_sThreadBHandler, NULL, m_fThreadBEntryPoint, (void *) this);
if (RetVal == -1)
if (RetVal != 0) // pthread_create returns an error number, not -1
{
LOG_ERROR("Thread B Creation Faled.");
m_bThreadBStatus = TH_PRE_INIT;
return false;
};
m_bThreadBJoinable = true;
#else
m_hThreadBHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadBEntryPoint, (LPVOID) this, 0, &m_dwThreadBID);
// TODO: Add thread creation check
m_bThreadBJoinable = true;
#endif
}
else
@ -123,15 +129,27 @@ DWORD WINAPI basicDualThread::m_fThreadBEntryPoint(LPVOID ptr)
void basicDualThread::waitThreadAEnd(bool Force)
{
if (m_bThreadAStatus > TH_WORKING || m_bThreadAStatus < TH_ARMED)
// A thread that ended by itself (TH_ENDED / TH_EXIT) must still be joined,
// otherwise its stack and descriptor are never released.
if (! m_bThreadAJoinable || m_bThreadAStatus < TH_ARMED)
{
return;
}
if (Force && m_bThreadAStatus < TH_EXIT)
if (Force && m_bThreadAStatus < TH_ENDED)
{
m_bThreadAStatus = TH_ENDED;
}
#if defined(__linux__) || defined(WIN_PTHREAD)
if (pthread_equal(m_sThreadAHandler, pthread_self()))
{
// Called from the thread itself: it cannot wait for / join itself.
pthread_detach(m_sThreadAHandler);
m_bThreadAJoinable = false;
return;
}
#endif
while (m_bThreadAStatus < TH_EXIT)
{
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
@ -139,23 +157,37 @@ void basicDualThread::waitThreadAEnd(bool Force)
#if defined(__linux__) || defined(WIN_PTHREAD)
pthread_join(m_sThreadAHandler, NULL);
m_bThreadAJoinable = false;
#else
WaitForSingleObject(m_hThreadAHandle, 2000);
CloseHandle(m_hThreadAHandle);
m_bThreadAJoinable = false;
#endif
}
void basicDualThread::waitThreadBEnd(bool Force)
{
if (m_bThreadBStatus > TH_WORKING || m_bThreadBStatus < TH_ARMED)
// A thread that ended by itself (TH_ENDED / TH_EXIT) must still be joined,
// otherwise its stack and descriptor are never released.
if (! m_bThreadBJoinable || m_bThreadBStatus < TH_ARMED)
{
return;
}
if (Force && m_bThreadBStatus < TH_EXIT)
if (Force && m_bThreadBStatus < TH_ENDED)
{
m_bThreadBStatus = TH_ENDED;
}
#if defined(__linux__) || defined(WIN_PTHREAD)
if (pthread_equal(m_sThreadBHandler, pthread_self()))
{
// Called from the thread itself: it cannot wait for / join itself.
pthread_detach(m_sThreadBHandler);
m_bThreadBJoinable = false;
return;
}
#endif
while (m_bThreadBStatus < TH_EXIT)
{
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
@ -163,9 +195,11 @@ void basicDualThread::waitThreadBEnd(bool Force)
#if defined(__linux__) || defined(WIN_PTHREAD)
pthread_join(m_sThreadBHandler, NULL);
m_bThreadBJoinable = false;
#else
WaitForSingleObject(m_hThreadBHandle, 2000);
CloseHandle(m_hThreadBHandle);
m_bThreadBJoinable = false;
#endif
}

10
lib/components/base/basicDualThread.h

@ -26,6 +26,10 @@ class basicDualThread
static DWORD WINAPI m_fThreadAEntryPoint(LPVOID lpParameter);
static DWORD WINAPI m_fThreadBEntryPoint(LPVOID lpParameter);
#endif
// true while a created thread has not been joined yet (its stack is only released by pthread_join)
bool m_bThreadAJoinable;
bool m_bThreadBJoinable;
protected:
thredStatus m_bThreadAStatus;
@ -63,8 +67,10 @@ class basicDualThread
inline basicDualThread::basicDualThread()
{
m_pLog = basicLogCollection::getInstance();
m_bThreadAStatus = TH_PRE_INIT;
m_bThreadBStatus = TH_PRE_INIT;
m_bThreadAStatus = TH_PRE_INIT;
m_bThreadBStatus = TH_PRE_INIT;
m_bThreadAJoinable = false;
m_bThreadBJoinable = false;
}
// Destructor implementation

22
lib/components/base/basicEvent.cpp

@ -30,7 +30,7 @@ void utilizeBasicEvent(EVENT_CONTAINER & Evt)
{
case EVT_FH_MISSION_BEGIN:
case EVT_FH_BUSY:
case EVT_FH_CANCEL_ACTION : delete((FH_CTRL_DATA *) Evt.m_pEvtData); break;
case EVT_FH_CANCEL_ACTION : free(Evt.m_pEvtData); break; // allocated with malloc everywhere
case EVT_FH_FLY_TO :
case EVT_FH_WAYPOINT :
case EVT_FH_DEST_REACHED :
@ -40,7 +40,7 @@ void utilizeBasicEvent(EVENT_CONTAINER & Evt)
case EVT_FH_INTERCEPT_BEGIN:
case EVT_FH_INTERCEPT_OK :
case EVT_FH_INTERCEPT_NOK :
case EVT_FH_MISSION_END : delete((FH_MISSION_DATA *) Evt.m_pEvtData); break;
case EVT_FH_MISSION_END : free(Evt.m_pEvtData); break; // allocated with malloc everywhere
case EVT_TARGET_1D:
case EVT_TARGET_2D_WHITE:
case EVT_TARGET_2D_BLACK:
@ -48,7 +48,7 @@ void utilizeBasicEvent(EVENT_CONTAINER & Evt)
case EVT_TARGET_3D_BLACK:
case EVT_CAMERA_MOVE_TO:
{
delete((TARGET_DATA *) Evt.m_pEvtData);
free(Evt.m_pEvtData); // allocated with malloc everywhere (delete was an allocator mismatch)
break;
};
default: free(Evt.m_pEvtData);
@ -75,7 +75,7 @@ void utilizeBasicEvent(EVENT_CONTAINER *& Evt)
{
case EVT_FH_MISSION_BEGIN:
case EVT_FH_BUSY:
case EVT_FH_CANCEL_ACTION : delete((FH_CTRL_DATA *) Evt->m_pEvtData); break;
case EVT_FH_CANCEL_ACTION : free(Evt->m_pEvtData); break; // allocated with malloc everywhere
case EVT_FH_FLY_TO :
case EVT_FH_WAYPOINT :
case EVT_FH_DEST_REACHED :
@ -85,7 +85,7 @@ void utilizeBasicEvent(EVENT_CONTAINER *& Evt)
case EVT_FH_INTERCEPT_BEGIN:
case EVT_FH_INTERCEPT_OK :
case EVT_FH_INTERCEPT_NOK :
case EVT_FH_MISSION_END : delete((FH_MISSION_DATA *) Evt->m_pEvtData); break;
case EVT_FH_MISSION_END : free(Evt->m_pEvtData); break; // allocated with malloc everywhere
case EVT_TARGET_1D:
case EVT_TARGET_2D_WHITE:
case EVT_TARGET_2D_BLACK:
@ -93,7 +93,7 @@ void utilizeBasicEvent(EVENT_CONTAINER *& Evt)
case EVT_TARGET_3D_BLACK:
case EVT_CAMERA_MOVE_TO:
{
delete((TARGET_DATA *) Evt->m_pEvtData);
free(Evt->m_pEvtData); // allocated with malloc everywhere (delete was an allocator mismatch)
break;
};
default: free(Evt->m_pEvtData);
@ -135,9 +135,12 @@ EVENT_CONTAINER * createBasicEvent(uint64_t SRC, basicEventClassID Class, basicE
{
memset(tEvent->m_UUID, 0, 65);
};
tEvent->m_ref_UUID[0] = (char) 0; // malloc'ed struct: was left uninitialised without RefUUID
tEvent->m_db_id = 0;
tEvent->m_ref_id = 0;
if (RefUUID)
{
sprintf(tEvent->m_ref_UUID, "%s", RefUUID);
snprintf(tEvent->m_ref_UUID, sizeof(tEvent->m_ref_UUID), "%s", RefUUID);
};
tEvent->m_eType = Type;
tEvent->m_nDestinationNodeID = 0; // Broadcast
@ -189,9 +192,12 @@ EVENT_CONTAINER * createBasicTargetedEvent(uint64_t SRC, uint64_t DST, basicEven
{
memset(tEvent->m_UUID, 0, 65);
};
tEvent->m_ref_UUID[0] = (char) 0; // malloc'ed struct: was left uninitialised without RefUUID
tEvent->m_db_id = 0;
tEvent->m_ref_id = 0;
if (RefUUID)
{
sprintf(tEvent->m_ref_UUID, "%s", RefUUID);
snprintf(tEvent->m_ref_UUID, sizeof(tEvent->m_ref_UUID), "%s", RefUUID);
};
tEvent->m_eType = Type;
tEvent->m_nDestinationNodeID = DST;

2
lib/components/base/basicFunctionality.cpp

@ -448,7 +448,7 @@ CSTR trim(CSTR val)
}
if ((i > 0) && ((j - i + 1) > 0))
{
memcpy(val, val + i, j - i + 1);
memmove(val, val + i, j - i + 1); // regions overlap: memcpy is undefined here
}
val[j - i + 1] = (char) 0;
};

30
lib/components/base/basicThread.cpp

@ -21,16 +21,19 @@ bool basicThread::init(void)
m_bThreadStatus = TH_ARMED;
#if defined(__linux__) || defined(WIN_PTHREAD)
int RetVal = pthread_create(&m_sThreadHandler, NULL, m_fThreadEntryPoint, (void *) this);
if (RetVal == -1)
if (RetVal != 0) // pthread_create returns an error number, not -1
{
#ifndef BTH_NO_LOGGING
LOG_ERROR("Thread Creation Failed.");
#endif
m_bThreadStatus = TH_PRE_INIT;
return false;
};
m_bThreadJoinable = true;
#else
m_hThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) m_fThreadEntryPoint, (LPVOID) this, 0, &m_dwThreadID);
// TODO: Add thread creation check
m_bThreadJoinable = true;
#endif
}
else
@ -82,15 +85,29 @@ DWORD WINAPI basicThread::m_fThreadEntryPoint(LPVOID ptr)
void basicThread::waitThreadEnd(bool forced)
{
if (m_bThreadStatus > TH_WORKING || m_bThreadStatus < TH_ARMED)
// Nothing to wait for if no thread was created, or it was already joined.
// NOTE: a thread that ended by itself (TH_ENDED / TH_EXIT) must still be joined,
// otherwise its stack and descriptor are never released.
if (! m_bThreadJoinable || m_bThreadStatus < TH_ARMED)
{
return;
}
if (forced && m_bThreadStatus < TH_EXIT)
if (forced && m_bThreadStatus < TH_ENDED)
{
m_bThreadStatus = TH_ENDED;
}
#if defined(__linux__) || defined(WIN_PTHREAD)
if (pthread_equal(m_sThreadHandler, pthread_self()))
{
// Called from the thread itself: it cannot wait for / join itself. Detach so the
// stack is released automatically when the thread procedure returns.
pthread_detach(m_sThreadHandler);
m_bThreadJoinable = false;
return;
}
#endif
while (m_bThreadStatus < TH_EXIT)
{
SLEEP(DEFAULT_THREAD_SLEEP_TIME);
@ -102,6 +119,7 @@ void basicThread::waitThreadEnd(bool forced)
WaitForSingleObject(m_hThreadHandle, DEFAULT_THREAD_SLEEP_TIME);
CloseHandle(m_hThreadHandle);
#endif
m_bThreadJoinable = false;
}
bool basicThread::uninit(void)
@ -122,8 +140,14 @@ bool basicThread::uninit(void)
void basicThread::abort()
{
if (! m_bThreadJoinable)
{
return;
}
pthread_cancel(m_sThreadHandler);
pthread_join(m_sThreadHandler, NULL);
m_bThreadJoinable = false;
m_bThreadStatus = TH_EXIT;
}
#ifndef BTH_NO_LOGGING

6
lib/components/base/basicThread.h

@ -59,6 +59,9 @@ class basicThread
DWORD m_dwThreadID;
static DWORD WINAPI m_fThreadEntryPoint(LPVOID lpParameter);
#endif
// true while a created thread has not been joined yet (its stack is only released by pthread_join)
bool m_bThreadJoinable;
protected:
thredStatus m_bThreadStatus;
@ -102,7 +105,8 @@ inline basicThread::basicThread()
#ifndef BTH_NO_LOGGING
m_pLog = basicLogCollection::getInstance();
#endif
m_bThreadStatus = TH_PRE_INIT;
m_bThreadStatus = TH_PRE_INIT;
m_bThreadJoinable = false;
}
// Destructor implementation

28
lib/data/curl/curlRequest.cpp

@ -21,9 +21,10 @@ curlRequest::curlRequest(CSTR URL, CSTR cntType)
m_nMemorySize = 0;
m_bRequestComplete = false;
m_pURL = (CSTR) malloc(strlen(URL) + 1);
m_cContentType[0] = (char) 0;
if (cntType != NULL)
{
sprintf(m_cContentType, "Content-Type: %s", cntType);
snprintf(m_cContentType, sizeof(m_cContentType), "Content-Type: %s", cntType);
}
if (m_pURL)
{
@ -42,13 +43,15 @@ curlRequest::curlRequest(CSTR Host, CSTR Path, CSTR cntType)
m_pBaseObject = NULL;
m_pBaseParam = NULL;
m_pSignalFunc = NULL;
m_pPostData = NULL; // was uninitialised: destructor freed a garbage pointer
m_pMemoryBuffer = NULL;
m_HeaderList = NULL;
m_nMemorySize = 0;
m_bRequestComplete = false;
m_cContentType[0] = (char) 0;
if (cntType != NULL)
{
sprintf(m_cContentType, "Content-Type: %s", cntType);
snprintf(m_cContentType, sizeof(m_cContentType), "Content-Type: %s", cntType);
}
m_pURL = (CSTR) malloc(strlen(Host) + 1 + strlen(Path));
if (m_pURL)
@ -66,7 +69,17 @@ curlRequest::~curlRequest()
#ifdef DEBUG
#endif
curl_easy_cleanup(m_pCURL);
if (m_pCURL)
{
curl_easy_cleanup(m_pCURL);
m_pCURL = NULL;
}
// The header list is not owned by the easy handle - it must be freed explicitly
if (m_HeaderList)
{
curl_slist_free_all(m_HeaderList);
m_HeaderList = NULL;
}
if (m_pURL)
{
free(m_pURL);
@ -134,7 +147,10 @@ void curlRequest::init_CURL()
m_pCURL = curl_easy_init();
if (m_pCURL != NULL)
{
m_HeaderList = curl_slist_append(m_HeaderList, m_cContentType);
if (m_cContentType[0])
{
m_HeaderList = curl_slist_append(m_HeaderList, m_cContentType);
}
//m_HeaderList = curl_slist_append( m_HeaderList, "Connection: Keep-Alive" );
m_HeaderList = curl_slist_append( m_HeaderList, "accept: application/json" );
// m_HeaderList = curl_slist_append( m_HeaderList, "Connection: Close" );
@ -210,6 +226,10 @@ void curlRequest::setPOST(CSTR postDATA, int size)
{
size = strlen(postDATA);
}
if (m_pPostData)
{
free(m_pPostData);
}
m_pPostData = (CSTR) malloc(size + 1);
if (! m_pPostData)
{

3
lib/data/curl/curlRequestsManager.cpp

@ -221,6 +221,9 @@ bool curlRequestsManager::accomplish(curlRequest * RC)
{
LOG_ERROR(curl_multi_strerror(RetCode));
};
// The manager owns the request: on success it is deleted when completed,
// on failure it must be deleted here (no caller uses it afterwards).
delete (RC);
};
return false;
}

6
lib/data/media/video/videoAVLIBSource.cpp

@ -185,13 +185,12 @@ bool videoAVLIBSource::setFrameAspectRatio(FrameAspectRatio AspectRatio)
bool videoAVLIBSource::_close(void)
{
LOG("CLOSE");
// set Close Status - to prevent continue
m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSED;
if (m_pVideoState == NULL)
{
return true;
}
// set Close Status - to prevent continue
m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSED;
m_pMediaDestination = NULL; //??
//_stop(); //stops the Thread
@ -243,6 +242,7 @@ bool videoAVLIBSource::_close(void)
};
sws_freeContext(m_pVideoState->m_pSwsCtx);
m_pVideoState->m_pSwsCtx = NULL; // _close() runs again from the destructor - avoid double free
return true;
}

33
lib/data/protocol/protocol.cpp

@ -50,6 +50,16 @@ protocolParser::~protocolParser()
};
#endif
*/
// Release packages that were never fetched from the pool
for (packagePoll::iterator idx = m_vPackagePool.begin(); idx != m_vPackagePool.end(); idx++)
{
if (*idx)
{
dataEraser((*idx)->m_pData);
free(*idx);
};
};
m_vPackagePool.clear();
if (m_pParserBuffer != NULL)
{
free(m_pParserBuffer);
@ -201,7 +211,7 @@ bool protocolParser::parse(U8 * Buffer, size_t BufferSize)
#endif
// Note: RetVal is NEGATIVE!!??
m_nParserDataCount += RetVal;
memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
memmove(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
} else if (RetVal == 0) {
protocolPackage * tVal = NULL;
tVal = packetExtractor(m_pParserBuffer, m_nParserDataCount);
@ -215,7 +225,7 @@ bool protocolParser::parse(U8 * Buffer, size_t BufferSize)
// Move unused data toward buffer's begin to parse at next parse attempt
m_nParserDataCount -= tVal->m_nSize;
if (m_nParserDataCount > 0){
memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
memmove(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
} else {
// Clean Buff;
memset(m_pParserBuffer, 0, PROTOCOL_PARSER_BUFFER_SIZE);
@ -232,10 +242,11 @@ bool protocolParser::parse(U8 * Buffer, size_t BufferSize)
#ifdef DEBUG_COMMUNICATION
LOG_ERROR("Pack Pool Overflow!");
#endif
// Delete oldest package in queue
// Delete oldest package in queue (free the removed one, not the newly pooled one)
protocolPackage * tOld = m_vPackagePool.front();
m_vPackagePool.erase(m_vPackagePool.begin());
dataEraser(tVal->m_pData);
free(tVal);
dataEraser(tOld->m_pData);
free(tOld);
};
} else {
#ifdef DEBUG_COMMUNICATION
@ -305,7 +316,7 @@ void protocolParser::m_fParserThreadProc(void)
{
// Note: RetVal is NEGATIVE!!??
m_nParserDataCount += RetVal;
memcpy(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
memmove(m_pParserBuffer, m_pParserBuffer - RetVal, m_nParserDataCount);
}
else if (RetVal == 0)
{
@ -321,17 +332,18 @@ void protocolParser::m_fParserThreadProc(void)
#endif
// Move unused data toward buffer's begin to parse at next parse attempt
m_nParserDataCount -= tVal->m_nSize;
memcpy(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
memmove(m_pParserBuffer, m_pParserBuffer + tVal->m_nSize, m_nParserDataCount);
// In Case implementation of packetHandler - handler must return !true!
if (! packetHandler(tVal))
{
m_vPackagePool.push_back(tVal);
if (m_vPackagePool.size() > PROTO_QUEUE_MAX_PACKAGES)
{
// Delete oldest package in queue
// Delete oldest package in queue (free the removed one, not the newly pooled one)
protocolPackage * tOld = m_vPackagePool.front();
m_vPackagePool.erase(m_vPackagePool.begin());
dataEraser(tVal->m_pData);
free(tVal);
dataEraser(tOld->m_pData);
free(tOld);
};
}
else
@ -426,6 +438,7 @@ bool protocolParser::getPackage(U8 ** Package, size_t * Size)
memcpy(*Package, tVal->m_pData, tVal->m_nSize);
*Size = tVal->m_nSize;
m_vPackagePool.erase(m_vPackagePool.begin());
dataEraser(tVal->m_pData);
free(tVal);
m_mBufferProtectorMutex.unlock();
return true;

10
lib/data/protocol/protocolBVMS.cpp

@ -36,7 +36,15 @@ CSTR protocolBVMS::buildURL(CSTR templ, int dim, CSTR devID){
CSTR protocolBVMS::buildUniqID(void){
CSTR RetVal;
RetVal = (CSTR) malloc(64);
sprintf(RetVal, "%s", MD5String(getSHTimeStamp()));
if (RetVal)
{
// both getSHTimeStamp() and MD5String() return malloc'ed strings - free them
CSTR tTS = getSHTimeStamp();
CSTR tMD5 = tTS ? MD5String(tTS) : NULL;
snprintf(RetVal, 64, "%s", tMD5 ? tMD5 : "");
free(tMD5);
free(tTS);
};
return RetVal;
}

10
lib/data/protocol/protocolKAFKA.cpp

@ -141,6 +141,7 @@ protocolKAFKA::protocolKAFKA(KAFKA_SETTINGS * Settings)
{
m_pLog = basicLogCollection::getInstance();
n_pProducer = NULL;
m_pConsumer = NULL;
// m_pServerIP = NULL;
m_pSettings = Settings;
// memset( m_pKafkaBrokerSTR, 0 , sizeof( m_pKafkaBrokerSTR ));
@ -212,8 +213,15 @@ protocolKAFKA::~protocolKAFKA()
LOG_ERROR("Some messages are not delivered!");
}
delete n_pProducer;
n_pProducer = NULL;
// if( n_pProducerTopic!= NULL ) delete n_pProducerTopic;
};
// The consumer handle (with its librdkafka threads and buffers) was never released
if (m_pConsumer != NULL)
{
delete m_pConsumer;
m_pConsumer = NULL;
};
// if( m_pScannerName != NULL ) free(m_pScannerName);
// if( m_pServerIP != NULL ) free(m_pServerIP);
// if( m_pAccessURL != NULL ) free(m_pAccessURL);
@ -548,6 +556,7 @@ bool protocolKAFKA::init()
if (tGlobalConf->set("enable.partition.eof", "true", tBuffer) != RdKafka::Conf::CONF_OK)
{
delete(n_pProducer);
n_pProducer = NULL; // destructor would flush/delete it again
delete(tGlobalConf);
LOG_ERROR(tBuffer.c_str());
return false;
@ -562,6 +571,7 @@ bool protocolKAFKA::init()
if (m_pConsumer == NULL)
{
delete(n_pProducer);
n_pProducer = NULL; // destructor would flush/delete it again
delete(tGlobalConf);
LOG_ERROR("Failed to create consumer:");
LOG_ERROR(tBuffer.c_str());

35
lib/data/sqlite/sqlite_class.cpp

@ -166,8 +166,16 @@ bool SQLite_Connector::Connect(CSTR FileName, int AUX_FLAGS, bool Persistent)
#endif
if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL))
{
// Wait for locks held by other connections instead of failing immediately
sqlite3_busy_timeout(m_pDBLink, SQLITE_BUSY_TIMEOUT_MS);
m_bIsConnected = true;
}
else if (m_pDBLink != NULL)
{
// sqlite3_open_v2 allocates a handle even on failure
sqlite3_close(m_pDBLink);
m_pDBLink = NULL;
}
#ifdef DEBUG_ALL
LOG("Done!");
#endif
@ -188,11 +196,20 @@ bool SQLite_Connector::ReConnect(void)
{
return false;
}
m_pDBLink = NULL;
}
if (SQLITE_OK == sqlite3_open_v2(m_pDBName, &m_pDBLink, m_nFLAGS, NULL))
{
// Wait for locks held by other connections instead of failing immediately
sqlite3_busy_timeout(m_pDBLink, SQLITE_BUSY_TIMEOUT_MS);
return true;
}
if (m_pDBLink != NULL)
{
// sqlite3_open_v2 allocates a handle even on failure
sqlite3_close(m_pDBLink);
m_pDBLink = NULL;
}
return false;
}
@ -289,7 +306,10 @@ AAssoc * SQLite_Connector::Query(CSTR QS, bool useCache)
LOG(sqlite3_expanded_sql(query));
#endif
RetVal = new AAssoc();
while (sqlite3_step(query) != SQLITE_DONE)
int stepRC;
// Only SQLITE_ROW carries data. Looping until SQLITE_DONE spins forever (allocating a new
// row every iteration) when step returns SQLITE_BUSY / SQLITE_ERROR / SQLITE_MISUSE.
while ((stepRC = sqlite3_step(query)) == SQLITE_ROW)
{
string Key;
string Val;
@ -307,6 +327,7 @@ AAssoc * SQLite_Connector::Query(CSTR QS, bool useCache)
case SQLITE_INTEGER: Val = std::string((char *) sqlite3_column_text(query, i)); break;
case SQLITE_FLOAT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
case SQLITE3_TEXT : Val = std::string((char *) sqlite3_column_text(query, i)); break;
default : Val.clear(); break; // NULL / BLOB: don't reuse previous column's value
};
(*tElement)[Key] = Val;
};
@ -314,6 +335,12 @@ AAssoc * SQLite_Connector::Query(CSTR QS, bool useCache)
};
};
};
if (stepRC != SQLITE_DONE)
{
char tB[256];
snprintf(tB, sizeof(tB), "Query step failed (%d): %s", stepRC, sqlite3_errmsg(m_pDBLink));
LOG_ERROR(tB);
};
sqlite3_finalize(query);
@ -370,7 +397,7 @@ CSTR SQLite_Connector::escapeBinary(uint8_t * value, uint32_t Size)
if (m_bIsConnected)
{
int v_len = Size;
CSTR result = (CSTR) malloc(3 * v_len);
CSTR result = (CSTR) malloc(3 * v_len + 1);
if (result)
{
int idx = 0;
@ -386,14 +413,14 @@ CSTR SQLite_Connector::escapeBinary(uint8_t * value, uint32_t Size)
{
char tB[4];
snprintf(tB, 4, "#%02X", value[i]);
strncat(result + idx, tB, 3);
memcpy(result + idx, tB, 3); // result is not zero-terminated: strncat was undefined
idx += 3;
};
};
buffer = (CSTR) malloc(idx + 1);
if (buffer)
{
strncat(buffer, result, idx);
memcpy(buffer, result, idx);
buffer[idx] = (char) 0;
};
free(result);

5
lib/data/sqlite/sqlite_class.h

@ -7,6 +7,11 @@
// #include <mysql/mysql.h>
#include <sqlite3.h>
#ifndef SQLITE_BUSY_TIMEOUT_MS
// How long a statement waits for a lock held by another connection before failing with SQLITE_BUSY
#define SQLITE_BUSY_TIMEOUT_MS 2000
#endif
#ifdef DEBUG_EXTRA
#define DEBUG_SQLITE_QUERIES
#endif

Loading…
Cancel
Save