@@ -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 {
0 commit comments