Skip to content

Commit ac77f39

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

File tree

12 files changed

+307
-61
lines changed

12 files changed

+307
-61
lines changed

source/bulkdata/datamodel.c

Lines changed: 44 additions & 6 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
{
5859
pthread_mutex_lock(&rpMutex);
60+
shouldContinue = !stopProcessing;
61+
if(!shouldContinue)
62+
{
63+
pthread_mutex_unlock(&rpMutex);
64+
break;
65+
}
5966
T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__);
60-
pthread_cond_wait(&rpCond, &rpMutex);
67+
while(t2_queue_count(rpQueue) == 0 && shouldContinue)
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)
@@ -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 && shouldContinue)
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 && shouldContinue)
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,13 +310,16 @@ 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
{
316+
pthread_mutex_unlock(&rpMutex);
280317
t2_queue_push(rpMsgPkgQueue, (void *)msgpack);
281318
pthread_cond_signal(&msg_Cond);
282319
}
283320
else
284321
{
322+
pthread_mutex_unlock(&rpMutex);
285323
free(msgpack->msgpack_blob);
286324
free(msgpack);
287325
T2Error("Datamodel not initialized, dropping request \n");

source/bulkdata/profile.c

Lines changed: 38 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,21 @@ 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)
565+
{
566+
fclose(urandom);
567+
}
568+
maxuploadinmilliSec = (unsigned int)(time(0) % (profile->maxUploadLatency - 1));
569+
}
554570
maxuploadinSec = (maxuploadinmilliSec + 1) / 1000;
555571
}
556572
if( strcmp(profile->protocol, "HTTP") == 0 || strcmp(profile->protocol, "RBUS_METHOD") == 0 )
@@ -564,12 +580,20 @@ static void* CollectAndReport(void* data)
564580
pthread_mutex_lock(&profile->reportMutex);
565581
T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec);
566582
profile->maxlatencyTime.tv_sec += maxuploadinSec;
567-
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
583+
do
584+
{
585+
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
586+
}
587+
while (n == EINTR);
568588
if(n == ETIMEDOUT)
569589
{
570590
T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name);
571591
ret = sendReportOverHTTP(httpUrl, jsonReport, NULL);
572592
}
593+
else if(n == 0)
594+
{
595+
T2Info("Profile : %s signaled before timeout, skipping report upload\n", profile->name);
596+
}
573597
else
574598
{
575599
T2Error("Profile : %s pthread_cond_timedwait ERROR!!!\n", profile->name);
@@ -615,12 +639,20 @@ static void* CollectAndReport(void* data)
615639
pthread_mutex_lock(&profile->reportMutex);
616640
T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec);
617641
profile->maxlatencyTime.tv_sec += maxuploadinSec;
618-
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
642+
do
643+
{
644+
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
645+
}
646+
while (n == EINTR);
619647
if(n == ETIMEDOUT)
620648
{
621649
T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name);
622650
ret = sendReportsOverRBUSMethod(profile->t2RBUSDest->rbusMethodName, profile->t2RBUSDest->rbusMethodParamList, jsonReport);
623651
}
652+
else if(n == 0)
653+
{
654+
T2Info("Profile : %s signaled before timeout, skipping report upload\n", profile->name);
655+
}
624656
else
625657
{
626658
T2Error("Profile : %s pthread_cond_timedwait ERROR!!!\n", profile->name);
@@ -782,9 +814,11 @@ reportThreadEnd :
782814
}
783815
while(profile->enable);
784816
T2Info("%s --out Exiting collect and report Thread\n", __FUNCTION__);
817+
pthread_mutex_unlock(&profile->reuseThreadMutex);
785818
pthread_mutex_lock(&profile->reportInProgressMutex);
786819
profile->reportInProgress = false;
787820
pthread_mutex_unlock(&profile->reportInProgressMutex);
821+
pthread_mutex_lock(&profile->reuseThreadMutex);
788822
profile->threadExists = false;
789823
pthread_mutex_unlock(&profile->reuseThreadMutex);
790824
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: 51 additions & 16 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,14 +242,26 @@ 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));
254+
while(t2_queue_count(eQueue) == 0 && shouldContinue)
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+
246265
if(t2_queue_count(eQueue) > 0)
247266
{
248267
T2Event *event = (T2Event *)t2_queue_pop(eQueue);
@@ -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: 6 additions & 6 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)
791786
{
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)