From c79144d0a0a3e7b7aef57ae024afe35e1ec49594 Mon Sep 17 00:00:00 2001 From: queyanwen Date: Fri, 22 Dec 2023 23:35:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=9C=A8=E7=99=BD=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E8=BF=9E=E6=8E=A5=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E7=AB=AF=E5=87=BA=E7=8E=B0=E5=AE=A2=E6=88=B7=E7=AB=AFcoredump?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pwrapic/src/sockclient.c | 18 ++++++++++++++---- pwrapis/src/server.c | 31 ++++++++++++++++++------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/pwrapic/src/sockclient.c b/pwrapic/src/sockclient.c index 0f40861..fdc3add 100644 --- a/pwrapic/src/sockclient.c +++ b/pwrapic/src/sockclient.c @@ -192,15 +192,25 @@ static void ProcessOtherMsg(PwrMsg *msg) static void RecvMsgFromSocket(void) { PwrMsg *msg = (PwrMsg *)malloc(sizeof(PwrMsg)); - if (!msg || ReadMsg(msg, sizeof(PwrMsg)) != PWR_SUCCESS) { - ReleasePwrMsg(&msg); + if (!msg) { + return; + } + bzero(msg, sizeof(PwrMsg)); + if (ReadMsg(msg, sizeof(PwrMsg)) != PWR_SUCCESS) { + free(msg); return; } if (msg->head.dataLen != 0) { char *msgcontent = malloc(msg->head.dataLen); - if (!msgcontent || ReadMsg(msgcontent, msg->head.dataLen) != PWR_SUCCESS) { - ReleasePwrMsg(&msg); + if (!msgcontent) { + free(msg); + return; + } + bzero(msgcontent, msg->head.dataLen); + if (ReadMsg(msgcontent, msg->head.dataLen) != PWR_SUCCESS) { + free(msg); + free(msgcontent); return; } msg->data = msgcontent; diff --git a/pwrapis/src/server.c b/pwrapis/src/server.c index 6c5cf6a..b3f3fa6 100644 --- a/pwrapis/src/server.c +++ b/pwrapis/src/server.c @@ -214,7 +214,7 @@ static void AcceptConnection(void) } SendEventToClient(newClientFd, credSocket.pid, (char *)eventInfo, - sizeof(PWR_COM_EventInfo) + strlen(info)); + sizeof(PWR_COM_EventInfo) + strlen(info) + 1); close(newClientFd); return; } @@ -270,16 +270,26 @@ static void ProcessRecvMsgFromClient(int clientIdx) // Get msg from connFd, send to service queue and waiting for processing int dstFd = g_pwrClients[clientIdx].fd; PwrMsg *msg = (PwrMsg *)malloc(sizeof(PwrMsg)); - if (!msg || ReadMsg(msg, sizeof(PwrMsg), dstFd, clientIdx) != PWR_SUCCESS) { - ReleasePwrMsg(&msg); + if (!msg) { + return; + } + bzero(msg, sizeof(PwrMsg)); + if (ReadMsg(msg, sizeof(PwrMsg), dstFd, clientIdx) != PWR_SUCCESS) { + free(msg); return; } Logger(DEBUG, MD_NM_SVR, "receivd msg. opt:%d,sysId:%d", msg->head.optType, msg->head.sysId); if (msg->head.dataLen > 0) { char *msgcontent = malloc(msg->head.dataLen); - if (!msgcontent || ReadMsg(msgcontent, msg->head.dataLen, dstFd, clientIdx) != PWR_SUCCESS) { - ReleasePwrMsg(&msg); + if (!msgcontent) { + free(msg); + return; + } + bzero(msgcontent, msg->head.dataLen); + if (ReadMsg(msgcontent, msg->head.dataLen, dstFd, clientIdx) != PWR_SUCCESS) { + free(msg); + free(msgcontent); return; } msg->data = msgcontent; @@ -366,17 +376,14 @@ static int SendEventToClient(const int dstFd, const uint32_t sysId, char *data, } PwrMsg *event = (PwrMsg *)malloc(sizeof(PwrMsg)); - char *dataCpy = (char *)malloc(len); - if (!event || !dataCpy) { + if (!event) { Logger(ERROR, MD_NM_SVR, "Malloc failed"); free(data); return PWR_ERR_SYS_EXCEPTION; } bzero(event, sizeof(PwrMsg)); - memset(dataCpy, 0, len); - memcpy(dataCpy, data, len); - int res = GenerateEventMsg(event, sysId, dataCpy, len); + int res = GenerateEventMsg(event, sysId, data, len); if (res != PWR_SUCCESS) { Logger(ERROR, MD_NM_SVR, "Generate event msg failed, result:%d", res); free(data); @@ -387,9 +394,7 @@ static int SendEventToClient(const int dstFd, const uint32_t sysId, char *data, SendMsgToClientAction(dstFd, event); Logger(INFO, MD_NM_SVR, "Send event notifcation success."); - free(data); - data = NULL; - ReleasePwrMsg(&event); + ReleasePwrMsg(&event); // This will free(data) return PWR_SUCCESS; } -- Gitee