From 0ccfe66212c95ca9cef5c41aed06071cbdfa0c24 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 27 Jan 2017 08:14:27 +0200 Subject: [PATCH] [Server] Send stable C string in GetCellStateDescription() --- apps/openmw-mp/Script/Functions/Cells.cpp | 11 ++++++++--- apps/openmw-mp/Script/Functions/Cells.hpp | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Cells.cpp b/apps/openmw-mp/Script/Functions/Cells.cpp index 221a0c376..5f718e306 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; } -const char* CellFunctions::GetCellStateDescription(unsigned short pid, unsigned int i) noexcept +char *CellFunctions::GetCellStateDescription(unsigned short pid, unsigned int i) noexcept { Player *player; GET_PLAYER(pid, player, ""); @@ -24,10 +24,15 @@ const char* CellFunctions::GetCellStateDescription(unsigned short pid, unsigned if (i >= player->cellStateChanges.count) return "invalid"; - return player->cellStateChanges.cells.at(i).getDescription().c_str(); + std::string cellDescription = player->cellStateChanges.cells.at(i).getDescription(); + + char *cstrDescription = new char[cellDescription.length() + 1]; + std::strcpy(cstrDescription, cellDescription.c_str()); + + return cstrDescription; } -const char* CellFunctions::GetCell(unsigned short pid) noexcept +const char *CellFunctions::GetCell(unsigned short pid) noexcept { Player *player; GET_PLAYER(pid, player, 0); diff --git a/apps/openmw-mp/Script/Functions/Cells.hpp b/apps/openmw-mp/Script/Functions/Cells.hpp index cbefe7df7..c1fd2b15b 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 const char *GetCellStateDescription(unsigned short pid, unsigned int i) noexcept; + static 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;