From 2a8eb64e61b7d4daad5910aa71214b8cebfb3166 Mon Sep 17 00:00:00 2001 From: Gatsby Van Date: Wed, 25 Dec 2024 16:05:02 +0800 Subject: [PATCH] fix(linklayer): set default value for variables to prevent unexpected behavior Some variables had size mismatches with the data size defined in the frame segment. After deframing, this could leave residual bits as random and unknown values. --- src/networking/link_layer/scan.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/networking/link_layer/scan.c b/src/networking/link_layer/scan.c index 44c83ac..357dc6e 100644 --- a/src/networking/link_layer/scan.c +++ b/src/networking/link_layer/scan.c @@ -478,7 +478,7 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) float nodePos; L2_DEASSEMBLE(msg, L2_MSG_ADV_SRC_POSX, &nodePos); node->nodeInfo.pos.x = (double)nodePos; - L2_DEASSEMBLE(msg, L2_MSG_ADV_SRC_POSY, &nodePos); + L2_DEASSEMBLE(msg, L2_MSG_ADV_SRC_POSY, &nodePos); node->nodeInfo.pos.y = (double)nodePos; L2_DEASSEMBLE(msg, L2_MSG_ADV_SRC_POSZ, &nodePos); node->nodeInfo.pos.z = (double)nodePos; @@ -487,7 +487,7 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) // Reset node ttl node->ttlCnt = 0; - uint32_t netstartTime; + uint32_t netstartTime = 0; // Update net start time. L2_DEASSEMBLE(msg, L2_MSG_ADV_SRC_NETSTART_TIME, &netstartTime); L2_EVENT netSetupDoneEvent = { @@ -496,10 +496,12 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) AssertL2Event(instPtr, &netSetupDoneEvent); // Calculate time deviation - uint32_t frameNum, slotNum = 0; + uint32_t frameNum = 0; + uint32_t slotNum = 0; + int64_t timeDev = 0; L2_DEASSEMBLE(msg, L2_MSG_FRAME_NUM, &frameNum); L2_DEASSEMBLE(msg, L2_MSG_SLOT_NUM, &slotNum); - Status = L2_GetTimeDeviation(instPtr, frameNum, slotNum, true, &(instPtr->log.timeDev)); + Status = L2_GetTimeDeviation(instPtr, frameNum, slotNum, true, &timeDev); if (Status != SUCCESS) { DBG_ERROR("Failed to compute time deviation.\n") @@ -507,7 +509,8 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) } // Time sync. - L2_TimeAdjust(instPtr, &(instPtr->log.timeDev)); + L2_TimeAdjust(instPtr, &timeDev); + instPtr->log.timeDev = timeDev; // trigger flag AdvRecvStateUpdate(scanFsmPtr, true); @@ -550,15 +553,18 @@ int32_t L2_Scan_PktHandler(L2_INST *instPtr, uint8_t *msg) node->ttlCnt = 0; // Calculate time deviation - uint32_t frameNum, slotNum = 0; + uint32_t frameNum = 0; + uint32_t slotNum = 0; + int64_t timeDev = 0; L2_DEASSEMBLE(msg, L2_MSG_FRAME_NUM, &frameNum); L2_DEASSEMBLE(msg, L2_MSG_SLOT_NUM, &slotNum); - Status = L2_GetTimeDeviation(instPtr, frameNum, slotNum, true, &(instPtr->log.timeDev)); + Status = L2_GetTimeDeviation(instPtr, frameNum, slotNum, true, &timeDev); if (Status != SUCCESS) { DBG_ERROR("Failed to compute time deviation.\n") break; } + instPtr->log.timeDev = timeDev; if (L2_GetNodeType(instPtr) != L2_NODE_HEAD) {