Skip to content

Commit

Permalink
Merge pull request nasa#2584 from tandharia:fix-2581-check-bytes-read
Browse files Browse the repository at this point in the history
Fix nasa#2581, Adding bytes read check
  • Loading branch information
dzbaker committed Aug 2, 2024
2 parents 7885b8d + 704e7ef commit 8244c18
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
10 changes: 6 additions & 4 deletions modules/fs/fsw/src/cfe_fs_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ CFE_Status_t CFE_FS_ReadHeader(CFE_FS_Header_t *Hdr, osal_id_t FileDes)
*/
OsStatus = OS_read(FileDes, Hdr, sizeof(CFE_FS_Header_t));

/* Check if the read was successful */
if (OsStatus != sizeof(CFE_FS_Header_t))
{
return CFE_STATUS_EXTERNAL_RESOURCE_FAIL;
}

/* Determine if this processor is a little endian processor */
/* cppcheck-suppress knownConditionTrueFalse */
if ((*(char *)(&EndianCheck)) == 0x04)
Expand All @@ -146,11 +152,7 @@ CFE_Status_t CFE_FS_ReadHeader(CFE_FS_Header_t *Hdr, osal_id_t FileDes)
/* its standard big-endian format into a little endian format to ease user access */
CFE_FS_ByteSwapCFEHeader(Hdr);
}
}

if (OsStatus >= OS_SUCCESS)
{
/* The "OsStatus" reflects size actually read */
Result = (long)OsStatus;
}
else
Expand Down
5 changes: 5 additions & 0 deletions modules/fs/ut-coverage/fs_UT.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ void Test_CFE_FS_ReadHeader(void)
UT_SetDefaultReturnValue(UT_KEY(OS_read), OS_ERROR);
UtAssert_INT32_EQ(CFE_FS_ReadHeader(&Hdr, FileDes), CFE_STATUS_EXTERNAL_RESOURCE_FAIL);

/* Test partial success with reading header */
UT_InitData();
UT_SetDefaultReturnValue(UT_KEY(OS_read), 1);
UtAssert_INT32_EQ(CFE_FS_ReadHeader(&Hdr, FileDes), CFE_STATUS_EXTERNAL_RESOURCE_FAIL);

/* Test successfully reading the header */
UT_InitData();
UtAssert_INT32_EQ(CFE_FS_ReadHeader(&Hdr, FileDes), sizeof(Hdr));
Expand Down

0 comments on commit 8244c18

Please sign in to comment.