 la_mojo: fix memory leaks and heap corruption
Continuous leaks:
- protocolParser: on package-pool overflow the oldest package was removed
but never freed while the newest (still pooled) one was freed. Every
packet a handler does not consume (e.g. other sensors on a shared bus)
leaked once the pool was full. Free the removed package, free pooled
packages in the destructor and getPackage(), use memmove for the
overlapping buffer shifts.
- SQLite_Connector::Query: loop only while sqlite3_step() returns
SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on
SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed
open, fix escapeBinary() writing into uninitialised buffers.
- basicThread/basicDualThread: threads that ended by themselves were never
joined, leaking their stacks on every stream drop/restart. Always join
once (detach when called from the thread itself); check pthread_create
result correctly.
- curlRequest: free the header list; initialise m_pPostData and the
content type; curlRequestsManager deletes requests it cannot queue.
- kernelModuleModemManager: free the phonebook ID list on every reload.
- deviceNodeFactory: do not append the same pending node every cycle.
- streamHandler: free frames left in the encoding queue on destruction.
Heap corruption:
- kernelModuleFactory: module that failed to start was deleted twice.
- mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a
bool; compare with -1. Fix packetExtractor and destructor leaks.
- utilizeBasicEvent: payloads are malloc'ed, release with free().
- videoAVLIBSource: avoid sws context double free, NULL check order.
- trim(): memmove for overlapping copy.
Smaller leaks: dropSession duplicate query, LRF upload data, report
backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
1 day ago |
|
#include "protocolBVMS.h"
#include "../../alg/generic/md5.h"
#define LOG(X) m_pLog->Log("PBVMS", this, X)
#define LOG_ERROR(X) m_pLog->Log("PBVMS", this, _LOG_ERROR, X)
CSTR systemStatusMode[] = {"Operational", "Degraded"};CSTR systemFaultCode[] = {"Critical", "Warning", "Info"};
CSTR getSHTimeStamp(void){ CSTR RetVal;#ifdef __linux__
RetVal = (CSTR) malloc(64); if (RetVal != NULL){ sprintf(RetVal, "%llu", getTimeValue()); };#else
RetVal = (CSTR) malloc(64); if (RetVal != NULL){ sprintf(RetVal, "%I64d", getTimeValue()); };#endif
return RetVal;}
CSTR protocolBVMS::buildURL(CSTR templ, int dim, CSTR devID){ CSTR RetVal; char tpl[256]; RetVal = (CSTR) malloc(256); sprintf(tpl, "http://%s:%d%s", m_pServerIP, m_nServerPort, templ); sprintf(RetVal, tpl, dim, devID); return RetVal;}
CSTR protocolBVMS::buildUniqID(void){ CSTR RetVal; RetVal = (CSTR) malloc(64); 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;}
protocolBVMS::protocolBVMS( CSTR server, uint16_t port ){ m_pServerIP = strdup( server ); m_nServerPort = port;}
/*
1.2 GHzNumber of CoresQuad-CoreTypeTegra 3Processor NumberT30SLManufacturerNVIDIA
Verify that you have the following installed on your system Ubuntu 16.04 LTS (with GCC 5.4) NVIDIA Display Driver ver. 375 NVIDIA VideoSDK 7.1 cuDNN 6 & TensorRT 2.1 NVIDIA CUDA 8.0 Download the DeepStream 1.0 tar file (below) Extract the package at the location of your choice Follow the usage instructions from the Deepstream_User_guide.pdf in the downloaded package.
*/
protocolBVMS::~protocolBVMS(){ //if (m_pScannerName != NULL)
//{
// free(m_pScannerName);
//}
//if (m_pServerIP != NULL)
//{
// free(m_pServerIP);
//}
}
void protocolBVMS::setScannerName(CSTR Name){ //sprintf(m_pScannerName, "%s", Name);
}
void protocolBVMS::setProtocolType(bool DeviceIs3D){// if (DeviceIs3D)
// {
// m_nScannerDimentions = 3;
// }
// else
// {
// m_nScannerDimentions = 2;
// };
}
bool protocolBVMS::buildStatusPackage(unsigned char * Buffer, int & BufferSize, bool Status){ if (Buffer != NULL) { CSTR tBuf; tBuf = getSHTimeStamp(); if (tBuf != NULL) { if (Status) { // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[0], tBuf);
} else { // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[1], tBuf);
}; BufferSize = strlen((char *) Buffer); free(tBuf); return true; }; }; return false;}
bool protocolBVMS::buildFaultPackage(unsigned char * Buffer, int & BufferSize, unsigned char Type, CSTR Description){ if (Buffer != NULL) { CSTR tBuf; tBuf = getSHTimeStamp(); if (tBuf != NULL) { // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_FAULT, systemFaultCode[Type], tBuf, Description);
BufferSize = strlen((char *) Buffer); free(tBuf); return true; }; }; return false;}/*
bool protocolBVMS::buildEvent2DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, int Angle, int Range, int Width){ if (Buffer != NULL) { CSTR tBuf; tBuf = getSHTimeStamp(); if (tBuf != NULL) { // sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_EVT2D, msgUID, tBuf, targetID, Angle, Range, Width);
BufferSize = strlen((char *) Buffer); free(tBuf); return true; }; }; return false;}
bool protocolBVMS::buildEvent3DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, int Angle1, int Angle2, int Range){ if (Buffer != NULL) { CSTR tBuf; tBuf = getSHTimeStamp(); if (tBuf != NULL) { //sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_EVT3D, msgUID, tBuf, targetID, Angle1, Angle2, Range);
BufferSize = strlen((char *) Buffer); free(tBuf); return true; }; }; return false;}*/
bool protocolBVMS::buildClockSyncPackage(unsigned char * Buffer, int & BufferSize){ if (Buffer != NULL) { CSTR tBuf; tBuf = getSHTimeStamp(); if (tBuf != NULL) { //sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC, tBuf);
BufferSize = strlen((char *) Buffer); free(tBuf); return true; }; }; return false;}
bool protocolBVMS::buildCallBackPackage(unsigned char * Buffer, int & BufferSize, CSTR CallBackPath){ if (Buffer != NULL) { //sprintf((char *) (Buffer + BufferSize), PACKAGE_ENVELOPE_TEMPLATE_REG_CALL, CallBackPath);
BufferSize = strlen((char *) Buffer); return true; }; return false;}/*
bool protocolBVMS::Alarm2D(int Angle, int Range, int Width, int ID){ bool RetVal = false; char Buffer[1024]; int BufferSize = 0; CSTR URL; CSTR UID; if (! m_bEnabled) { return false; } UID = buildUniqID(); if (UID != NULL) { //URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
RetVal = buildEvent2DPackage((unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width); if (URL != NULL) { RetVal = issuePost(URL, Buffer, BufferSize); free(URL); }; free(UID); }; return RetVal;}
bool protocolBVMS::Alarm3D(int Angle1, int Angle2, int Range, int ID){ bool RetVal = false; char Buffer[1024]; int BufferSize = 0; CSTR URL; CSTR UID; if (! m_bEnabled) { return false; } UID = buildUniqID(); if (UID != NULL) { //URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
LOG(URL); RetVal = buildEvent3DPackage((unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range); if (URL != NULL) { RetVal = issuePost(URL, Buffer, BufferSize); free(URL); }; free(UID); }; return RetVal;}*/bool protocolBVMS::VisibilityChanged(bool Worsened){ bool RetVal = false; char Buffer[1024]; int BufferSize = 0; CSTR URL; //URL = buildURL(SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName);
RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, ! Worsened); if (URL != NULL) { RetVal = issuePost(URL, Buffer, BufferSize); free(URL); }; return RetVal;}
bool protocolBVMS::StatusUpdate(void){ bool RetVal = false; char Buffer[1024]; int BufferSize = 0; CSTR URL; //if (! m_bEnabled)
//{
// return false;
//}
//URL = buildURL(SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName);
RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, m_bCurrentStatus); if (URL != NULL) { RetVal = issuePost(URL, Buffer, BufferSize); free(URL); }; return RetVal;}
bool protocolBVMS::ErrorReporting(int lvl, CSTR Descr){ bool RetVal = false; char Buffer[1024]; int BufferSize = 0; CSTR URL; //if (! m_bEnabled)
//{
// return false;
//}
//URL = buildURL(SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName);
RetVal = buildFaultPackage((unsigned char *) Buffer, BufferSize, lvl, Descr); if (URL != NULL) { RetVal = issuePost(URL, Buffer, BufferSize); free(URL); }; return RetVal;}
bool protocolBVMS::StartTimeSynch(void){ bool RetVal = false; char Buffer[1024]; int BufferSize = 0; CSTR URL = NULL; //if (! m_bEnabled)
//{
// return false;
//}
//URL = buildURL(SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName);
RetVal = buildClockSyncPackage((unsigned char *) Buffer, BufferSize); if (URL != NULL) { RetVal = issuePost(URL, Buffer, BufferSize); free(URL); }; return RetVal;}
bool protocolBVMS::packetHandler(protocolPackage * Package){ // LOG("PACKET HANDLER");
//if (! m_bEnabled)
//{
// return false;
//}
if (Package == NULL) { return false; } if (Package->m_nSize == 0) { return false; } if (Package->m_pData == NULL) { return false; } if (Package->m_nCode != 200) { return false; } /*
try { XMLNode node1 = XMLNode::parseString((const char *) Package->m_pData, "root"); if (node1.isEmpty()) { // char BUBU[128];
// sprintf( BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element.", strlen(pPackage), Package->m_nSize );
// LOG_ERROR(BUBU);
} else { LOG(node1.getName()); if (strstr(node1.getName(), "ClockSync") != NULL) {#ifdef __linux__
unsigned long long t0, t1, t2, t3;#else
unsigned __int64 t0, t1, t2, t3;#endif
XMLNode node3 = node1.getChildNode("t0", 0); XMLNode node4 = node1.getChildNode("t1", 0); XMLNode node5 = node1.getChildNode("t2", 0); t3 = getTimeValue();#ifdef __linux__
t0 = atoll(node3.getText(0)); t1 = atoll(node4.getText(0)); t2 = atoll(node5.getText(0));#else
t0 = _atoi64(node3.getText(0)); t1 = _atoi64(node4.getText(0)); t2 = _atoi64(node5.getText(0));#endif
int offset = ((t1 - t0) + (t2 - t3)) / 2; int latency = (t3 - t0) - (t2 - t1); int jagajaga = offset + latency; // if( 0 & correctSHTime( jagajaga ) ){
// LOG("Time is Synced");
// g_iTimeDiff = 0;
// } else {
// LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
correctSoftTime(jagajaga); //};
}; }; } catch (exception e) { }; */ return true;}
#undef LOG
#undef LOG_ERROR
|