fix: strcpy check output size.

Signed-off-by: TaowerfulMAX <liurantao@huawei.com>

q!
This commit is contained in:
TaowerfulMAX 2024-08-22 17:48:55 +08:00
parent 273c0765ec
commit 92655bc6a6
5 changed files with 6 additions and 6 deletions

View File

@ -681,7 +681,7 @@ void PrintLogEx(const char *functionName, int line, uint8_t logLevel, const char
return static_cast<int>(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<uint16_t>(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;

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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);