From d4005f9d14f8e156bbde563186f73f60e91c5872 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Tue, 10 Aug 2021 18:18:15 -0700 Subject: [PATCH] [cli] fix usage of strncpy in coap (#6908) --- src/cli/cli_coap.cpp | 4 ++-- src/cli/cli_coap_secure.cpp | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cli/cli_coap.cpp b/src/cli/cli_coap.cpp index 8ed09274c..0b73d4c83 100644 --- a/src/cli/cli_coap.cpp +++ b/src/cli/cli_coap.cpp @@ -402,8 +402,8 @@ otError Coap::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode) SuccessOrExit(error = aArgs[0].ParseAsIp6Address(coapDestinationIp)); VerifyOrExit(!aArgs[1].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - VerifyOrExit(aArgs[1].GetLength() < kMaxUriLength, error = OT_ERROR_INVALID_ARGS); - strncpy(coapUri, aArgs[1].GetCString(), sizeof(coapUri) - 1); + VerifyOrExit(aArgs[1].GetLength() < sizeof(coapUri), error = OT_ERROR_INVALID_ARGS); + strcpy(coapUri, aArgs[1].GetCString()); // CoAP-Type if (!aArgs[2].IsEmpty()) diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index ad37468cc..d74d3f54f 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -61,6 +61,7 @@ CoapSecure::CoapSecure(Interpreter &aInterpreter) memset(&mResource, 0, sizeof(mResource)); memset(&mPsk, 0, sizeof(mPsk)); memset(&mPskId, 0, sizeof(mPskId)); + memset(&mUriPath, 0, sizeof(mUriPath)); strncpy(mResourceContent, "0", sizeof(mResourceContent)); mResourceContent[sizeof(mResourceContent) - 1] = '\0'; } @@ -249,8 +250,8 @@ otError CoapSecure::ProcessRequest(Arg aArgs[], otCoapCode aCoapCode) #endif VerifyOrExit(!aArgs[0].IsEmpty(), error = OT_ERROR_INVALID_ARGS); - - strncpy(coapUri, aArgs[0].GetCString(), sizeof(coapUri) - 1); + VerifyOrExit(aArgs[0].GetLength() < sizeof(coapUri), error = OT_ERROR_INVALID_ARGS); + strcpy(coapUri, aArgs[0].GetCString()); if (!aArgs[1].IsEmpty()) {