diff --git a/src/rpc/src/rpcMain.c b/src/rpc/src/rpcMain.c index 978fc5b8ec..e7861201d3 100644 --- a/src/rpc/src/rpcMain.c +++ b/src/rpc/src/rpcMain.c @@ -218,9 +218,9 @@ void *rpcOpen(const SRpcInit *pInit) { pRpc->localPort = pInit->localPort; pRpc->afp = pInit->afp; pRpc->sessions = pInit->sessions+1; - if (pInit->user) strcpy(pRpc->user, pInit->user); - if (pInit->secret) strcpy(pRpc->secret, pInit->secret); - if (pInit->ckey) strcpy(pRpc->ckey, pInit->ckey); + if (pInit->user) tstrncpy(pRpc->user, pInit->user, sizeof(pRpc->user)); + if (pInit->secret) memcpy(pRpc->secret, pInit->secret, sizeof(pRpc->secret)); + if (pInit->ckey) tstrncpy(pRpc->ckey, pInit->ckey, sizeof(pRpc->ckey)); pRpc->spi = pInit->spi; pRpc->cfp = pInit->cfp; pRpc->afp = pInit->afp; @@ -434,7 +434,8 @@ void rpcSendResponse(const SRpcMsg *pRsp) { } void rpcSendRedirectRsp(void *thandle, const SRpcIpSet *pIpSet) { - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; + memset(&rpcMsg, 0, sizeof(rpcMsg)); rpcMsg.contLen = sizeof(SRpcIpSet); rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); diff --git a/src/rpc/src/rpcTcp.c b/src/rpc/src/rpcTcp.c index defd277db1..5dd5e4015e 100644 --- a/src/rpc/src/rpcTcp.c +++ b/src/rpc/src/rpcTcp.c @@ -253,12 +253,14 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int num, void * if (pthread_mutex_init(&(pThreadObj->mutex), NULL) < 0) { tError("%s failed to init TCP client mutex(%s)", label, strerror(errno)); + free(pThreadObj); return NULL; } pThreadObj->pollFd = epoll_create(10); // size does not matter if (pThreadObj->pollFd < 0) { tError("%s failed to create TCP client epoll", label); + free(pThreadObj); return NULL; } @@ -269,6 +271,8 @@ void *taosInitTcpClient(uint32_t ip, uint16_t port, char *label, int num, void * int code = pthread_create(&(pThreadObj->thread), &thattr, taosProcessTcpData, (void *)(pThreadObj)); pthread_attr_destroy(&thattr); if (code != 0) { + close(pThreadObj->pollFd); + free(pThreadObj); tError("%s failed to create TCP read data thread(%s)", label, strerror(errno)); return NULL; } diff --git a/src/rpc/test/rclient.c b/src/rpc/test/rclient.c index ea1ebb5974..857b39dd82 100644 --- a/src/rpc/test/rclient.c +++ b/src/rpc/test/rclient.c @@ -14,6 +14,7 @@ */ #include "os.h" +#include "tutil.h" #include "tglobal.h" #include "rpcLog.h" #include "trpc.h" @@ -105,7 +106,7 @@ int main(int argc, char *argv[]) { if (strcmp(argv[i], "-p")==0 && i < argc-1) { ipSet.port[0] = atoi(argv[++i]); } else if (strcmp(argv[i], "-i") ==0 && i < argc-1) { - strcpy(ipSet.fqdn[0], argv[++i]); + tstrncpy(ipSet.fqdn[0], argv[++i], sizeof(ipSet.fqdn)); } else if (strcmp(argv[i], "-t")==0 && i < argc-1) { rpcInit.numOfThreads = atoi(argv[++i]); } else if (strcmp(argv[i], "-m")==0 && i < argc-1) { diff --git a/src/rpc/test/rsclient.c b/src/rpc/test/rsclient.c index 3b19d7a9ea..6f367bc19b 100644 --- a/src/rpc/test/rsclient.c +++ b/src/rpc/test/rsclient.c @@ -15,6 +15,7 @@ #include "os.h" +#include "tutil.h" #include "tglobal.h" #include "rpcLog.h" #include "trpc.h" @@ -106,7 +107,7 @@ int main(int argc, char *argv[]) { if (strcmp(argv[i], "-p")==0 && i < argc-1) { ipSet.port[0] = atoi(argv[++i]); } else if (strcmp(argv[i], "-i") ==0 && i < argc-1) { - strcpy(ipSet.fqdn[0], argv[++i]); + tstrncpy(ipSet.fqdn[0], argv[++i], sizeof(ipSet.fqdn)); } else if (strcmp(argv[i], "-t")==0 && i < argc-1) { rpcInit.numOfThreads = atoi(argv[++i]); } else if (strcmp(argv[i], "-m")==0 && i < argc-1) { diff --git a/src/rpc/test/rserver.c b/src/rpc/test/rserver.c index 4ce6a0a8cf..71e7c62b9e 100644 --- a/src/rpc/test/rserver.c +++ b/src/rpc/test/rserver.c @@ -69,6 +69,7 @@ void processShellMsg() { taosGetQitem(qall, &type, (void **)&pRpcMsg); rpcFreeCont(pRpcMsg->pCont); + memset(&rpcMsg, 0, sizeof(rpcMsg)); rpcMsg.pCont = rpcMallocCont(msgSize); rpcMsg.contLen = msgSize; rpcMsg.handle = pRpcMsg->handle; diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index 984df23879..1960dc2a2a 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -42,6 +42,11 @@ extern "C" { } \ } +#define tstrncpy(dst, src, size) do { \ + strncpy((dst), (src), (size)); \ + (dst)[(size) - 1] = 0; \ +} while (0); + #define tclose(x) taosCloseSocket(x) // Pointer p drift right by b bytes diff --git a/src/wal/test/waltest.c b/src/wal/test/waltest.c index cf9bf9542e..073dbf72af 100644 --- a/src/wal/test/waltest.c +++ b/src/wal/test/waltest.c @@ -15,6 +15,7 @@ //#define _DEFAULT_SOURCE #include "os.h" +#include "tutil.h" #include "tglobal.h" #include "tlog.h" #include "twal.h" @@ -45,7 +46,7 @@ int main(int argc, char *argv[]) { for (int i=1; i