From 82b607597a5220ef7447a821dc744a6e97586995 Mon Sep 17 00:00:00 2001 From: jdfiguer Date: Tue, 4 Jun 2024 12:04:35 -0400 Subject: [PATCH] Fix #93, Replaces strlen and strncpy with CS_strnlen and snprintf --- fsw/src/cs_compute.c | 8 +++--- fsw/src/cs_init.c | 2 +- fsw/src/cs_table_processing.c | 40 +++++++++++++++++++-------- unit-test/cs_table_processing_tests.c | 3 +- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/fsw/src/cs_compute.c b/fsw/src/cs_compute.c index 7307f05..1e27700 100644 --- a/fsw/src/cs_compute.c +++ b/fsw/src/cs_compute.c @@ -536,20 +536,20 @@ void CS_RecomputeEepromMemoryChildTask(void) if (Table == CS_EEPROM_TABLE) { - strncpy(TableType, "EEPROM", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "EEPROM"); } if (Table == CS_MEMORY_TABLE) { - strncpy(TableType, "Memory", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Memory"); } if (Table == CS_CFECORE) { - strncpy(TableType, "cFE Core", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "cFE Core"); CS_AppData.HkPacket.Payload.CfeCoreBaseline = NewChecksumValue; } if (Table == CS_OSCORE) { - strncpy(TableType, "OS", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "OS"); CS_AppData.HkPacket.Payload.OSBaseline = NewChecksumValue; } diff --git a/fsw/src/cs_init.c b/fsw/src/cs_init.c index 3476461..d832d8d 100644 --- a/fsw/src/cs_init.c +++ b/fsw/src/cs_init.c @@ -42,7 +42,7 @@ CFE_Status_t CS_SbInit(void) CFE_Status_t Result = CFE_SUCCESS; /* Initialize app configuration data */ - strncpy(CS_AppData.PipeName, CS_CMD_PIPE_NAME, CS_CMD_PIPE_NAME_LEN); + snprintf(CS_AppData.PipeName, CS_CMD_PIPE_NAME_LEN, CS_CMD_PIPE_NAME); CS_AppData.PipeDepth = CS_PIPE_DEPTH; diff --git a/fsw/src/cs_table_processing.c b/fsw/src/cs_table_processing.c index 405c714..aff9a46 100644 --- a/fsw/src/cs_table_processing.c +++ b/fsw/src/cs_table_processing.c @@ -204,6 +204,22 @@ CFE_Status_t CS_ValidateMemoryChecksumDefinitionTable(void *TblPtr) return Result; } +/*---------------------------------------------------------------- + * + * Internal helper routine only, not part of API. + * + *-----------------------------------------------------------------*/ +static inline size_t CS_strnlen(const char *str, size_t maxlen) +{ + const char *end = memchr(str, 0, maxlen); + if (end != NULL) + { + /* actual length of string is difference */ + maxlen = end - str; + } + return maxlen; +} + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* */ /* CS Validation Callback function for Tables Table */ @@ -231,7 +247,7 @@ CFE_Status_t CS_ValidateTablesChecksumDefinitionTable(void *TblPtr) StateField = OuterEntry->State; /* Check for non-zero length for table name */ - if (strlen(OuterEntry->Name) != 0) + if (CS_strnlen(OuterEntry->Name, sizeof(OuterEntry->Name)) != 0) { /* Verify valid state definition */ if (((StateField == CS_STATE_EMPTY) || (StateField == CS_STATE_ENABLED) || @@ -357,7 +373,7 @@ CFE_Status_t CS_ValidateAppChecksumDefinitionTable(void *TblPtr) } BadCount++; } - else if (strlen(OuterEntry->Name) != 0) + else if (CS_strnlen(OuterEntry->Name, sizeof(OuterEntry->Name)) != 0) { /* Verify valid state definition */ if (((StateField == CS_STATE_EMPTY) || (StateField == CS_STATE_ENABLED) || @@ -825,7 +841,7 @@ CFE_Status_t CS_TableInit(CFE_TBL_Handle_t *DefinitionTableHandle, CFE_TBL_Handl osal_id_t Fd = OS_OBJECT_ID_UNDEFINED; char TableType[CS_TABLETYPE_NAME_SIZE]; - strncpy(TableType, "Undef Tbl", CS_TABLETYPE_NAME_SIZE); /* Init table type */ + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Undef Tbl"); /* Init table type */ SizeOfTable = NumEntries * SizeofResultsTableEntry; @@ -904,19 +920,19 @@ CFE_Status_t CS_TableInit(CFE_TBL_Handle_t *DefinitionTableHandle, CFE_TBL_Handl { if (Table == CS_EEPROM_TABLE) { - strncpy(TableType, "EEPROM", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "EEPROM"); } if (Table == CS_MEMORY_TABLE) { - strncpy(TableType, "Memory", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Memory"); } if (Table == CS_TABLES_TABLE) { - strncpy(TableType, "Tables", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Tables"); } if (Table == CS_APP_TABLE) { - strncpy(TableType, "Apps", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Apps"); } CFE_EVS_SendEvent(CS_TBL_INIT_ERR_EID, CFE_EVS_EventType_ERROR, @@ -967,7 +983,7 @@ CFE_Status_t CS_HandleTableUpdate(void *DefinitionTblPtr, void *ResultsTblPtr, C int32 Loop = 0; char TableType[CS_TABLETYPE_NAME_SIZE]; - strncpy(TableType, "Undef Tbl", CS_TABLETYPE_NAME_SIZE); /* Init table type */ + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Undef Tbl"); /* Init table type */ /* Below, there are several values that are returned and assigned, but never evaluated. */ /* This is done so intentionally, as it helps us with Source-Level debugging this functions. */ @@ -1031,19 +1047,19 @@ CFE_Status_t CS_HandleTableUpdate(void *DefinitionTblPtr, void *ResultsTblPtr, C { if (Table == CS_EEPROM_TABLE) { - strncpy(TableType, "EEPROM", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "EEPROM"); } if (Table == CS_MEMORY_TABLE) { - strncpy(TableType, "Memory", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Memory"); } if (Table == CS_TABLES_TABLE) { - strncpy(TableType, "Table", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "Table"); } if (Table == CS_APP_TABLE) { - strncpy(TableType, "App", CS_TABLETYPE_NAME_SIZE); + snprintf(TableType, CS_TABLETYPE_NAME_SIZE, "App"); } /* There was a problem somewhere, generate an event */ diff --git a/unit-test/cs_table_processing_tests.c b/unit-test/cs_table_processing_tests.c index 1d7df7f..6ff7c29 100644 --- a/unit-test/cs_table_processing_tests.c +++ b/unit-test/cs_table_processing_tests.c @@ -919,7 +919,8 @@ void CS_ValidateTablesChecksumDefinitionTable_Test_UndefTableErrorResult(void) strncpy(CS_AppData.DefTablesTblPtr[0].Name, "name", 10); strncpy(CS_AppData.DefTablesTblPtr[1].Name, "name", 10); - strncpy(CS_AppData.DefTablesTblPtr[2].Name, "name", 10); + memset(CS_AppData.DefTablesTblPtr[2].Name, 0xFF, sizeof(CS_AppData.DefTablesTblPtr[2].Name)); + /* This test also covers CS_strnlen false branch */ /* Execute the function being tested */ Result = CS_ValidateTablesChecksumDefinitionTable(CS_AppData.DefTablesTblPtr);