From 2b1f534ea407a8ec0b94e0389a360f6cdff54598 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期三, 08 九月 2021 14:02:38 +0800 Subject: [PATCH] call C.BHFree after return success --- src/bh_api.cpp | 72 ++++++++++++++++++++++++++++------- 1 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/bh_api.cpp b/src/bh_api.cpp index 4a3b7a8..4f623f8 100644 --- a/src/bh_api.cpp +++ b/src/bh_api.cpp @@ -1042,8 +1042,9 @@ int key; int size; int sec, nsec; - char topics_buf[MAX_STR_LEN] = { 0x00 }; - char data_buf[MAX_STR_LEN * 3] = { 0x00 }; + char buf_temp[100] = { 0x00 }; + char *topics_buf = NULL; + char *data_buf = NULL; struct _ReadSubReply { @@ -1091,11 +1092,43 @@ if (len > size) { len = size; } - strncpy(topics_buf, (char *)buf, len > (sizeof(topics_buf) - 1) ? (sizeof(topics_buf) - 1) : len); + + topics_buf = (char *)malloc(len + 10); + if (topics_buf == NULL) { + + rv = EBUS_NO_MEM; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + logger->error("in BHRequest: Out of memory!\n"); + + pthread_mutex_unlock(&mutex); + + return false; + } + memset(topics_buf, 0x00, len + 10); + + strncpy(topics_buf, (char *)buf, len); if (len < size) { - len = strlen(topics_buf) + 1; + + data_buf = (char *)malloc(size - len + 10); + if (data_buf == NULL) { + + rv = EBUS_NO_MEM; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + logger->error("in BHRequest: Out of memory!\n"); + + free(topics_buf); + pthread_mutex_unlock(&mutex); + return false; + } + memset(data_buf, 0x00, size - len + 10); + + len = strlen(topics_buf) + 1; strncpy(data_buf, (char *)buf + len, size - len); } @@ -1103,17 +1136,23 @@ #if defined(PRO_DE_SERIALIZE) rsr.topic = topics_buf; - rsr.data = data_buf; + if (data_buf != NULL) { + rsr.data = data_buf; + } else { + rsr.data = topics_buf; + } - memset(topics_buf, 0x00, sizeof(topics_buf)); - sprintf(topics_buf, "%d", key); + sprintf(buf_temp, "%d", key); if ((proc_id != NULL) && (proc_id_len != NULL)) { - rsr.proc_id = topics_buf; + rsr.proc_id = buf_temp; *proc_id_len = rsr.proc_id.size(); *proc_id = malloc(*proc_id_len); memcpy(*proc_id, rsr.proc_id.data(), *proc_id_len); } + + free(topics_buf); + free(data_buf); ::bhome_msg::MsgPublish Mp; Mp.set_topic(rsr.topic); @@ -1133,14 +1172,17 @@ } *msgpub = ptr; *msgpub_len = len; - + + free(topics_buf); + free(data_buf); + if ((proc_id != NULL) && (proc_id_len != NULL)) { - memset(topics_buf, 0x00, sizeof(topics_buf)); - sprintf(topics_buf, "%d", key); + memset(buf_temp, 0x00, sizeof(buf_temp)); + sprintf(buf_temp, "%d", key); - *proc_id_len = strlen(topics_buf); + *proc_id_len = strlen(buf_temp); *proc_id = malloc(*proc_id_len); - memcpy(*proc_id, topics_buf, *proc_id_len); + memcpy(*proc_id, buf_temp, *proc_id_len); } #endif @@ -1512,7 +1554,7 @@ memcpy(*proc_id, rr.proc_id.c_str(), *proc_id_len); } - topics_buf = (char *)malloc(size); + topics_buf = (char *)malloc(size + 10); if (topics_buf == NULL) { rv = EBUS_NO_MEM; @@ -1525,7 +1567,7 @@ return false; } - memset(topics_buf, 0x00, size); + memset(topics_buf, 0x00, size + 10); memcpy(topics_buf, buf, size); rr.data = topics_buf; -- Gitblit v1.8.0