!1485 fix: strcpy安全函数不规范用法整改

Merge pull request !1485 from liurantao/master
This commit is contained in:
openharmony_ci 2024-08-29 11:30:53 +00:00 committed by Gitee
commit 12e6afd992
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 6 additions and 6 deletions

View File

@ -707,7 +707,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))) {
@ -722,7 +722,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

@ -64,7 +64,7 @@ namespace Base {
#endif
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);