From 0618406a7dfa399df07854ba1e548df115f280cc Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sun, 3 Dec 2023 21:21:56 +0800 Subject: [PATCH] fix message Searialize memory leak --- src/plugins/c/c_message_plugin.cpp | 66 +++++++++++++++++++++++------- src/plugins/c/c_message_plugin.h | 2 + src/plugins/c/rpcs/crpc_client.cpp | 10 ++--- src/plugins/c/rpcs/crpc_server.cpp | 10 ++--- 4 files changed, 63 insertions(+), 25 deletions(-) diff --git a/src/plugins/c/c_message_plugin.cpp b/src/plugins/c/c_message_plugin.cpp index dbc43ad..f672aba 100644 --- a/src/plugins/c/c_message_plugin.cpp +++ b/src/plugins/c/c_message_plugin.cpp @@ -139,6 +139,8 @@ std::string CMessagePlugin::GenerateFunctionDeclaration(Message::Ptr message) { std::stringstream ss; + ss << message->id() << "* " << message->id() << "_New();" << std::endl; + ss << "void " << message->id() << "_Free(" << message->id() << "** message_ptr);" << std::endl; ss << "void " << message->id() << "_Init(" << message->id() << "* message);" << std::endl; ss << "void " << message->id() << "_Destroy(" << message->id() << "* message);" << std::endl; ss << std::endl; @@ -164,10 +166,10 @@ std::string CMessagePlugin::GenerateFunctionDefinition(Message::Ptr message) { std::stringstream ss; + ss << GenerateFunctionDefinitionNew(message); + ss << GenerateFunctionDefinitionFree(message); ss << GenerateFunctionDefinitionInit(message); ss << GenerateFunctionDefinitionDestroy(message); - ss << std::endl; - ss << GenerateFunctionDefinitionSerialize(message); ss << GenerateFunctionDefinitionDeserialize(message); ss << std::endl; @@ -314,12 +316,12 @@ CMessagePlugin::GenerateFunctionDefinitionSerialize(Message::Ptr message) ss << " return *buffer_size; " << std::endl; ss << " } while (0);" << std::endl; -// for (int idx = 0; idx < message->items().size(); idx++) { -// const auto &item = items[idx]; -// if (item->type() == Item::Type::kString) { -// ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl; -// } -// } + for (int idx = 0; idx < message->items().size(); idx++) { + const auto &item = items[idx]; + if (item->type() == Item::Type::kString) { + ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl; + } + } } ss << " return 0; " << std::endl; ss << "}" << std::endl; @@ -337,22 +339,24 @@ CMessagePlugin::GenerateFunctionDefinitionDeserialize(Message::Ptr message) ss << " size_t dump_sz = buffer_size;" << std::endl; // if (!message->items().empty()) { if (true) { - ss << " do {" << std::endl; - ss << " " << message->id() << "_Destroy(message);" << std::endl; - ss << " " << message->id() << "_Init(message);" << std::endl; auto items = message->items(); for (int idx = 0; idx < message->items().size(); idx++) { const auto &item = items[idx]; std::string type_name; if (item->type() == Item::Type::kInt32) { - ss << " int32_t item_" << idx << ";" << std::endl; + ss << " int32_t item_" << idx << ";" << std::endl; } else if (item->type() == Item::Type::kString) { - ss << " char* item_" << idx << " = NULL;" << std::endl; + ss << " char* item_" << idx << " = NULL;" << std::endl; } else if (item->type() == Item::Type::kMessage) { // type_name = "char*"; } } + + ss << " do {" << std::endl; + ss << " " << message->id() << "_Destroy(message);" << std::endl; + ss << " " << message->id() << "_Init(message);" << std::endl; + ss << std::endl; std::string tpl_format = GetTPLMap(message->items()); @@ -393,9 +397,14 @@ CMessagePlugin::GenerateFunctionDefinitionDeserialize(Message::Ptr message) // type_name = "char*"; } } - - ss << " } while (0);" << std::endl; + + for (int idx = 0; idx < message->items().size(); idx++) { + const auto &item = items[idx]; + if (item->type() == Item::Type::kString) { + ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl; + } + } } ss << " return 0; " << std::endl; ss << "}" << std::endl; @@ -422,6 +431,7 @@ CMessagePlugin::GenerateFunctionDefinitionDestroy(Message::Ptr message) { std::stringstream ss; ss << "void " << message->id() << "_Destroy(" << message->id() << "* message) {" << std::endl; + ss << " if (message == NULL) { return; }" << std::endl; for (auto &item : message->items()) { auto c_item = CItem::Create(item); ss << c_item->GenerateDestroyCodeBlock(message->id()) << std::endl; @@ -430,4 +440,30 @@ CMessagePlugin::GenerateFunctionDefinitionDestroy(Message::Ptr message) ss << std::endl; return ss.str(); } + +std::string +CMessagePlugin::GenerateFunctionDefinitionNew(Message::Ptr message) +{ + std::stringstream ss; + ss << message->id() << "* " << message->id() << "_New() {" << std::endl; + ss << " " << message->id() << "* message = (" << message->id() << "*)malloc(sizeof(" << message->id() << "));" << std::endl; + ss << " if (message == NULL) { return NULL; }" << std::endl; + ss << " " << message->id() << "_Init(message);" << std::endl; + ss << " return message;" << std::endl; + ss << "}" << std::endl; + return ss.str(); +} + +std::string +CMessagePlugin::GenerateFunctionDefinitionFree(Message::Ptr message) +{ + std::stringstream ss; + ss << "void " << message->id() << "_Free(" << message->id() << "** message_ptr) {" << std::endl; + ss << " if (!message_ptr) { return; }" << std::endl; + ss << " " << message->id() << "_Destroy(*message_ptr);" << std::endl; + ss << " free(*message_ptr);" << std::endl; + ss << " *message_ptr = NULL; " << std::endl; + ss << "}" << std::endl; + return ss.str(); +} }// namespace tqcq diff --git a/src/plugins/c/c_message_plugin.h b/src/plugins/c/c_message_plugin.h index 5aed4d7..c36ba01 100644 --- a/src/plugins/c/c_message_plugin.h +++ b/src/plugins/c/c_message_plugin.h @@ -29,6 +29,8 @@ private: std::string GenerateFunctionDeclaration(Message::Ptr message); std::string GenerateFunctionDefinition(Message::Ptr message); + std::string GenerateFunctionDefinitionNew(Message::Ptr message); + std::string GenerateFunctionDefinitionFree(Message::Ptr message); std::string GenerateFunctionDefinitionSerialize(Message::Ptr message); std::string GenerateFunctionDefinitionDeserialize(Message::Ptr message); std::string GenerateFunctionDefinitionInit(Message::Ptr message); diff --git a/src/plugins/c/rpcs/crpc_client.cpp b/src/plugins/c/rpcs/crpc_client.cpp index b30899c..817dd01 100644 --- a/src/plugins/c/rpcs/crpc_client.cpp +++ b/src/plugins/c/rpcs/crpc_client.cpp @@ -103,13 +103,12 @@ CRPCClient::GenerateStructAPIWrapper(std::string service_id) const ss << " do {" << std::endl; - ss << " response = (" << response_id() << " *)malloc(sizeof(" << response_id() << "));" << std::endl; + ss << " response = " << response_id() << "_New(); " << std::endl; ss << " if (response == NULL) {" << std::endl; ss << " status = -1;" << std::endl; ss << " break;" << std::endl; ss << " }" << std::endl; - ss << " " << response_id() << "_Init(response);" << std::endl; ss << " status = " << response_id() << "_Deserialize(response, msg, msg_len);" << std::endl; ss << " if (status != 0) {" << std::endl; ss << " " << response_id() << "_Destroy(response);" << std::endl; @@ -194,14 +193,15 @@ CRPCClient::GenerateCallFunctionDefinition(std::string service_id) const ss << " break;" << std::endl; ss << " }" << std::endl; ss << " Base64encode(base64_buffer, request_buffer, request_buffer_size);" << std::endl; - ss << " const char* service_name = (const char*)strdup(\"" << service_id << "::" << id() << "\");"<< std::endl; + ss << " const char* service_name = \"" << service_id << "::" << id() << "\";"<< std::endl; ss << " /* tpl pack A(ss)*/ " << std::endl; ss << " {" << std::endl; // ss << " const char* base64_buffer_ptr = strdup(base64_buffer); " << std::endl; ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer);" << std::endl; ss << " status = tpl_pack(tn, 1);" << std::endl; - ss << " if (status == -1) { break; }" << std::endl; + ss << " if (status == -1) { break; tpl_free(tn); }" << std::endl; ss << " tpl_dump(tn, TPL_MEM, &dump_buffer, &dump_sz);" << std::endl; + ss << " tpl_free(tn);" << std::endl; ss << " if (dump_sz < 0) { break; }" << std::endl; ss << " }" << std::endl; ss << " status = "<< service_id << "_Client_Send(client, dump_buffer, dump_sz);" << std::endl; @@ -223,7 +223,7 @@ CRPCClient::GenerateCallFunctionDefinition(std::string service_id) const ss << GenerateCallUnlock(service_id) << std::endl; ss << " free(request_buffer);" << std::endl; ss << " free(dump_buffer);" << std::endl; - // ss << " free(base64_buffer);" << std::endl; + ss << " free(base64_buffer);" << std::endl; ss << " return priv->ok ? 0 : -1;" << std::endl; ss << "}" << std::endl; return ss.str(); diff --git a/src/plugins/c/rpcs/crpc_server.cpp b/src/plugins/c/rpcs/crpc_server.cpp index b422681..9d2d361 100644 --- a/src/plugins/c/rpcs/crpc_server.cpp +++ b/src/plugins/c/rpcs/crpc_server.cpp @@ -82,12 +82,12 @@ CRPCServer::GenerateStructAPIWrapper(std::string service_id) const ss << " printf(\"" << id() << " failed: %d\\n\", status);" << std::endl; ss << " break;" << std::endl; ss << " }" << std::endl; - ss << " Base64encode(base64_buffer, response_msg, response_msg_len);" << std::endl; + ss << " base64_buffer_size = Base64encode(base64_buffer, response_msg, response_msg_len);" << std::endl; ss << " // pack" << std::endl; ss << " {" << std::endl; - ss << " const char* service_name = (const char*)strdup(\"" << service_id << "::" << id() << "\");" << std::endl; - // ss << " const char* base64_buffer_ptr = strdup(base64_buffer);" << std::endl; - ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer);" << std::endl; + ss << " const char* service_name = \"" << service_id << "::" << id() << "\";" << std::endl; + ss << " const char* base64_buffer_ptr = base64_buffer;" << std::endl; + ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer_ptr);" << std::endl; ss << " tpl_pack(tn, 1);" << std::endl; ss << " tpl_dump(tn, TPL_MEM, &buffer, &buffer_size);" << std::endl; ss << " tpl_free(tn);" << std::endl; @@ -101,7 +101,7 @@ CRPCServer::GenerateStructAPIWrapper(std::string service_id) const ss << " status = 0;" << std::endl; ss << " } while (0);" << std::endl; ss << " free(buffer);" << std::endl; - // ss << " free(base64_buffer);" << std::endl; +// ss << " free(base64_buffer);" << std::endl; ss << " free(response_msg);" << std::endl; ss << " " << response_id() << "_Destroy(&response);" << std::endl; ss << " " << request_id() << "_Destroy(&request);" << std::endl;