From c2946b37a583115708d50ed2dc274330b28b98b1 Mon Sep 17 00:00:00 2001 From: jdfiguer Date: Wed, 22 May 2024 16:53:57 -0400 Subject: [PATCH] Fix #2529, Adds snprintf check return value in EVS_SendViaPorts --- modules/evs/fsw/src/cfe_evs_utils.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/modules/evs/fsw/src/cfe_evs_utils.c b/modules/evs/fsw/src/cfe_evs_utils.c index 03a39a95a..528bb343a 100644 --- a/modules/evs/fsw/src/cfe_evs_utils.c +++ b/modules/evs/fsw/src/cfe_evs_utils.c @@ -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) { @@ -574,6 +585,7 @@ void EVS_SendViaPorts(CFE_EVS_LongEventTlm_t *EVS_PktPtr) EVS_OutputPort(4, PortMessage); } } +} /*---------------------------------------------------------------- *