diff --git a/src/ipc.c b/src/ipc.c index 7a678c883..e0886b4a6 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -237,7 +237,7 @@ ipcParseSingleFrame(const char *msgBuf, ssize_t msgLen, req_parse_status_t *pars // There is no scope data if (msgLen <= dataOffset) { *parseStatus = REQ_PARSE_MISSING_SCOPE_DATA_ERROR; - goto cleanJson; + goto cleanMetadata; } // Calculate the scope data length size_t dataLen = msgLen - dataOffset; @@ -245,7 +245,7 @@ ipcParseSingleFrame(const char *msgBuf, ssize_t msgLen, req_parse_status_t *pars scopeMsg = scope_calloc(1, sizeof(char) * dataLen); if (!scopeMsg) { *parseStatus = REQ_PARSE_ALLOCATION_ERROR; - goto cleanJson; + goto cleanMetadata; } // Skip NUL char separator scope_memcpy(scopeMsg, msgBuf + dataOffset, dataLen); @@ -255,6 +255,9 @@ ipcParseSingleFrame(const char *msgBuf, ssize_t msgLen, req_parse_status_t *pars *parseStatus = REQ_PARSE_PARTIAL; } +cleanMetadata: + scope_free(metaData); + cleanJson: cJSON_Delete(msgJson); @@ -418,6 +421,7 @@ ipcSendFailedResponse(mqd_t mqDes, size_t msgBufSize, req_parse_status_t parseSt res = ipcSendFrameWithRetry(mqDes, metadataBytes, metadataLen); end: + scope_free(metadataBytes); cJSON_Delete(meta); return res; } @@ -502,13 +506,16 @@ ipcSendSuccessfulResponse(mqd_t mqDes, size_t msgBufSize, const char *scopeDataR return res; } char *scopeRespBytes = ipcRespScopeRespStr(scopeRespWrap); + if (!scopeRespBytes) { + goto destroyWrap; + } size_t scopeDataRemainLen = scope_strlen(scopeRespBytes); size_t scopeDataOffset = 0; // Allocate buffer to send out void *frame = scope_malloc(msgBufSize * sizeof(char)); if (!frame) { - goto destroyWrap; + goto destroyScopeRespStr; } while (scopeDataRemainLen) { @@ -524,6 +531,7 @@ ipcSendSuccessfulResponse(mqd_t mqDes, size_t msgBufSize, const char *scopeDataR // There is not sufficient place to use msg buffer if (metadataLen >= msgBufSize) { res = RESP_UNSUFFICENT_MSGBUF_ERROR; + scope_free(metadataBytes); cJSON_Delete(metadataJson); goto destroyFrame; } @@ -549,6 +557,7 @@ ipcSendSuccessfulResponse(mqd_t mqDes, size_t msgBufSize, const char *scopeDataR } scope_memcpy(frame, metadataBytes, metadataLen); + scope_free(metadataBytes); cJSON_Delete(metadataJson); // Copy the scope frame data @@ -567,6 +576,9 @@ ipcSendSuccessfulResponse(mqd_t mqDes, size_t msgBufSize, const char *scopeDataR destroyFrame: scope_free(frame); +destroyScopeRespStr: + scope_free(scopeRespBytes); + destroyWrap: ipcRespWrapperDestroy(scopeRespWrap); diff --git a/src/ipc_resp.c b/src/ipc_resp.c index f5c9b1f35..18edae2e8 100644 --- a/src/ipc_resp.c +++ b/src/ipc_resp.c @@ -26,12 +26,10 @@ extern log_t *g_log; extern mtc_t *g_mtc; extern ctl_t *g_ctl; -#define WRAP_PRIV_SIZE (2) - // Wrapper for scope message response +// TODO: replace scopeRespWrapper with cJSON struct scopeRespWrapper{ cJSON *resp; // Scope message response - void *priv[WRAP_PRIV_SIZE]; // Additional resources allocated to create response }; /* @@ -44,9 +42,6 @@ respWrapperCreate(void) { return NULL; } wrap->resp = NULL; - for (int i = 0; i < WRAP_PRIV_SIZE; ++i) { - wrap->priv[i] = NULL; - } return wrap; } @@ -56,12 +51,7 @@ respWrapperCreate(void) { void ipcRespWrapperDestroy(scopeRespWrapper *wrap) { if (wrap->resp) { - cJSON_free(wrap->resp); - } - for (int i = 0; i < WRAP_PRIV_SIZE; ++i) { - if (wrap->priv[i]) { - cJSON_free(wrap->priv[i]); - } + cJSON_Delete(wrap->resp); } scope_free(wrap); @@ -112,12 +102,12 @@ createCmdDesc(int id, const char *name) { } if (!cJSON_AddNumberToObject(cmdDesc, "id", id)) { - cJSON_free(cmdDesc); + cJSON_Delete(cmdDesc); return NULL; } if (!cJSON_AddStringToObject(cmdDesc, "name", name)) { - cJSON_free(cmdDesc); + cJSON_Delete(cmdDesc); return NULL; } @@ -150,11 +140,10 @@ ipcRespGetScopeCmds(const cJSON * unused) { goto allocFail; } - wrap->priv[0] = metaCmds; for (int id = 0; id < ARRAY_SIZE(cmdMetaName); ++id){ cJSON *singleCmd = createCmdDesc(id, cmdMetaName[id]); if (!singleCmd) { - goto allocFail; + goto metaCmdFail; } cJSON_AddItemToArray(metaCmds, singleCmd); } @@ -162,14 +151,13 @@ ipcRespGetScopeCmds(const cJSON * unused) { cJSON *scopeCmds = cJSON_CreateArray(); if (!scopeCmds) { - goto allocFail; + goto metaCmdFail; } - wrap->priv[1] = scopeCmds; for (int id = 0; id < ARRAY_SIZE(cmdScopeName); ++id){ cJSON *singleCmd = createCmdDesc(id, cmdScopeName[id]); if (!singleCmd) { - goto allocFail; + goto scopeCmdFail; } cJSON_AddItemToArray(scopeCmds, singleCmd); } @@ -177,6 +165,12 @@ ipcRespGetScopeCmds(const cJSON * unused) { return wrap; +metaCmdFail: + cJSON_Delete(metaCmds); + +scopeCmdFail: + cJSON_Delete(scopeCmds); + allocFail: ipcRespWrapperDestroy(wrap); return NULL; @@ -233,7 +227,6 @@ ipcRespGetScopeCfg(const cJSON *unused) { } return wrap; } - wrap->priv[0] = cfg; cJSON_AddItemToObjectCS(resp, "cfg", cfg); @@ -271,6 +264,7 @@ ipcProcessSetCfg(const cJSON *scopeReq) { char *cfgStr = cJSON_PrintUnformatted(cfgKey); config_t *cfg = cfgFromString(cfgStr); doAndReplaceConfig(cfg); + scope_free(cfgStr); res = TRUE; return res; } @@ -416,7 +410,7 @@ ipcRespGetTransportStatus(const cJSON *unused) { if (!interfaces) { goto allocFail; } - wrap->priv[0] = interfaces; + for (int index = 0; index < ARRAY_SIZE(scope_interfaces); ++index) { int interfaceIndex = index; // Skip preparing the interface info if it is disabled @@ -431,34 +425,40 @@ ipcRespGetTransportStatus(const cJSON *unused) { cJSON *singleInterface = cJSON_CreateObject(); if (!singleInterface) { - goto allocFail; + goto interfaceFail; } transport_status_t status = scope_interfaces[interfaceIndex].status(); if (!cJSON_AddStringToObject(singleInterface, "name", scope_interfaces[interfaceIndex].name)) { - goto allocFail; + cJSON_Delete(singleInterface); + goto interfaceFail; } if (!cJSON_AddStringToObject(singleInterface, "config", status.configString)) { - goto allocFail; + cJSON_Delete(singleInterface); + goto interfaceFail; } if (status.isConnected == TRUE) { if (!cJSON_AddTrueToObject(singleInterface, "connected")) { - goto allocFail; + cJSON_Delete(singleInterface); + goto interfaceFail; } } else { if (!cJSON_AddFalseToObject(singleInterface, "connected")) { - goto allocFail; + cJSON_Delete(singleInterface); + goto interfaceFail; } if (!cJSON_AddNumberToObject(singleInterface, "attempts", status.connectAttemptCount)) { - goto allocFail; + cJSON_Delete(singleInterface); + goto interfaceFail; } if (status.failureString) { if (!cJSON_AddStringToObject(singleInterface, "failure_details", status.failureString)) { - goto allocFail; + cJSON_Delete(singleInterface); + goto interfaceFail; } } } @@ -467,6 +467,9 @@ ipcRespGetTransportStatus(const cJSON *unused) { cJSON_AddItemToObjectCS(resp, "interfaces", interfaces); return wrap; +interfaceFail: + cJSON_Delete(interfaces); + allocFail: ipcRespWrapperDestroy(wrap); return NULL;