Skip to content

Commit c35d07f

Browse files
author
mtirum011
committed
RDK-60307 [RRD] RDK Coverity Defect Resolution for Device Management
1 parent c613c0b commit c35d07f

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

src/rrdEventProcess.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,22 +173,26 @@ static void processIssueType(data_buf *rbuf)
173173
size_t dynamicstrlen = strlen(dynamicprofiledata->command);
174174
if (staticstrlen > 0 && staticprofiledata->command[staticstrlen - 1] == '"') {
175175
staticprofiledata->command[staticstrlen - 1] = '\0';
176+
staticstrlen--; // Update length after removing trailing quote
176177
}
177178
if (dynamicprofiledata->command[0] == '"') {
178179
dynamicprofiledata->command[0] = COMMAND_DELIM;
179180
}
180181
RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: Static Profile Commands: %s, Dynamic Profile Commands: %s\n", __FUNCTION__, __LINE__, staticprofiledata->command, dynamicprofiledata->command);
181182

182-
size_t appendstrlen = ((staticstrlen - 1) + dynamicstrlen + 1);
183+
size_t appendstrlen = (staticstrlen + dynamicstrlen + 1);
183184
char *appendcommandstr = (char *)realloc(staticprofiledata->command, appendstrlen);
184185
if (appendcommandstr == NULL) {
185-
RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: Memory Allocation Failed... \n", __FUNCTION__, __LINE__);
186+
RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: Memory Allocation Failed... \n", __FUNCTION__, __LINE__);
186187
}
187-
strcat(appendcommandstr, dynamicprofiledata->command);
188-
staticprofiledata->command = appendcommandstr;
189-
RDK_LOG(RDK_LOG_INFO, LOG_REMDEBUG, "[%s:%d]: Updated command after append from dynamic and static profile: %s \n", __FUNCTION__, __LINE__, staticprofiledata->command);
190-
RDK_LOG(RDK_LOG_DEBUG,LOG_REMDEBUG,"[%s:%d]: Executing Commands in Runtime Service... \n",__FUNCTION__,__LINE__);
191-
checkIssueNodeInfo(pIssueNode, NULL, rbuf, false, staticprofiledata);
188+
else
189+
{
190+
strcat(appendcommandstr, dynamicprofiledata->command);
191+
staticprofiledata->command = appendcommandstr;
192+
RDK_LOG(RDK_LOG_INFO, LOG_REMDEBUG, "[%s:%d]: Updated command after append from dynamic and static profile: %s \n", __FUNCTION__, __LINE__, staticprofiledata->command);
193+
RDK_LOG(RDK_LOG_DEBUG,LOG_REMDEBUG,"[%s:%d]: Executing Commands in Runtime Service... \n",__FUNCTION__,__LINE__);
194+
checkIssueNodeInfo(pIssueNode, NULL, rbuf, false, staticprofiledata);
195+
}
192196
}
193197
}
194198
}

src/rrdJsonParser.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,22 @@ char * readJsonFile(char *jsonfile)
9696
}
9797
fseek(fp, 0, SEEK_SET);
9898
jsonfile_content = (char *) malloc(sizeof(char) * (ch_count + 1));
99-
fread(jsonfile_content, 1, ch_count,fp);
99+
if (jsonfile_content == NULL)
100+
{
101+
RDK_LOG(RDK_LOG_ERROR,LOG_REMDEBUG,"[%s:%d]: Memory allocation failed for json file %s \n",__FUNCTION__,__LINE__,jsonfile);
102+
fclose(fp);
103+
return NULL;
104+
}
105+
106+
size_t bytes_read = fread(jsonfile_content, 1, ch_count, fp);
107+
if (bytes_read != (size_t)ch_count)
108+
{
109+
RDK_LOG(RDK_LOG_ERROR,LOG_REMDEBUG,"[%s:%d]: Failed to read json file %s. Expected %d bytes, read %zu bytes \n",__FUNCTION__,__LINE__,jsonfile,ch_count,bytes_read);
110+
free(jsonfile_content);
111+
fclose(fp);
112+
return NULL;
113+
}
114+
100115
jsonfile_content[ch_count] ='\0';
101116
fclose(fp);
102117

@@ -486,6 +501,14 @@ void checkIssueNodeInfo(issueNodeData *issuestructNode, cJSON *jsoncfg, data_buf
486501
struct tm *ltime;
487502
rfcbuf = strdup(buff->mdata);
488503

504+
if (rfcbuf == NULL)
505+
{
506+
RDK_LOG(RDK_LOG_ERROR,LOG_REMDEBUG,"[%s:%d]: Memory allocation failed for rfcbuf\n",__FUNCTION__,__LINE__);
507+
free(buff->mdata); // free rfc data
508+
free(buff->jsonPath); // free rrd path info
509+
return;
510+
}
511+
489512
// Creating Directory for MainNode under /tmp/rrd Folder
490513
ctime = time (NULL);
491514
ltime = localtime (&ctime);
@@ -500,6 +523,7 @@ void checkIssueNodeInfo(issueNodeData *issuestructNode, cJSON *jsoncfg, data_buf
500523
if (mkdir(outdir,0777) != 0)
501524
{
502525
RDK_LOG(RDK_LOG_ERROR,LOG_REMDEBUG,"[%s:%d]: %s Directory creation failed!!!\n",__FUNCTION__,__LINE__,outdir);
526+
free(rfcbuf); // free duplicated rfc data
503527
free(buff->mdata); // free rfc data
504528
free(buff->jsonPath); // free rrd path info
505529
return;
@@ -552,12 +576,22 @@ void checkIssueNodeInfo(issueNodeData *issuestructNode, cJSON *jsoncfg, data_buf
552576
RDK_LOG(RDK_LOG_INFO,LOG_REMDEBUG,"[%s:%d]: RRD Upload Script Execution Success...\n",__FUNCTION__,__LINE__);
553577
}
554578
}
579+
if (rfcbuf != NULL)
580+
{
581+
free(rfcbuf); // free duplicated rfc data
582+
}
555583
free(buff->mdata); // free rfc data
556584
free(buff->jsonPath); // free rrd path info
557585
}
558586
else
559587
{
560588
RDK_LOG(RDK_LOG_ERROR,LOG_REMDEBUG,"[%s:%d]: No Command excuted as RRD Failed to change directory:%s\n",__FUNCTION__,__LINE__,outdir);
589+
if (rfcbuf != NULL)
590+
{
591+
free(rfcbuf); // free duplicated rfc data
592+
}
593+
free(buff->mdata); // free rfc data
594+
free(buff->jsonPath); // free rrd path info
561595
}
562596
}
563597
}
@@ -634,7 +668,7 @@ bool processAllDebugCommand(cJSON *jsoncfg, issueNodeData *issuestructNode, char
634668
}
635669
}
636670
}
637-
free(rfcbuf); // free rfc value
671+
// Note: rfcbuf is owned by caller and will be freed there
638672
}
639673
else
640674
{

src/rrdRunCmdThread.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ static cacheData *cacheDataNode = NULL;
4040
void initCache(void)
4141
{
4242
pthread_mutex_init(&rrdCacheMut, NULL);
43+
pthread_mutex_lock(&rrdCacheMut);
4344
cacheDataNode = NULL;
45+
pthread_mutex_unlock(&rrdCacheMut);
4446
}
4547

4648
/*
@@ -375,8 +377,9 @@ bool executeCommands(issueData *cmdinfo)
375377
/*Executing Commands using systemd-run*/
376378
RDK_LOG(RDK_LOG_INFO,LOG_REMDEBUG,"[%s:%d]: Executing following commands using systemd-run:\n \"%s\"\n",__FUNCTION__,__LINE__,cmdData->command);
377379

378-
strncpy(remoteDebuggerServiceStr, remoteDebuggerPrefix, strlen(remoteDebuggerPrefix) + 1);
379-
strncat(remoteDebuggerServiceStr, cmdData->rfcvalue, strlen(cmdData->rfcvalue));
380+
strncpy(remoteDebuggerServiceStr, remoteDebuggerPrefix, sizeof(remoteDebuggerServiceStr) - 1);
381+
remoteDebuggerServiceStr[sizeof(remoteDebuggerServiceStr) - 1] = '\0';
382+
strncat(remoteDebuggerServiceStr, cmdData->rfcvalue, sizeof(remoteDebuggerServiceStr) - strlen(remoteDebuggerServiceStr) - 1);
380383

381384
removeQuotes(cmdData->command);
382385

@@ -389,8 +392,8 @@ bool executeCommands(issueData *cmdinfo)
389392
{
390393
RDK_LOG(RDK_LOG_INFO,LOG_REMDEBUG,"[%s:%d]: Starting remote_debugger_%s service success...\n",__FUNCTION__,__LINE__,cmdData->rfcvalue);
391394
copyDebugLogDestFile(systemdfp, filePointer);
395+
v_secure_pclose(systemdfp);
392396
}
393-
v_secure_pclose(systemdfp);
394397

395398
/*Logging output using journalctl to Output file*/
396399
RDK_LOG(RDK_LOG_INFO,LOG_REMDEBUG,"[%s:%d]: Using journalctl to log command output...\n",__FUNCTION__,__LINE__);
@@ -403,9 +406,8 @@ bool executeCommands(issueData *cmdinfo)
403406
{
404407
RDK_LOG(RDK_LOG_INFO,LOG_REMDEBUG,"[%s:%d]: journalctl remote_debugger_%s service success...\n",__FUNCTION__,__LINE__,cmdData->rfcvalue);
405408
copyDebugLogDestFile(journalctlfp, filePointer);
409+
v_secure_pclose(journalctlfp);
406410
}
407-
408-
v_secure_pclose(journalctlfp);
409411

410412
/* Close debug_output.txt file*/
411413
fclose(filePointer);

0 commit comments

Comments
 (0)