Skip to content

Commit

Permalink
Fix nasa#2529, Adds snprintf check return value in EVS_SendViaPorts
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfiguer authored and jdfiguer committed Jun 12, 2024
1 parent 505baa1 commit 38e782a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion modules/evs/fsw/src/cfe_evs_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#define CFE_EVS_MAX_FILTER_COUNT 65535
#define CFE_EVS_MAX_SQUELCH_COUNT 255
#define CFE_EVS_PIPE_NAME "EVS_CMD_PIPE"
#define CFE_EVS_MAX_PORT_MSG_LENGTH (CFE_MISSION_EVS_MAX_MESSAGE_LENGTH + OS_MAX_API_NAME + 30)
#define CFE_EVS_MAX_PORT_MSG_LENGTH (CFE_MISSION_EVS_MAX_MESSAGE_LENGTH + OS_MAX_API_NAME + 19)

/* Since CFE_EVS_MAX_PORT_MSG_LENGTH is the size of the buffer that is sent to
* print out (using OS_printf), we need to check to make sure that the buffer
Expand Down
18 changes: 14 additions & 4 deletions modules/evs/fsw/src/cfe_evs_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,24 @@ void EVS_SendViaPorts(CFE_EVS_LongEventTlm_t *EVS_PktPtr)
char PortMessage[CFE_EVS_MAX_PORT_MSG_LENGTH];
char TimeBuffer[CFE_TIME_PRINTED_STRING_SIZE];
CFE_TIME_SysTime_t PktTime = {0};
CFE_Status_t Status = 0;

CFE_MSG_GetMsgTime(CFE_MSG_PTR(EVS_PktPtr->TelemetryHeader), &PktTime);
CFE_TIME_Print(TimeBuffer, PktTime);

snprintf(PortMessage, sizeof(PortMessage), "%s %u/%u/%s %u: %s", TimeBuffer,
(unsigned int)EVS_PktPtr->Payload.PacketID.SpacecraftID,
(unsigned int)EVS_PktPtr->Payload.PacketID.ProcessorID, EVS_PktPtr->Payload.PacketID.AppName,
(unsigned int)EVS_PktPtr->Payload.PacketID.EventID, EVS_PktPtr->Payload.Message);
Status = snprintf(PortMessage, sizeof(PortMessage), "%s %u/%u/%s %u: %s", TimeBuffer,
(unsigned int)EVS_PktPtr->Payload.PacketID.SpacecraftID,
(unsigned int)EVS_PktPtr->Payload.PacketID.ProcessorID, EVS_PktPtr->Payload.PacketID.AppName,
(unsigned int)EVS_PktPtr->Payload.PacketID.EventID, EVS_PktPtr->Payload.Message);

if (Status >= sizeof(PortMessage))
{
// Handle truncation: ensure the message ends with "*"
PortMessage[sizeof(PortMessage) - 2] = '*';
PortMessage[sizeof(PortMessage) - 1] = '\0'; // Ensure null terminator
OS_printf("Warning: PortMessage was truncated. Required size: %d, Buffer size: %zu\n", Status,
sizeof(PortMessage));
}

if (CFE_EVS_Global.EVS_TlmPkt.Payload.OutputPort & CFE_EVS_PORT1_BIT)
{
Expand Down

0 comments on commit 38e782a

Please sign in to comment.