From 92655bc6a673c8f49dab475e24d5b8757d5cd18b Mon Sep 17 00:00:00 2001 From: TaowerfulMAX Date: Thu, 22 Aug 2024 17:48:55 +0800 Subject: [PATCH] fix: strcpy check output size. Signed-off-by: TaowerfulMAX q! --- src/common/base.cpp | 4 ++-- src/common/base.h | 2 +- src/host/client.cpp | 2 +- src/host/host_tcp.cpp | 2 +- src/test/main.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/common/base.cpp b/src/common/base.cpp index c47813b6..f197c4e5 100644 --- a/src/common/base.cpp +++ b/src/common/base.cpp @@ -681,7 +681,7 @@ void PrintLogEx(const char *functionName, int line, uint8_t logLevel, const char return static_cast(GetRandom(min, max)); } - int ConnectKey2IPPort(const char *connectKey, char *outIP, uint16_t *outPort) + int ConnectKey2IPPort(const char *connectKey, char *outIP, uint16_t *outPort, size_t outSize) { char bufString[BUF_SIZE_TINY] = ""; if (strncpy_s(bufString, sizeof(bufString), connectKey, strlen(connectKey))) { @@ -696,7 +696,7 @@ void PrintLogEx(const char *functionName, int line, uint8_t logLevel, const char return ERR_PARM_SIZE; } uint16_t wPort = static_cast(atoi(p + 1)); - if (EOK != strcpy_s(outIP, BUF_SIZE_TINY, bufString)) { + if (EOK != strcpy_s(outIP, outSize, bufString)) { return ERR_BUF_COPY; } *outPort = wPort; diff --git a/src/common/base.h b/src/common/base.h index d13b5c7b..070d9370 100644 --- a/src/common/base.h +++ b/src/common/base.h @@ -61,7 +61,7 @@ namespace Base { string GetRandomString(const uint16_t expectedLen); int GetRandomNum(const int min, const int max); uint64_t GetRandom(const uint64_t min = 0, const uint64_t max = UINT64_MAX); - int ConnectKey2IPPort(const char *connectKey, char *outIP, uint16_t *outPort); + int ConnectKey2IPPort(const char *connectKey, char *outIP, uint16_t *outPort, size_t outSize); // As an uv_work_cb it must keep the same as prototype int StartWorkThread(uv_loop_t *loop, uv_work_cb pFuncWorkThread, uv_after_work_cb pFuncAfterThread, void *pThreadData); diff --git a/src/host/client.cpp b/src/host/client.cpp index 228802a5..d629c515 100755 --- a/src/host/client.cpp +++ b/src/host/client.cpp @@ -311,7 +311,7 @@ int HdcClient::ExecuteCommand(const string &commandIn) { char ip[BUF_SIZE_TINY] = ""; uint16_t port = 0; - int ret = Base::ConnectKey2IPPort(channelHostPort.c_str(), ip, &port); + int ret = Base::ConnectKey2IPPort(channelHostPort.c_str(), ip, &port, sizeof(ip)); if (ret < 0) { WRITE_LOG(LOG_FATAL, "ConnectKey2IPPort %s failed with %d", channelHostPort.c_str(), ret); diff --git a/src/host/host_tcp.cpp b/src/host/host_tcp.cpp index 8b6df312..e6cb002f 100644 --- a/src/host/host_tcp.cpp +++ b/src/host/host_tcp.cpp @@ -126,7 +126,7 @@ HSession HdcHostTCP::ConnectDaemon(const string &connectKey, bool isCheck) { char ip[BUF_SIZE_TINY] = ""; uint16_t port = 0; - if (Base::ConnectKey2IPPort(connectKey.c_str(), ip, &port) < 0) { + if (Base::ConnectKey2IPPort(connectKey.c_str(), ip, &port, sizeof(ip)) < 0) { WRITE_LOG(LOG_FATAL, "ConnectKey2IPPort error connectKey:%s", Hdc::MaskString(connectKey).c_str()); return nullptr; } diff --git a/src/test/main.cpp b/src/test/main.cpp index 46f9fbda..513b927b 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -23,7 +23,7 @@ TEST(HdcBaseFunction, HandleNoneZeroInput) int argc = 0; GTEST_ASSERT_LE(1, Base::GetRuntimeMSec()); GTEST_ASSERT_LE(10, Base::GetRandomNum(10, 12)); - GTEST_ASSERT_EQ(0, Base::ConnectKey2IPPort("127.0.0.1:8080", bufString, &num)); + GTEST_ASSERT_EQ(0, Base::ConnectKey2IPPort("127.0.0.1:8080", bufString, &num, sizeof(bufString))); Base::SplitCommandToArgs("xx p1 p2 p3", &argc); GTEST_ASSERT_EQ(4, argc);