fix message Searialize memory leak
Some checks failed
build-crpc / build (Debug, host.toolchain.cmake) (push) Failing after 13m0s
build-crpc / build (Release, host.toolchain.cmake) (push) Failing after 12m23s

This commit is contained in:
tqcq 2023-12-03 21:21:56 +08:00
parent e5bed79e64
commit 0618406a7d
4 changed files with 63 additions and 25 deletions

View File

@ -139,6 +139,8 @@ std::string
CMessagePlugin::GenerateFunctionDeclaration(Message::Ptr message) CMessagePlugin::GenerateFunctionDeclaration(Message::Ptr message)
{ {
std::stringstream ss; 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() << "_Init(" << message->id() << "* message);" << std::endl;
ss << "void " << message->id() << "_Destroy(" << message->id() << "* message);" << std::endl; ss << "void " << message->id() << "_Destroy(" << message->id() << "* message);" << std::endl;
ss << std::endl; ss << std::endl;
@ -164,10 +166,10 @@ std::string
CMessagePlugin::GenerateFunctionDefinition(Message::Ptr message) CMessagePlugin::GenerateFunctionDefinition(Message::Ptr message)
{ {
std::stringstream ss; std::stringstream ss;
ss << GenerateFunctionDefinitionNew(message);
ss << GenerateFunctionDefinitionFree(message);
ss << GenerateFunctionDefinitionInit(message); ss << GenerateFunctionDefinitionInit(message);
ss << GenerateFunctionDefinitionDestroy(message); ss << GenerateFunctionDefinitionDestroy(message);
ss << std::endl;
ss << GenerateFunctionDefinitionSerialize(message); ss << GenerateFunctionDefinitionSerialize(message);
ss << GenerateFunctionDefinitionDeserialize(message); ss << GenerateFunctionDefinitionDeserialize(message);
ss << std::endl; ss << std::endl;
@ -314,12 +316,12 @@ CMessagePlugin::GenerateFunctionDefinitionSerialize(Message::Ptr message)
ss << " return *buffer_size; " << std::endl; ss << " return *buffer_size; " << std::endl;
ss << " } while (0);" << std::endl; ss << " } while (0);" << std::endl;
// for (int idx = 0; idx < message->items().size(); idx++) { for (int idx = 0; idx < message->items().size(); idx++) {
// const auto &item = items[idx]; const auto &item = items[idx];
// if (item->type() == Item::Type::kString) { if (item->type() == Item::Type::kString) {
// ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl; ss << " if (item_" << idx << ") { free(item_" << idx << "); }" << std::endl;
// } }
// } }
} }
ss << " return 0; " << std::endl; ss << " return 0; " << std::endl;
ss << "}" << std::endl; ss << "}" << std::endl;
@ -337,9 +339,6 @@ CMessagePlugin::GenerateFunctionDefinitionDeserialize(Message::Ptr message)
ss << " size_t dump_sz = buffer_size;" << std::endl; ss << " size_t dump_sz = buffer_size;" << std::endl;
// if (!message->items().empty()) { // if (!message->items().empty()) {
if (true) { if (true) {
ss << " do {" << std::endl;
ss << " " << message->id() << "_Destroy(message);" << std::endl;
ss << " " << message->id() << "_Init(message);" << std::endl;
auto items = message->items(); auto items = message->items();
for (int idx = 0; idx < message->items().size(); idx++) { for (int idx = 0; idx < message->items().size(); idx++) {
@ -353,6 +352,11 @@ CMessagePlugin::GenerateFunctionDefinitionDeserialize(Message::Ptr message)
// type_name = "char*"; // type_name = "char*";
} }
} }
ss << " do {" << std::endl;
ss << " " << message->id() << "_Destroy(message);" << std::endl;
ss << " " << message->id() << "_Init(message);" << std::endl;
ss << std::endl; ss << std::endl;
std::string tpl_format = GetTPLMap(message->items()); std::string tpl_format = GetTPLMap(message->items());
@ -393,9 +397,14 @@ CMessagePlugin::GenerateFunctionDefinitionDeserialize(Message::Ptr message)
// type_name = "char*"; // type_name = "char*";
} }
} }
ss << " } while (0);" << 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;
}
}
} }
ss << " return 0; " << std::endl; ss << " return 0; " << std::endl;
ss << "}" << std::endl; ss << "}" << std::endl;
@ -422,6 +431,7 @@ CMessagePlugin::GenerateFunctionDefinitionDestroy(Message::Ptr message)
{ {
std::stringstream ss; std::stringstream ss;
ss << "void " << message->id() << "_Destroy(" << message->id() << "* message) {" << std::endl; ss << "void " << message->id() << "_Destroy(" << message->id() << "* message) {" << std::endl;
ss << " if (message == NULL) { return; }" << std::endl;
for (auto &item : message->items()) { for (auto &item : message->items()) {
auto c_item = CItem::Create(item); auto c_item = CItem::Create(item);
ss << c_item->GenerateDestroyCodeBlock(message->id()) << std::endl; ss << c_item->GenerateDestroyCodeBlock(message->id()) << std::endl;
@ -430,4 +440,30 @@ CMessagePlugin::GenerateFunctionDefinitionDestroy(Message::Ptr message)
ss << std::endl; ss << std::endl;
return ss.str(); 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 }// namespace tqcq

View File

@ -29,6 +29,8 @@ private:
std::string GenerateFunctionDeclaration(Message::Ptr message); std::string GenerateFunctionDeclaration(Message::Ptr message);
std::string GenerateFunctionDefinition(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 GenerateFunctionDefinitionSerialize(Message::Ptr message);
std::string GenerateFunctionDefinitionDeserialize(Message::Ptr message); std::string GenerateFunctionDefinitionDeserialize(Message::Ptr message);
std::string GenerateFunctionDefinitionInit(Message::Ptr message); std::string GenerateFunctionDefinitionInit(Message::Ptr message);

View File

@ -103,13 +103,12 @@ CRPCClient::GenerateStructAPIWrapper(std::string service_id) const
ss << " do {" << std::endl; 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 << " if (response == NULL) {" << std::endl;
ss << " status = -1;" << std::endl; ss << " status = -1;" << std::endl;
ss << " break;" << std::endl; ss << " break;" << std::endl;
ss << " }" << std::endl; ss << " }" << std::endl;
ss << " " << response_id() << "_Init(response);" << std::endl;
ss << " status = " << response_id() << "_Deserialize(response, msg, msg_len);" << std::endl; ss << " status = " << response_id() << "_Deserialize(response, msg, msg_len);" << std::endl;
ss << " if (status != 0) {" << std::endl; ss << " if (status != 0) {" << std::endl;
ss << " " << response_id() << "_Destroy(response);" << 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 << " break;" << std::endl;
ss << " }" << std::endl; ss << " }" << std::endl;
ss << " Base64encode(base64_buffer, request_buffer, request_buffer_size);" << 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 << " /* tpl pack A(ss)*/ " << std::endl;
ss << " {" << std::endl; ss << " {" << std::endl;
// ss << " const char* base64_buffer_ptr = strdup(base64_buffer); " << 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 << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &base64_buffer);" << std::endl;
ss << " status = tpl_pack(tn, 1);" << 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_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 << " if (dump_sz < 0) { break; }" << std::endl;
ss << " }" << std::endl; ss << " }" << std::endl;
ss << " status = "<< service_id << "_Client_Send(client, dump_buffer, dump_sz);" << 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 << GenerateCallUnlock(service_id) << std::endl;
ss << " free(request_buffer);" << std::endl; ss << " free(request_buffer);" << std::endl;
ss << " free(dump_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 << " return priv->ok ? 0 : -1;" << std::endl;
ss << "}" << std::endl; ss << "}" << std::endl;
return ss.str(); return ss.str();

View File

@ -82,12 +82,12 @@ CRPCServer::GenerateStructAPIWrapper(std::string service_id) const
ss << " printf(\"" << id() << " failed: %d\\n\", status);" << std::endl; ss << " printf(\"" << id() << " failed: %d\\n\", status);" << std::endl;
ss << " break;" << std::endl; ss << " break;" << std::endl;
ss << " }" << 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 << " // pack" << std::endl;
ss << " {" << std::endl; ss << " {" << 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 << " const char* base64_buffer_ptr = strdup(base64_buffer);" << std::endl; ss << " const char* base64_buffer_ptr = base64_buffer;" << std::endl;
ss << " tpl_node* tn = tpl_map(\"A(ss)\", &service_name, &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_pack(tn, 1);" << std::endl;
ss << " tpl_dump(tn, TPL_MEM, &buffer, &buffer_size);" << std::endl; ss << " tpl_dump(tn, TPL_MEM, &buffer, &buffer_size);" << std::endl;
ss << " tpl_free(tn);" << 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 << " status = 0;" << std::endl;
ss << " } while (0);" << std::endl; ss << " } while (0);" << std::endl;
ss << " free(buffer);" << 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 << " free(response_msg);" << std::endl;
ss << " " << response_id() << "_Destroy(&response);" << std::endl; ss << " " << response_id() << "_Destroy(&response);" << std::endl;
ss << " " << request_id() << "_Destroy(&request);" << std::endl; ss << " " << request_id() << "_Destroy(&request);" << std::endl;