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 May 23, 2024
1 parent 505baa1 commit c2946b3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions modules/evs/fsw/src/cfe_evs_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,15 +540,26 @@ 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_TIME_SysTime_t PktTime = {0};
CFE_Status_t Status = 0;
uint32 EVS_OutputPortBuffer = 11; // "EVS Port%u %s\n"

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

if (CFE_EVS_Global.EVS_TlmPkt.Payload.OutputPort & CFE_EVS_PORT1_BIT)
{
Expand All @@ -574,6 +585,7 @@ void EVS_SendViaPorts(CFE_EVS_LongEventTlm_t *EVS_PktPtr)
EVS_OutputPort(4, PortMessage);
}
}
}

/*----------------------------------------------------------------
*
Expand Down

0 comments on commit c2946b3

Please sign in to comment.