Skip to content

Commit ddb6710

Browse files
author
mtirum011
committed
RDK-60291 [telemetry] RDK Coverity Defect Resolution for Device Management
1 parent a272470 commit ddb6710

File tree

12 files changed

+296
-75
lines changed

12 files changed

+296
-75
lines changed

source/bulkdata/datamodel.c

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,24 @@ static void *process_rp_thread(void *data)
5050
{
5151
(void) data;//To fix compiler warning
5252
cJSON *reportProfiles = NULL;
53+
bool shouldContinue = true;
5354

5455
T2Debug("%s ++in\n", __FUNCTION__);
5556

56-
while(!stopProcessing)
57+
while(shouldContinue)
5758
{
58-
pthread_mutex_lock(&rpMutex);
59-
T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__);
60-
pthread_cond_wait(&rpCond, &rpMutex);
59+
pthread_mutex_lock(&rpMutex);
60+
shouldContinue = !stopProcessing;
61+
if(!shouldContinue)
62+
{
63+
pthread_mutex_unlock(&rpMutex);
64+
break;
65+
}
66+
T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__);
67+
while(t2_queue_count(rpQueue) == 0 && !stopProcessing)
68+
{
69+
pthread_cond_wait(&rpCond, &rpMutex);
70+
}
6171

6272
T2Debug("%s: Received wake up signal \n", __FUNCTION__);
6373
if(t2_queue_count(rpQueue) > 0)
@@ -67,10 +77,10 @@ static void *process_rp_thread(void *data)
6777
{
6878
ReportProfiles_ProcessReportProfilesBlob(reportProfiles, T2_RP);
6979
cJSON_Delete(reportProfiles);
70-
// Unused value reportProfiles
71-
}
80+
// Unused value reportProfiles
81+
}
7282
}
73-
pthread_mutex_unlock(&rpMutex);
83+
pthread_mutex_unlock(&rpMutex);
7484
}
7585
T2Debug("%s --out\n", __FUNCTION__);
7686
return NULL;
@@ -80,14 +90,26 @@ static void *process_tmprp_thread(void *data)
8090
{
8191
(void) data;//To fix compiler warning
8292
cJSON *tmpReportProfiles = NULL;
93+
bool shouldContinue = true;
8394

8495
T2Debug("%s ++in\n", __FUNCTION__);
8596

86-
while(!stopProcessing)
97+
while(!shouldContinue)
8798
{
8899
pthread_mutex_lock(&tmpRpMutex);
100+
pthread_mutex_lock(&rpMutex);
101+
shouldContinue = !stopProcessing;
102+
pthread_mutex_unlock(&rpMutex);
103+
if(!shouldContinue)
104+
{
105+
pthread_mutex_unlock(&tmpRpMutex);
106+
break;
107+
}
89108
T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__);
90-
pthread_cond_wait(&tmpRpCond, &tmpRpMutex);
109+
while(t2_queue_count(tmpRpQueue) == 0 && !stopProcessing)
110+
{
111+
pthread_cond_wait(&tmpRpCond, &tmpRpMutex);
112+
}
91113

92114
T2Debug("%s: Received wake up signal \n", __FUNCTION__);
93115
if(t2_queue_count(tmpRpQueue) > 0)
@@ -110,10 +132,23 @@ static void *process_msg_thread(void *data)
110132
{
111133
(void) data;//To fix compiler warning
112134
struct __msgpack__ *msgpack;
113-
while(!stopProcessing)
135+
bool shouldContinue = true;
136+
137+
while(shouldContinue)
114138
{
115139
pthread_mutex_lock(&rpMsgMutex);
116-
pthread_cond_wait(&msg_Cond, &rpMsgMutex);
140+
pthread_mutex_lock(&rpMutex);
141+
shouldContinue = !stopProcessing;
142+
pthread_mutex_unlock(&rpMutex);
143+
if(!shouldContinue)
144+
{
145+
pthread_mutex_unlock(&rpMsgMutex);
146+
break;
147+
}
148+
while(t2_queue_count(rpMsgPkgQueue) == 0 && !stopProcessing)
149+
{
150+
pthread_cond_wait(&msg_Cond, &rpMsgMutex);
151+
}
117152
if(t2_queue_count(rpMsgPkgQueue) > 0)
118153
{
119154
msgpack = (struct __msgpack__ *)t2_queue_pop(rpMsgPkgQueue);
@@ -275,14 +310,17 @@ T2ERROR datamodel_MsgpackProcessProfile(char *str, int strSize)
275310
msgpack->msgpack_blob = str;
276311
msgpack->msgpack_blob_size = strSize;
277312
pthread_mutex_lock(&rpMsgMutex);
313+
pthread_mutex_lock(&rpMutex);
278314
if (!stopProcessing)
279315
{
280-
t2_queue_push(rpMsgPkgQueue, (void *)msgpack);
316+
pthread_mutex_unlock(&rpMutex);
317+
t2_queue_push(rpMsgPkgQueue, (void *)msgpack);
281318
pthread_cond_signal(&msg_Cond);
282319
}
283320
else
284321
{
285-
free(msgpack->msgpack_blob);
322+
pthread_mutex_unlock(&rpMutex);
323+
free(msgpack->msgpack_blob);
286324
free(msgpack);
287325
T2Error("Datamodel not initialized, dropping request \n");
288326
}

source/bulkdata/profile.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,13 @@ static void* CollectAndReport(void* data)
331331
do
332332
{
333333
T2Info("%s while Loop -- START \n", __FUNCTION__);
334+
// Release reuseThreadMutex before acquiring reportInProgressMutex to maintain lock order
335+
pthread_mutex_unlock(&profile->reuseThreadMutex);
334336
pthread_mutex_lock(&profile->reportInProgressMutex);
335337
profile->reportInProgress = true;
336338
pthread_cond_signal(&profile->reportInProgressCond);
337339
pthread_mutex_unlock(&profile->reportInProgressMutex);
340+
pthread_mutex_lock(&profile->reuseThreadMutex);
338341

339342
int count = profile->grepSeekProfile->execCounter;
340343

@@ -549,8 +552,18 @@ static void* CollectAndReport(void* data)
549552
pthread_cond_init(&profile->reportcond, NULL);
550553
clock_gettime(CLOCK_REALTIME, &profile->currentTime);
551554
profile->maxlatencyTime.tv_sec = profile->currentTime.tv_sec;
552-
srand(time(0)); // Initialise the random number generator
553-
maxuploadinmilliSec = rand() % (profile->maxUploadLatency - 1);
555+
unsigned int random_value = 0;
556+
FILE *urandom = fopen("/dev/urandom", "r");
557+
if(urandom != NULL && fread(&random_value, sizeof(random_value), 1, urandom) == 1)
558+
{
559+
maxuploadinmilliSec = random_value % (profile->maxUploadLatency - 1);
560+
fclose(urandom);
561+
}
562+
else
563+
{
564+
if(urandom != NULL) fclose(urandom);
565+
maxuploadinmilliSec = (unsigned int)(time(0) % (profile->maxUploadLatency - 1));
566+
}
554567
maxuploadinSec = (maxuploadinmilliSec + 1) / 1000;
555568
}
556569
if( strcmp(profile->protocol, "HTTP") == 0 || strcmp(profile->protocol, "RBUS_METHOD") == 0 )
@@ -564,7 +577,9 @@ static void* CollectAndReport(void* data)
564577
pthread_mutex_lock(&profile->reportMutex);
565578
T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec);
566579
profile->maxlatencyTime.tv_sec += maxuploadinSec;
567-
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
580+
do {
581+
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
582+
} while(n != ETIMEDOUT && n != 0);
568583
if(n == ETIMEDOUT)
569584
{
570585
T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name);
@@ -615,7 +630,9 @@ static void* CollectAndReport(void* data)
615630
pthread_mutex_lock(&profile->reportMutex);
616631
T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec);
617632
profile->maxlatencyTime.tv_sec += maxuploadinSec;
618-
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
633+
do {
634+
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
635+
} while(n != ETIMEDOUT && n != 0);
619636
if(n == ETIMEDOUT)
620637
{
621638
T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name);
@@ -782,9 +799,11 @@ reportThreadEnd :
782799
}
783800
while(profile->enable);
784801
T2Info("%s --out Exiting collect and report Thread\n", __FUNCTION__);
802+
pthread_mutex_unlock(&profile->reuseThreadMutex);
785803
pthread_mutex_lock(&profile->reportInProgressMutex);
786804
profile->reportInProgress = false;
787805
pthread_mutex_unlock(&profile->reportInProgressMutex);
806+
pthread_mutex_lock(&profile->reuseThreadMutex);
788807
profile->threadExists = false;
789808
pthread_mutex_unlock(&profile->reuseThreadMutex);
790809
pthread_mutex_destroy(&profile->reuseThreadMutex);

source/bulkdata/reportprofiles.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,17 @@ void ReportProfiles_ProcessReportProfilesBlob(cJSON *profiles_root, bool rprofil
946946

947947
// Delete profiles not present in the new profile list
948948
char *profileNameKey = NULL;
949-
int count = hash_map_count(profileHashMap) - 1;
949+
uint32_t hashmap_count = hash_map_count(profileHashMap);
950+
int count;
951+
if (hashmap_count == 0 || hashmap_count == UINT32_MAX)
952+
{
953+
count = -1;
954+
}
955+
else
956+
{
957+
count = (int)(hashmap_count - 1);
958+
}
959+
950960
const char *DirPath = NULL;
951961

952962
if (rprofiletypes == T2_RP)
@@ -1309,6 +1319,7 @@ int __ReportProfiles_ProcessReportProfilesMsgPackBlob(void *msgpack, bool checkP
13091319
return T2ERROR_PROFILE_NOT_FOUND;
13101320
}
13111321
hash_map_t *profileHashMap;
1322+
uint32_t hashmap_count;
13121323
int count;
13131324
char *profileNameKey = NULL;
13141325
int profileIndex;
@@ -1325,7 +1336,15 @@ int __ReportProfiles_ProcessReportProfilesMsgPackBlob(void *msgpack, bool checkP
13251336
}
13261337

13271338
/* Delete profiles not present in the new profile list */
1328-
count = hash_map_count(profileHashMap) - 1;
1339+
hashmap_count = hash_map_count(profileHashMap);
1340+
if (hashmap_count == 0 || hashmap_count == UINT32_MAX)
1341+
{
1342+
count = -1;
1343+
}
1344+
else
1345+
{
1346+
count = (int)(hashmap_count - 1);
1347+
}
13291348
while(count >= 0)
13301349
{
13311350
profile_found_flag = false;

source/bulkdata/t2eventreceiver.c

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,14 @@ void T2ER_Push(char* eventName, char* eventValue)
204204
event->value = strdup(eventValue);
205205
T2Debug("Adding eventName : %s eventValue : %s to t2event queue\n", event->name, event->value);
206206
t2_queue_push(eQueue, (void *) event);
207-
if(!stopDispatchThread)
207+
208+
bool shouldSignal = false;
209+
if(pthread_mutex_lock(&sTDMutex) == 0)
210+
{
211+
shouldSignal = !stopDispatchThread;
212+
pthread_mutex_unlock(&sTDMutex);
213+
}
214+
if(shouldSignal)
208215
{
209216
ret = pthread_cond_signal(&erCond);
210217
if(ret != 0) // pthread_cond _signal failed so after unlocking the mutex it will return
@@ -235,15 +242,27 @@ void* T2ER_EventDispatchThread(void *arg)
235242
(void) arg; // To avoid compiler warning
236243
T2Debug("%s ++in\n", __FUNCTION__);
237244
Vector *profileList = NULL;
238-
while(!stopDispatchThread)
245+
bool shouldContinue = true;
246+
while(shouldContinue)
239247
{
240248
if(pthread_mutex_lock(&erMutex) != 0) // mutex lock failed, without locking eQueue shouldn't be accessed
241249
{
242250
T2Error("%s pthread_mutex_lock for erMutex failed\n", __FUNCTION__);
243251
return NULL;
244252
}
245253
T2Debug("Checking for events in event queue , event count = %d\n", t2_queue_count(eQueue));
246-
if(t2_queue_count(eQueue) > 0)
254+
while(t2_queue_count(eQueue) == 0 && !stopDispatchThread)
255+
{
256+
T2Debug("Event Queue size is 0, Waiting events from T2ER_Push\n");
257+
int ret = pthread_cond_wait(&erCond, &erMutex);
258+
if(ret != 0) // pthread cond wait failed return after unlock
259+
{
260+
T2Error("%s pthread_cond_wait failed with error code: %d\n", __FUNCTION__, ret);
261+
}
262+
T2Debug("Received signal from T2ER_Push\n");
263+
}
264+
265+
if(t2_queue_count(eQueue) > 0)
247266
{
248267
T2Event *event = (T2Event *)t2_queue_pop(eQueue);
249268
if(event == NULL)
@@ -280,18 +299,21 @@ void* T2ER_EventDispatchThread(void *arg)
280299
}
281300
else
282301
{
283-
T2Debug("Event Queue size is 0, Waiting events from T2ER_Push\n");
284-
int ret = pthread_cond_wait(&erCond, &erMutex);
285-
if(ret != 0) // pthread cond wait failed return after unlock
286-
{
287-
T2Error("%s pthread_cond_wait failed with error code: %d\n", __FUNCTION__, ret);
288-
}
289302
if(pthread_mutex_unlock(&erMutex) != 0)
290303
{
291304
T2Error("%s pthread_mutex_unlock for erMutex failed\n", __FUNCTION__);
292305
return NULL;
293306
}
294-
T2Debug("Received signal from T2ER_Push\n");
307+
}
308+
// Check if thread should continue
309+
if(pthread_mutex_lock(&sTDMutex) == 0)
310+
{
311+
shouldContinue = !stopDispatchThread;
312+
pthread_mutex_unlock(&sTDMutex);
313+
}
314+
else
315+
{
316+
shouldContinue = false; // Exit on lock failure
295317
}
296318
}
297319
T2Debug("%s --out\n", __FUNCTION__);
@@ -345,7 +367,10 @@ T2ERROR T2ER_Init()
345367
registerForTelemetryEvents(T2ER_PushDataWithDelim);
346368
}
347369

348-
system("touch /tmp/.t2ReadyToReceiveEvents");
370+
if (system("touch /tmp/.t2ReadyToReceiveEvents") != 0)
371+
{
372+
T2Error("Failed to create /tmp/.t2ReadyToReceiveEvents flag file \n");
373+
}
349374
setT2EventReceiveState(T2_STATE_COMPONENT_READY);
350375
T2Info("T2 is now Ready to Recieve Events\n");
351376

@@ -500,14 +525,16 @@ void T2ER_Uninit()
500525
}
501526
EREnabled = false;
502527

528+
pthread_t threadToJoin;
529+
if(pthread_mutex_lock(&sTDMutex) != 0) // mutex lock failed so return from T2ER_Uninit
530+
{
531+
T2Error("%s pthread_mutex_lock for sTDMutex failed\n", __FUNCTION__);
532+
return;
533+
}
503534
if(!stopDispatchThread)
504535
{
505-
if(pthread_mutex_lock(&sTDMutex) != 0) // mutex lock failed so return from T2ER_Uninit
506-
{
507-
T2Error("%s pthread_mutex_lock for sTDMutex failed\n", __FUNCTION__);
508-
return;
509-
}
510536
stopDispatchThread = true;
537+
threadToJoin = erThread; // Save thread handle while holding lock
511538
if(pthread_mutex_unlock(&sTDMutex) != 0) //mutex unlock failed so return from T2ER_Uninit
512539
{
513540
T2Error("%s pthread_mutex_unlock for sTDMutex failed\n", __FUNCTION__);
@@ -530,7 +557,7 @@ void T2ER_Uninit()
530557
return;
531558
}
532559

533-
if(pthread_join(erThread, NULL) != 0)
560+
if(pthread_join(threadToJoin, NULL) != 0)
534561
{
535562
T2Error("%s erThread join failed\n", __FUNCTION__);
536563
}
@@ -551,6 +578,14 @@ void T2ER_Uninit()
551578
return;
552579
}
553580
}
581+
else
582+
{
583+
if(pthread_mutex_unlock(&sTDMutex) != 0)
584+
{
585+
T2Error("%s pthread_mutex_unlock for sTDMutex failed\n", __FUNCTION__);
586+
return;
587+
}
588+
}
554589
T2Debug("T2ER Event Dispatch Thread successfully terminated\n");
555590
t2_queue_destroy(eQueue, freeT2Event);
556591
eQueue = NULL;

source/commonlib/telemetry_busmessage_sender.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ void t2_uninit(void)
755755
T2ERROR t2_event_s(const char* marker, const char* value)
756756
{
757757

758-
int ret;
758+
int ret = -1;
759759
T2ERROR retStatus = T2ERROR_FAILURE ;
760760
EVENT_DEBUG("%s ++in\n", __FUNCTION__);
761761
char* strvalue = NULL;
@@ -782,13 +782,13 @@ T2ERROR t2_event_s(const char* marker, const char* value)
782782
return T2ERROR_SUCCESS;
783783
}
784784
strvalue = strdup(value);
785-
if( strvalue[strlen(strvalue) - 1] == '\n' )
786-
{
787-
strvalue[strlen(strvalue) - 1] = '\0';
788-
}
789-
ret = report_or_cache_data(strvalue, marker);
790785
if(strvalue != NULL)
791-
{
786+
{
787+
if( strvalue[strlen(strvalue) - 1] == '\n' )
788+
{
789+
strvalue[strlen(strvalue) - 1] = '\0';
790+
}
791+
ret = report_or_cache_data(strvalue, marker);
792792
free(strvalue);
793793
}
794794
if(ret != -1)

0 commit comments

Comments
 (0)