From ca3fca370eb776864f335d70b14c4c287e84aead Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Wed, 30 Sep 2020 10:39:54 -0400 Subject: [PATCH] Update #899, Use sizeof() for output strings Also check/ensure null termination of output --- fsw/cfe-core/src/es/cfe_es_apps.c | 10 +++++----- fsw/cfe-core/src/sb/cfe_sb_priv.c | 2 +- fsw/cfe-core/src/tbl/cfe_tbl_api.c | 4 +++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/fsw/cfe-core/src/es/cfe_es_apps.c b/fsw/cfe-core/src/es/cfe_es_apps.c index 1c3e4f30e..8bcf9c5b1 100644 --- a/fsw/cfe-core/src/es/cfe_es_apps.c +++ b/fsw/cfe-core/src/es/cfe_es_apps.c @@ -1525,9 +1525,9 @@ int32 CFE_ES_GetTaskInfoInternal(CFE_ES_TaskRecord_t *TaskRecPtr, CFE_ES_TaskInf ** Get the Application ID and Task Name */ TaskInfoPtr->AppId = TaskRecPtr->AppId; - strncpy((char *)TaskInfoPtr->TaskName, - (char *)TaskRecPtr->TaskName,OS_MAX_API_NAME); - TaskInfoPtr->TaskName[OS_MAX_API_NAME - 1] = '\0'; + strncpy((char*)TaskInfoPtr->TaskName, TaskRecPtr->TaskName, + sizeof(TaskInfoPtr->TaskName)-1); + TaskInfoPtr->TaskName[sizeof(TaskInfoPtr->TaskName)-1] = '\0'; /* ** Store away the Task ID ( for the QueryAllTasks Cmd ) @@ -1545,9 +1545,9 @@ int32 CFE_ES_GetTaskInfoInternal(CFE_ES_TaskRecord_t *TaskRecPtr, CFE_ES_TaskInf AppRecPtr = CFE_ES_LocateAppRecordByID(TaskRecPtr->AppId); if (CFE_ES_AppRecordIsMatch(AppRecPtr, TaskRecPtr->AppId)) { - strncpy((char*)TaskInfoPtr->AppName, - (char*)AppRecPtr->StartParams.Name, + strncpy((char*)TaskInfoPtr->AppName, AppRecPtr->StartParams.Name, sizeof(TaskInfoPtr->AppName)-1); + TaskInfoPtr->AppName[sizeof(TaskInfoPtr->AppName)-1] = '\0'; ReturnCode = CFE_SUCCESS; } else diff --git a/fsw/cfe-core/src/sb/cfe_sb_priv.c b/fsw/cfe-core/src/sb/cfe_sb_priv.c index 0cad3d4f4..059a23228 100644 --- a/fsw/cfe-core/src/sb/cfe_sb_priv.c +++ b/fsw/cfe-core/src/sb/cfe_sb_priv.c @@ -634,7 +634,7 @@ char *CFE_SB_GetAppTskName(uint32 TaskId,char *FullName){ strncpy(FullName,"Unknown",OS_MAX_API_NAME-1); FullName[OS_MAX_API_NAME-1] = '\0'; - }else if(strncmp((char *)ptr->AppName,(char *)ptr->TaskName,OS_MAX_API_NAME-1) == 0){ + }else if(strncmp((char *)ptr->AppName,(char *)ptr->TaskName,sizeof(ptr->AppName)) == 0){ /* if app name and task name are the same */ strncpy(FullName,(char *)ptr->AppName,OS_MAX_API_NAME-1); diff --git a/fsw/cfe-core/src/tbl/cfe_tbl_api.c b/fsw/cfe-core/src/tbl/cfe_tbl_api.c index 84aee00d8..2123edf8c 100644 --- a/fsw/cfe-core/src/tbl/cfe_tbl_api.c +++ b/fsw/cfe-core/src/tbl/cfe_tbl_api.c @@ -1401,7 +1401,9 @@ int32 CFE_TBL_GetInfo( CFE_TBL_Info_t *TblInfoPtr, const char *TblName ) TblInfoPtr->FileCreateTimeSecs = RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].FileCreateTimeSecs; TblInfoPtr->FileCreateTimeSubSecs = RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].FileCreateTimeSubSecs; TblInfoPtr->Crc = RegRecPtr->Buffers[RegRecPtr->ActiveBufferIndex].Crc; - strncpy(TblInfoPtr->LastFileLoaded, RegRecPtr->LastFileLoaded, OS_MAX_PATH_LEN); + strncpy(TblInfoPtr->LastFileLoaded, RegRecPtr->LastFileLoaded, + sizeof(TblInfoPtr->LastFileLoaded)-1); + TblInfoPtr->LastFileLoaded[sizeof(TblInfoPtr->LastFileLoaded)-1] = 0; /* Count the number of Access Descriptors to determine the number of users */ HandleIterator = RegRecPtr->HeadOfAccessList;