From 1c810ecad502bce208f164e9dc12d916905f827c Mon Sep 17 00:00:00 2001 From: Koncord Date: Fri, 27 Jan 2017 19:05:18 +0800 Subject: [PATCH] [Server] Fix memory leak in GetCellStateDescription() --- apps/openmw-mp/Script/Functions/Cells.cpp | 11 ++++++----- apps/openmw-mp/Script/Functions/Cells.hpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Cells.cpp b/apps/openmw-mp/Script/Functions/Cells.cpp index 5f718e306..55087822b 100644 --- a/apps/openmw-mp/Script/Functions/Cells.cpp +++ b/apps/openmw-mp/Script/Functions/Cells.cpp @@ -16,7 +16,7 @@ unsigned int CellFunctions::GetCellStateChangesSize(unsigned short pid) noexcept return player->cellStateChanges.count; } -char *CellFunctions::GetCellStateDescription(unsigned short pid, unsigned int i) noexcept +const char *CellFunctions::GetCellStateDescription(unsigned short pid, unsigned int i) noexcept { Player *player; GET_PLAYER(pid, player, ""); @@ -24,12 +24,13 @@ char *CellFunctions::GetCellStateDescription(unsigned short pid, unsigned int i) if (i >= player->cellStateChanges.count) return "invalid"; - std::string cellDescription = player->cellStateChanges.cells.at(i).getDescription(); + string cellDescription = player->cellStateChanges.cells.at(i).getDescription(); - char *cstrDescription = new char[cellDescription.length() + 1]; - std::strcpy(cstrDescription, cellDescription.c_str()); + static vector cstrDescription; + cstrDescription.reserve(cellDescription.size()); + strncpy(&cstrDescription[0], cellDescription.c_str(), cstrDescription.capacity()); - return cstrDescription; + return &cstrDescription[0]; } const char *CellFunctions::GetCell(unsigned short pid) noexcept diff --git a/apps/openmw-mp/Script/Functions/Cells.hpp b/apps/openmw-mp/Script/Functions/Cells.hpp index c1fd2b15b..cbefe7df7 100644 --- a/apps/openmw-mp/Script/Functions/Cells.hpp +++ b/apps/openmw-mp/Script/Functions/Cells.hpp @@ -23,7 +23,7 @@ class CellFunctions public: static unsigned int GetCellStateChangesSize(unsigned short pid) noexcept; - static char *GetCellStateDescription(unsigned short pid, unsigned int i) noexcept; + static const char *GetCellStateDescription(unsigned short pid, unsigned int i) noexcept; static const char *GetCell(unsigned short pid) noexcept; static void SetCell(unsigned short pid, const char *name) noexcept;