From a09e8813f934c2a518ac508ed3064bafda7048da Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 19 Dec 2022 12:12:45 -0800 Subject: [PATCH] [test] replace `sprintf` with `snprintf` in `otPlatDiagProcess()` (#8549) It also removes stale comments in `ncp_hdlc.cp` about use of `sprintf`. --- src/ncp/ncp_hdlc.cpp | 5 ----- tests/unit/test_platform.cpp | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/ncp/ncp_hdlc.cpp b/src/ncp/ncp_hdlc.cpp index 7ecf0e471..2020d157d 100644 --- a/src/ncp/ncp_hdlc.cpp +++ b/src/ncp/ncp_hdlc.cpp @@ -274,8 +274,6 @@ void NcpHdlc::HandleError(otError aError, uint8_t *aBuf, uint16_t aBufLength) super_t::IncrementFrameErrorCounter(); - // We can get away with sprintf because we know - // `hexbuf` is large enough. snprintf(hexbuf, sizeof(hexbuf), "Framing error %d: [", aError); // Write out the first part of our log message. @@ -285,9 +283,6 @@ void NcpHdlc::HandleError(otError aError, uint8_t *aBuf, uint16_t aBufLength) // The second '3' comes from the length of two hex digits and a space. for (i = 0; (i < aBufLength) && (i < (sizeof(hexbuf) - 3) / 3); i++) { - // We can get away with sprintf because we know - // `hexbuf` is large enough, based on our calculations - // above. snprintf(&hexbuf[i * 3], sizeof(hexbuf) - i * 3, " %02X", static_cast(aBuf[i])); } diff --git a/tests/unit/test_platform.cpp b/tests/unit/test_platform.cpp index 5550757c7..bf196d0f5 100644 --- a/tests/unit/test_platform.cpp +++ b/tests/unit/test_platform.cpp @@ -193,9 +193,9 @@ exit: return error; } -OT_TOOL_WEAK void otPlatDiagProcess(otInstance *, uint8_t, char *aArgs[], char *aOutput, size_t) +OT_TOOL_WEAK void otPlatDiagProcess(otInstance *, uint8_t, char *aArgs[], char *aOutput, size_t aOutputMaxLen) { - sprintf(aOutput, "diag feature '%s' is not supported\r\n", aArgs[0]); + snprintf(aOutput, aOutputMaxLen, "diag feature '%s' is not supported\r\n", aArgs[0]); } OT_TOOL_WEAK void otPlatDiagModeSet(bool aMode) { sDiagMode = aMode; }