Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions source/commonlib/telemetry_busmessage_sender.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ static void *bus_handle = NULL;
static bool isRFCT2Enable = false ;
static bool isT2Ready = false;
static bool isRbusEnabled = false ;
static int count = 0;
static pthread_mutex_t initMtx = PTHREAD_MUTEX_INITIALIZER;
static bool isMutexInitialized = false ;

Expand Down Expand Up @@ -85,13 +84,23 @@ static void EVENT_DEBUG(char* format, ...)
logHandle = fopen(SENDER_LOG_FILE, "a+");
if(logHandle)
{
time_t rawtime;
struct tm* timeinfo;
struct timespec ts;
struct tm timeinfo;

time(&rawtime);
timeinfo = localtime(&rawtime);
static char timeBuffer[20] = { '\0' };
strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", timeinfo);
if(clock_gettime(CLOCK_REALTIME, &ts) == -1)
{
fclose(logHandle);
pthread_mutex_unlock(&loggerMutex);
return;
}

char timeBuffer[24] = { '\0' };
long msecs;

localtime_r(&ts.tv_sec, &timeinfo);
msecs = ts.tv_nsec / 1000000;
strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
snprintf(timeBuffer + strlen(timeBuffer), sizeof(timeBuffer) - strlen(timeBuffer), ".%03ld", msecs);
Comment on lines +102 to +103
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The snprintf call builds on top of the existing string in timeBuffer by using strlen(timeBuffer) as the offset. While this works, it's more fragile than necessary. Consider using a single snprintf call with a combined format string, or at minimum verify the return value from strftime to ensure it succeeded before appending.

Suggested change
strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
snprintf(timeBuffer + strlen(timeBuffer), sizeof(timeBuffer) - strlen(timeBuffer), ".%03ld", msecs);
size_t len = strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
if (len > 0 && len < sizeof(timeBuffer)) {
int ret = snprintf(timeBuffer + len, sizeof(timeBuffer) - len, ".%03ld", msecs);
if (ret < 0) {
timeBuffer[len] = '\0';
}
} else {
timeBuffer[0] = '\0';
}

Copilot uses AI. Check for mistakes.
fprintf(logHandle, "%s : ", timeBuffer);
va_list argList;
va_start(argList, format);
Expand Down Expand Up @@ -316,8 +325,9 @@ void *cacheEventToFile(void *arg)
fl.l_len = 0;
fl.l_pid = 0;
FILE *fs = NULL;
char path[100];
pthread_detach(pthread_self());
int ch;
Copy link

Copilot AI Dec 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removal of pthread_detach(pthread_self()) may cause resource leaks. When threads are created via pthread_create at line 730 without the detached attribute, they need to either be joined or explicitly detached to release their resources when they terminate. Since this function is meant to run asynchronously and the calling code doesn't join the thread, the thread should either be created with PTHREAD_CREATE_DETACHED attribute or call pthread_detach(pthread_self()) as was done previously.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please retain the thread attribute to detached.

int count = 0;
EVENT_ERROR("%s:%d, Caching the event to File\n", __func__, __LINE__);
if(telemetry_data == NULL)
{
Expand Down Expand Up @@ -353,12 +363,25 @@ void *cacheEventToFile(void *arg)
EVENT_ERROR("%s: File open error %s\n", __FUNCTION__, T2_CACHE_FILE);
goto unlock;
}
fs = popen ("cat /tmp/t2_caching_file | wc -l", "r");
if(fs != NULL)

fs = fopen(T2_CACHE_FILE, "r");
if (fs != NULL)
{
fgets(path, 100, fs);
count = atoi ( path );
pclose(fs);
while ((ch = fgetc(fs)) != EOF)
{
if (ch == '\n')
{
count++;
}
}

//If the file is not empty and does not contain a newline, call it one line
if (count == 0 && ftell(fs) > 0)
{
count++;
}
fclose(fs);
fs = NULL;
}
if(count < MAX_EVENT_CACHE)
{
Expand Down
Loading