Skip to content

Commit b9475ed

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

File tree

12 files changed

+176
-48
lines changed

12 files changed

+176
-48
lines changed

source/bulkdata/datamodel.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ static void *process_rp_thread(void *data)
5757
{
5858
pthread_mutex_lock(&rpMutex);
5959
T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__);
60-
pthread_cond_wait(&rpCond, &rpMutex);
60+
while(t2_queue_count(rpQueue) == 0 && !stopProcessing)
61+
{
62+
pthread_cond_wait(&rpCond, &rpMutex);
63+
}
6164

6265
T2Debug("%s: Received wake up signal \n", __FUNCTION__);
6366
if(t2_queue_count(rpQueue) > 0)
@@ -87,7 +90,10 @@ static void *process_tmprp_thread(void *data)
8790
{
8891
pthread_mutex_lock(&tmpRpMutex);
8992
T2Info("%s: Waiting for event from tr-181 \n", __FUNCTION__);
90-
pthread_cond_wait(&tmpRpCond, &tmpRpMutex);
93+
while(t2_queue_count(tmpRpQueue) == 0 && !stopProcessing)
94+
{
95+
pthread_cond_wait(&tmpRpCond, &tmpRpMutex);
96+
}
9197

9298
T2Debug("%s: Received wake up signal \n", __FUNCTION__);
9399
if(t2_queue_count(tmpRpQueue) > 0)
@@ -113,7 +119,10 @@ static void *process_msg_thread(void *data)
113119
while(!stopProcessing)
114120
{
115121
pthread_mutex_lock(&rpMsgMutex);
116-
pthread_cond_wait(&msg_Cond, &rpMsgMutex);
122+
while(t2_queue_count(rpMsgPkgQueue) == 0 && !stopProcessing)
123+
{
124+
pthread_cond_wait(&msg_Cond, &rpMsgMutex);
125+
}
117126
if(t2_queue_count(rpMsgPkgQueue) > 0)
118127
{
119128
msgpack = (struct __msgpack__ *)t2_queue_pop(rpMsgPkgQueue);
@@ -274,6 +283,7 @@ T2ERROR datamodel_MsgpackProcessProfile(char *str, int strSize)
274283

275284
msgpack->msgpack_blob = str;
276285
msgpack->msgpack_blob_size = strSize;
286+
pthread_mutex_lock(&rpMutex);
277287
pthread_mutex_lock(&rpMsgMutex);
278288
if (!stopProcessing)
279289
{
@@ -287,6 +297,7 @@ T2ERROR datamodel_MsgpackProcessProfile(char *str, int strSize)
287297
T2Error("Datamodel not initialized, dropping request \n");
288298
}
289299
pthread_mutex_unlock(&rpMsgMutex);
300+
pthread_mutex_unlock(&rpMutex);
290301
return T2ERROR_SUCCESS;
291302
}
292303

source/bulkdata/profile.c

Lines changed: 11 additions & 2 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

@@ -564,7 +567,9 @@ static void* CollectAndReport(void* data)
564567
pthread_mutex_lock(&profile->reportMutex);
565568
T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec);
566569
profile->maxlatencyTime.tv_sec += maxuploadinSec;
567-
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
570+
do {
571+
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
572+
} while(n != ETIMEDOUT && n != 0);
568573
if(n == ETIMEDOUT)
569574
{
570575
T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name);
@@ -615,7 +620,9 @@ static void* CollectAndReport(void* data)
615620
pthread_mutex_lock(&profile->reportMutex);
616621
T2Info("waiting for %ld sec of macUploadLatency\n", (long) maxuploadinSec);
617622
profile->maxlatencyTime.tv_sec += maxuploadinSec;
618-
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
623+
do {
624+
n = pthread_cond_timedwait(&profile->reportcond, &profile->reportMutex, &profile->maxlatencyTime);
625+
} while(n != ETIMEDOUT && n != 0);
619626
if(n == ETIMEDOUT)
620627
{
621628
T2Info("TIMEOUT for maxUploadLatency of profile %s\n", profile->name);
@@ -782,9 +789,11 @@ reportThreadEnd :
782789
}
783790
while(profile->enable);
784791
T2Info("%s --out Exiting collect and report Thread\n", __FUNCTION__);
792+
pthread_mutex_unlock(&profile->reuseThreadMutex);
785793
pthread_mutex_lock(&profile->reportInProgressMutex);
786794
profile->reportInProgress = false;
787795
pthread_mutex_unlock(&profile->reportInProgressMutex);
796+
pthread_mutex_lock(&profile->reuseThreadMutex);
788797
profile->threadExists = false;
789798
pthread_mutex_unlock(&profile->reuseThreadMutex);
790799
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: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,18 @@ void* T2ER_EventDispatchThread(void *arg)
243243
return NULL;
244244
}
245245
T2Debug("Checking for events in event queue , event count = %d\n", t2_queue_count(eQueue));
246-
if(t2_queue_count(eQueue) > 0)
246+
while(t2_queue_count(eQueue) == 0 && !stopDispatchThread)
247+
{
248+
T2Debug("Event Queue size is 0, Waiting events from T2ER_Push\n");
249+
int ret = pthread_cond_wait(&erCond, &erMutex);
250+
if(ret != 0) // pthread cond wait failed return after unlock
251+
{
252+
T2Error("%s pthread_cond_wait failed with error code: %d\n", __FUNCTION__, ret);
253+
}
254+
T2Debug("Received signal from T2ER_Push\n");
255+
}
256+
257+
if(t2_queue_count(eQueue) > 0)
247258
{
248259
T2Event *event = (T2Event *)t2_queue_pop(eQueue);
249260
if(event == NULL)
@@ -280,18 +291,11 @@ void* T2ER_EventDispatchThread(void *arg)
280291
}
281292
else
282293
{
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-
}
289294
if(pthread_mutex_unlock(&erMutex) != 0)
290295
{
291296
T2Error("%s pthread_mutex_unlock for erMutex failed\n", __FUNCTION__);
292297
return NULL;
293298
}
294-
T2Debug("Received signal from T2ER_Push\n");
295299
}
296300
}
297301
T2Debug("%s --out\n", __FUNCTION__);
@@ -345,7 +349,10 @@ T2ERROR T2ER_Init()
345349
registerForTelemetryEvents(T2ER_PushDataWithDelim);
346350
}
347351

348-
system("touch /tmp/.t2ReadyToReceiveEvents");
352+
if (system("touch /tmp/.t2ReadyToReceiveEvents") != 0)
353+
{
354+
T2Error("Failed to create /tmp/.t2ReadyToReceiveEvents flag file \n");
355+
}
349356
setT2EventReceiveState(T2_STATE_COMPONENT_READY);
350357
T2Info("T2 is now Ready to Recieve Events\n");
351358

@@ -500,13 +507,13 @@ void T2ER_Uninit()
500507
}
501508
EREnabled = false;
502509

510+
if(pthread_mutex_lock(&sTDMutex) != 0) // mutex lock failed so return from T2ER_Uninit
511+
{
512+
T2Error("%s pthread_mutex_lock for sTDMutex failed\n", __FUNCTION__);
513+
return;
514+
}
503515
if(!stopDispatchThread)
504516
{
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-
}
510517
stopDispatchThread = true;
511518
if(pthread_mutex_unlock(&sTDMutex) != 0) //mutex unlock failed so return from T2ER_Uninit
512519
{
@@ -551,6 +558,14 @@ void T2ER_Uninit()
551558
return;
552559
}
553560
}
561+
else
562+
{
563+
if(pthread_mutex_unlock(&sTDMutex) != 0)
564+
{
565+
T2Error("%s pthread_mutex_unlock for sTDMutex failed\n", __FUNCTION__);
566+
return;
567+
}
568+
}
554569
T2Debug("T2ER Event Dispatch Thread successfully terminated\n");
555570
t2_queue_destroy(eQueue, freeT2Event);
556571
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)

source/dcautil/dca.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,9 @@ static FileDescriptor* getFileDeltaInMemMapAndSearch(const int fd, const off_t s
10171017

10181018
//create a tmp file for main file fd
10191019
char tmp_fdmain[] = "/tmp/dca_tmpfile_fdmainXXXXXX";
1020+
mode_t old_umask = umask(0077); // Set secure umask (owner read/write only)
10201021
int tmp_fd = mkstemp(tmp_fdmain);
1022+
umask(old_umask); // Restore original umask
10211023
if (tmp_fd == -1)
10221024
{
10231025
T2Error("Failed to create temp file: %s\n", strerror(errno));
@@ -1044,7 +1046,9 @@ static FileDescriptor* getFileDeltaInMemMapAndSearch(const int fd, const off_t s
10441046
if (rd != -1 && fstat(rd, &rb) == 0 && rb.st_size > 0)
10451047
{
10461048
char tmp_fdrotated[] = "/tmp/dca_tmpfile_fdrotatedXXXXXX";
1049+
mode_t old_umask = umask(0077); // Set secure umask (owner read/write only)
10471050
int tmp_rd = mkstemp(tmp_fdrotated);
1051+
umask(old_umask); // Restore original umask
10481052
if (tmp_rd == -1)
10491053
{
10501054
T2Error("Failed to create temp file: %s\n", strerror(errno));

source/protocol/rbusMethod/rbusmethodinterface.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static void asyncMethodHandler(rbusHandle_t handle, char const* methodName, rbus
5050
(void) params;
5151

5252
T2Info("T2 asyncMethodHandler called: %s with return error code = %s \n", methodName, rbusError_ToString(retStatus));
53+
pthread_mutex_lock(&rbusMethodMutex);
5354
if(retStatus == RBUS_ERROR_SUCCESS)
5455
{
5556
isRbusMethod = true ;

source/reportgen/reportgen.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,7 @@ char *prepareHttpUrl(T2HTTP *http)
12101210
unsigned int index = 0;
12111211
int params_len = 0;
12121212
char *url_params = NULL;
1213+
char *temp_params = NULL;
12131214

12141215
for(; index < http->RequestURIparamList->count; index++)
12151216
{
@@ -1250,13 +1251,14 @@ char *prepareHttpUrl(T2HTTP *http)
12501251
{
12511252
new_params_len += strlen(httpParamVal);
12521253
}
1253-
url_params = realloc(url_params, new_params_len);
1254-
if(url_params == NULL)
1254+
temp_params = realloc(url_params, new_params_len);
1255+
if(temp_params == NULL)
12551256
{
12561257
T2Error("Unable to allocate %d bytes of memory at Line %d on %s \n", new_params_len, __LINE__, __FILE__);
12571258
curl_free(httpParamVal);
12581259
continue;
12591260
}
1261+
url_params = temp_params;
12601262
params_len += snprintf(url_params + params_len, new_params_len - params_len, "%s=%s&", httpParam->HttpName, httpParamVal);
12611263

12621264
curl_free(httpParamVal);

source/scheduler/scheduler.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,25 @@ void* TimeoutThread(void *arg)
258258
if(tProfile->firstreportint > 0 && tProfile->firstexecution == true )
259259
{
260260
T2Info("Waiting for %d sec for next TIMEOUT for profile as firstreporting interval is given - %s\n", tProfile->firstreportint, tProfile->name);
261-
n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts);
261+
do {
262+
n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts);
263+
} while(n != ETIMEDOUT && n != 0);
262264
}
263265
else
264266
{
265267
if(tProfile->timeOutDuration == UINT_MAX && tProfile->timeRefinSec == 0)
266268
{
267269
T2Info("Waiting for condition as reporting interval is not configured for profile - %s\n", tProfile->name);
268-
n = pthread_cond_wait(&tProfile->tCond, &tProfile->tMutex);
270+
do {
271+
n = pthread_cond_wait(&tProfile->tCond, &tProfile->tMutex);
272+
} while(n != 0 && !tProfile->terminated);
269273
}
270274
else
271275
{
272276
T2Info("Waiting for timeref or reporting interval for the profile - %s is started\n", tProfile->name);
273-
n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts);
277+
do {
278+
n = pthread_cond_timedwait(&tProfile->tCond, &tProfile->tMutex, &_ts);
279+
} while(n != ETIMEDOUT && n != 0);
274280
}
275281
}
276282
if(n == ETIMEDOUT)
@@ -648,8 +654,20 @@ T2ERROR unregisterProfileFromScheduler(const char* profileName)
648654
// pthread_join(tProfile->tId, NULL); // pthread_detach in freeSchedulerProfile will detach the thread
649655
sched_yield(); // This will give chance for the signal receiving thread to start
650656
int count = 0;
651-
while(signalrecived_and_executing && !is_activation_time_out)
657+
bool signal_executing = true;
658+
while(signal_executing && !is_activation_time_out)
652659
{
660+
if(pthread_mutex_lock(&tProfile->tMutex) != 0)
661+
{
662+
T2Error("tProfile Mutex lock failed\n");
663+
return T2ERROR_FAILURE;
664+
}
665+
signal_executing = signalrecived_and_executing;
666+
if(pthread_mutex_unlock(&tProfile->tMutex) != 0)
667+
{
668+
T2Error("tProfile Mutex unlock failed\n");
669+
return T2ERROR_FAILURE;
670+
}
653671
if(count++ > 10)
654672
{
655673
break;

source/utils/persistence.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,18 @@ T2ERROR getPrivacyModeFromPersistentFolder(char **privMode)
464464
T2Error("Unable to open the file : %s\n", filePath);
465465
return T2ERROR_FAILURE;
466466
}
467-
stat(filePath, &filestat);
468-
fread(data, sizeof(char), filestat.st_size, fp);
467+
if (stat(filePath, &filestat) != 0)
468+
{
469+
T2Error("Unable to stat file : %s\n", filePath);
470+
fclose(fp);
471+
return T2ERROR_FAILURE;
472+
}
473+
if (fread(data, sizeof(char), filestat.st_size, fp) != (size_t)filestat.st_size)
474+
{
475+
T2Error("Failed to read complete data from file : %s\n", filePath);
476+
fclose(fp);
477+
return T2ERROR_FAILURE;
478+
}
469479
*privMode = strdup(data);
470480
if(fclose(fp) != 0)
471481
{

0 commit comments

Comments
 (0)