From f91ca1a157104d43b98a6a1291cda6ff1310c8c6 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Mon, 3 Nov 2014 18:48:10 +0000 Subject: [PATCH] Fixed loot_get_plugin_messages bugs. Bugs fixed: * Non-null message pointer outputted when no messages are present. * Plugins with multiple messages output structures that point to uninitialised memory. --- src/api/api.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/api/api.cpp b/src/api/api.cpp index 714fd220..7cf7c0d4 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -717,6 +717,7 @@ LOOT_API unsigned int loot_get_plugin_messages(loot_db db, const char * const pl } delete[] db->extMessageArray; db->extMessageArray = nullptr; + db->extMessageArraySize = 0; } //Initialise output. @@ -730,17 +731,20 @@ LOOT_API unsigned int loot_get_plugin_messages(loot_db db, const char * const pl std::list temp(p.Messages()); pluginMessages.insert(pluginMessages.end(), temp.begin(), temp.end()); - db->extMessageArraySize = pluginMessages.size(); - try { - db->extMessageArray = new loot_message[db->extMessageArraySize]; - int i = 0; - for (const auto &message : pluginMessages) { - db->extMessageArray[i].type = message.Type(); - db->extMessageArray[i].message = ToNewCString(message.ChooseContent(loot::Language::any).Str()); + if (!pluginMessages.empty()) { + db->extMessageArraySize = pluginMessages.size(); + try { + db->extMessageArray = new loot_message[db->extMessageArraySize]; + int i = 0; + for (const auto &message : pluginMessages) { + db->extMessageArray[i].type = message.Type(); + db->extMessageArray[i].message = ToNewCString(message.ChooseContent(loot::Language::any).Str()); + ++i; + } + } + catch (std::bad_alloc& e) { + return c_error(loot_error_no_mem, e.what()); } - } - catch (std::bad_alloc& e) { - return c_error(loot_error_no_mem, e.what()); } *messages = db->extMessageArray;