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 | 341 ++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 219 insertions(+), 122 deletions(-) diff --git a/src/bh_api.cpp b/src/bh_api.cpp index e36daee..4f623f8 100644 --- a/src/bh_api.cpp +++ b/src/bh_api.cpp @@ -41,13 +41,13 @@ }_input; ::bhome_msg::ProcInfo input; - if(!input.ParseFromArray(proc_info, proc_info_len)) { + if ((!input.ParseFromArray(proc_info, proc_info_len)) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } _input.proc_id = input.proc_id().c_str(); @@ -56,13 +56,13 @@ _input.private_info = input.private_info().c_str(); #else - if ((proc_info == NULL) || (proc_info_len == 0)) { + if ((proc_info == NULL) || (proc_info_len == 0) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x90, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } #endif @@ -77,7 +77,7 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } rv = pthread_mutex_trylock(&mutex); @@ -154,31 +154,29 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + return false; } - -exit_entry: + #if defined(PRO_DE_SERIALIZE) - ::bhome_msg::MsgCommonReply mcr; - mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); - mcr.mutable_errmsg()->set_errstring(errString); - *reply_len = mcr.ByteSizeLong(); - *reply = malloc(*reply_len); - mcr.SerializePartialToArray(*reply, *reply_len); + ::bhome_msg::MsgCommonReply mcr; + mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); + mcr.mutable_errmsg()->set_errstring(errString); + *reply_len = mcr.ByteSizeLong(); + *reply = malloc(*reply_len); + mcr.SerializePartialToArray(*reply, *reply_len); #else - min = strlen(errString) + 1; - buf = malloc(min) ; - memcpy(buf, errString, strlen(errString)); - *((char *)buf + min - 1) = '\0'; - - *reply = buf; - *reply_len = min; + min = strlen(errString) + 1; + buf = malloc(min) ; + memcpy(buf, errString, strlen(errString)); + *((char *)buf + min - 1) = '\0'; + + *reply = buf; + *reply_len = min; #endif - - if (rv == 0) - return true; - return false; + return true; + } int BHUnregister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms) @@ -198,20 +196,29 @@ ::bhome_msg::ProcInfo input; - if(!input.ParseFromArray(proc_info, proc_info_len)) { + if(!input.ParseFromArray(proc_info, proc_info_len) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } _input.proc_id = input.proc_id().c_str(); _input.name = input.name().c_str(); _input.public_info = input.public_info().c_str(); _input.private_info = input.private_info().c_str(); +#else + if ((reply == NULL) || (reply_len == NULL)) { + rv = EBUS_INVALID_PARA; + + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; + } #endif if (gRun_stat == 0) { @@ -222,7 +229,7 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } rv = pthread_mutex_trylock(&mutex); @@ -248,9 +255,10 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } -exit_entry: #if defined(PRO_DE_SERIALIZE) ::bhome_msg::MsgCommonReply mcr; mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); @@ -268,10 +276,8 @@ *reply_len = min; #endif - if (rv == 0) - return true; - - return false; + return true; + } int BHRegisterTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms) @@ -291,14 +297,14 @@ }_input; ::bhome_msg::MsgTopicList input; - if(!input.ParseFromArray(topics, topics_len)) { + if(!input.ParseFromArray(topics, topics_len) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } _input.amount = input.topic_list_size(); @@ -312,14 +318,14 @@ total += strlen(_input.topics[i]) + 1; } #else - if ((topics == NULL) || (topics_len == 0)) { + if ((topics == NULL) || (topics_len == 0) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } total = topics_len; @@ -332,7 +338,7 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } rv = pthread_mutex_trylock(&mutex); @@ -349,7 +355,7 @@ pthread_mutex_unlock(&mutex); - goto exit_entry; + return false; } memset(topics_buf, 0x00, total); @@ -385,9 +391,10 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } -exit_entry: #if defined(PRO_DE_SERIALIZE) ::bhome_msg::MsgCommonReply mcr; mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); @@ -405,10 +412,8 @@ *reply_len = len; #endif - if (rv == 0) - return true; + return true; - return false; } int BHQueryTopicAddress(const void *remote, const int remote_len, const void *topic, const int topic_len, void **reply, int *reply_len, const int timeout_ms) @@ -434,13 +439,13 @@ ::bhome_msg::BHAddress input0; ::bhome_msg::MsgQueryTopic input1; - if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(topic, topic_len)) { + if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(topic, topic_len) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } _input0.mq_id = input0.mq_id(); @@ -450,13 +455,13 @@ _input1 = input1.topic().c_str(); #else - if ((topic == NULL) || (topic_len == 0)) { + if ((topic == NULL) || (topic_len == 0) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } #endif @@ -467,7 +472,7 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } rv = pthread_mutex_trylock(&mutex); @@ -493,9 +498,10 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } -exit_entry: #if defined(PRO_DE_SERIALIZE) struct _MsgQueryTopicReply @@ -560,10 +566,8 @@ #endif - if (rv == 0) - return true; + return true; - return false; } int BHQueryProcs(const void *remote, const int remote_len, const void *query, const int query_len, void **reply, int *reply_len, const int timeout_ms) @@ -588,13 +592,13 @@ ::bhome_msg::BHAddress input0; ::bhome_msg::MsgQueryProc input1; - if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(query, query_len)) { + if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(query, query_len) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } _input0.mq_id = input0.mq_id(); @@ -602,6 +606,14 @@ _input0.ip = input0.ip().c_str(); _input0.port = input0.port(); _input1 = input1.proc_id().c_str(); +#else + if ((reply == NULL) || (reply_len == NULL)) { + rv = EBUS_INVALID_PARA; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; + } #endif if (gRun_stat == 0) { @@ -611,7 +623,7 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } rv = pthread_mutex_trylock(&mutex); @@ -633,9 +645,10 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } -exit_entry: #if defined(PRO_DE_SERIALIZE) struct _MsgQueryProcReply { @@ -718,10 +731,7 @@ } #endif - if (rv == 0) - return true; - - return false; + return true; } @@ -743,13 +753,13 @@ }_input; ::bhome_msg::MsgTopicList input; - if(!input.ParseFromArray(topics, topics_len)) { + if(!input.ParseFromArray(topics, topics_len) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } _input.amount = input.topic_list_size(); @@ -765,12 +775,12 @@ } #else - if ((topics == NULL) || (topics_len == 0)) { + if ((topics == NULL) || (topics_len == 0) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } #endif @@ -781,7 +791,7 @@ memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); - goto exit_entry; + return false; } rv = pthread_mutex_trylock(&mutex); @@ -798,7 +808,7 @@ pthread_mutex_unlock(&mutex); - goto exit_entry; + return false; } memset(topics_buf, 0x00, total); @@ -848,9 +858,10 @@ rv = EBUS_RES_BUSY; memset(errString, 0x00, sizeof(errString)); strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } -exit_entry: #if defined(PRO_DE_SERIALIZE) ::bhome_msg::MsgCommonReply mcr; mcr.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); @@ -868,10 +879,7 @@ *reply_len = len; #endif - if (rv == 0) - return true; - - return false; + return true; } @@ -1034,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 { @@ -1052,6 +1061,14 @@ strncpy(errString, bus_strerror(rv), sizeof(errString)); return false; + } + + if ((msgpub == NULL) || (msgpub_len == NULL)) { + rv = EBUS_INVALID_PARA; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; } if (timeout_ms > 0) { @@ -1075,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); } @@ -1087,15 +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); - rsr.proc_id = topics_buf; - *proc_id_len = rsr.proc_id.size(); - *proc_id = malloc(*proc_id_len); - memcpy(*proc_id, rsr.proc_id.data(), *proc_id_len); + if ((proc_id != NULL) && (proc_id_len != NULL)) { + 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); @@ -1115,13 +1172,18 @@ } *msgpub = ptr; *msgpub_len = len; - - memset(topics_buf, 0x00, sizeof(topics_buf)); - sprintf(topics_buf, "%d", key); - - *proc_id_len = strlen(topics_buf); - *proc_id = malloc(*proc_id_len); - memcpy(*proc_id, topics_buf, *proc_id_len); + + free(topics_buf); + free(data_buf); + + if ((proc_id != NULL) && (proc_id_len != NULL)) { + memset(buf_temp, 0x00, sizeof(buf_temp)); + sprintf(buf_temp, "%d", key); + + *proc_id_len = strlen(buf_temp); + *proc_id = malloc(*proc_id_len); + memcpy(*proc_id, buf_temp, *proc_id_len); + } #endif @@ -1291,9 +1353,10 @@ } if (rv == 0) { - memset(topics_buf, 0x00, sizeof(topics_buf)); - sprintf(topics_buf, "%d", val); - MsgID = topics_buf; + + memset(buf_temp, 0x00, sizeof(buf_temp)); + sprintf(buf_temp, "%d", val); + MsgID = buf_temp; *msg_id_len = MsgID.size(); *msg_id = malloc(*msg_id_len); @@ -1346,7 +1409,7 @@ ::bhome_msg::BHAddress input0; ::bhome_msg::MsgRequestTopic input1; - if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(request, request_len)) { + if (!input0.ParseFromArray(remote, remote_len) || !input1.ParseFromArray(request, request_len) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); @@ -1363,7 +1426,7 @@ _input1.data = input1.data().c_str(); #else - if ((request == NULL) || (request_len == 0)) { + if ((request == NULL) || (request_len == 0) || (reply == NULL) || (reply_len == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); @@ -1447,12 +1510,15 @@ size = recv_arr[0].content_length; buf = (char *)malloc(size); - memset(buf, 0x00, size); + if (buf == NULL) { + printf("Out of memory\n"); + + exit(0); + } + memset((char *)buf, 0x00, size); + strncpy((char *)buf, (char *)recv_arr[0].content, size); -#if !defined(PRO_DE_SERIALIZE) - *reply = buf; - *reply_len = size; -#endif + } net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size); @@ -1478,17 +1544,36 @@ strncpy(errString, bus_strerror(rv), sizeof(errString)); if (rv == 0) { - memset(buf_temp, 0x00, sizeof(buf_temp)); - sprintf(buf_temp, "%d", node.key); + if ((proc_id != NULL) && (proc_id_len != NULL)) { + memset(buf_temp, 0x00, sizeof(buf_temp)); + sprintf(buf_temp, "%d", node.key); - rr.proc_id = buf_temp; - *proc_id_len = rr.proc_id.size(); - *proc_id = malloc(*proc_id_len); - memcpy(*proc_id, rr.proc_id.data(), *proc_id_len); + rr.proc_id = buf_temp; + *proc_id_len = rr.proc_id.size(); + *proc_id = malloc(*proc_id_len); + memcpy(*proc_id, rr.proc_id.c_str(), *proc_id_len); + } - memset(buf_temp, 0x00, sizeof(buf_temp)); - memcpy(buf_temp, buf, size); - rr.data = buf_temp; + topics_buf = (char *)malloc(size + 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, size + 10); + + memcpy(topics_buf, buf, size); + rr.data = topics_buf; + + free(buf); + free(topics_buf); } pthread_mutex_unlock(&mutex); @@ -1500,16 +1585,18 @@ strncpy(errString, bus_strerror(rv), sizeof(errString)); } -#if defined(PRO_DE_SERIALIZE) - ::bhome_msg::MsgRequestTopicReply mrt; - mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); - mrt.mutable_errmsg()->set_errstring(errString); - mrt.set_data(rr.data.data()); - *reply_len = mrt.ByteSizeLong(); - *reply = malloc(*reply_len); - mrt.SerializePartialToArray(*reply, *reply_len); +#if defined(PRO_DE_SERIALIZE) + if (rv == 0) { + ::bhome_msg::MsgRequestTopicReply mrt; + mrt.mutable_errmsg()->set_errcode(::bhome_msg::ErrorCode(rv)); + mrt.mutable_errmsg()->set_errstring(errString); + mrt.set_data(rr.data.data()); + *reply_len = mrt.ByteSizeLong(); + *reply = malloc(*reply_len); + mrt.SerializePartialToArray(*reply, *reply_len); + } #else - if (rv > 0) { + if (rv == 0) { min = strlen(errString) + 1; buf = malloc(min); memcpy(buf, errString, strlen(errString)); @@ -1546,6 +1633,14 @@ return false; } + + if ((request == NULL) || (request_len == 0) || (src == NULL)) { + rv = EBUS_INVALID_PARA; + memset(errString, 0x00, sizeof(errString)); + strncpy(errString, bus_strerror(rv), sizeof(errString)); + + return false; + } if (timeout_ms > 0) { @@ -1572,12 +1667,14 @@ void *src; } rrr; - sprintf(buf_temp, "%d", key); - rrr.proc_id = buf_temp; + if ((proc_id != NULL) && (proc_id_len != NULL)) { + sprintf(buf_temp, "%d", key); + rrr.proc_id = buf_temp; - *proc_id_len = rrr.proc_id.size(); - *proc_id = malloc(*proc_id_len); - memcpy(*proc_id, rrr.proc_id.data(), *proc_id_len); + *proc_id_len = rrr.proc_id.size(); + *proc_id = malloc(*proc_id_len); + memcpy(*proc_id, rrr.proc_id.data(), *proc_id_len); + } topics_buf = (char *)malloc(size + MIN_STR_LEN); if (topics_buf == NULL) { @@ -1642,7 +1739,7 @@ #if defined(PRO_DE_SERIALIZE) ::bhome_msg::MsgRequestTopicReply input; - if (!input.ParseFromArray(reply, reply_len)) { +if (!input.ParseFromArray(reply, reply_len) || (src == NULL)) { rv = EBUS_INVALID_PARA; memset(errString, 0x00, sizeof(errString)); -- Gitblit v1.8.0