From f89fb303b5560c0466015928e57509cd2d072748 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 10 Apr 2020 19:57:12 +0200 Subject: [PATCH] Fixed memory issues with new Enum ToString handling. --- src/Enum.hpp | 2 +- tests/EnumTests.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Enum.hpp b/src/Enum.hpp index 272da49..8eae97c 100644 --- a/src/Enum.hpp +++ b/src/Enum.hpp @@ -63,7 +63,7 @@ switch (value) { MACRO_UTILS_FOR_EACH(ENUM_CASE, name, values) } \ auto v = static_cast(value); \ auto size = (int)((ceil(log10(v)) + 1) * sizeof(char)); \ - char* snum = new char[size]; \ + char* snum = new char[size + 1]; \ sprintf(snum, "%d", v); \ return snum; \ } \ diff --git a/tests/EnumTests.cpp b/tests/EnumTests.cpp index 86c74d4..7f0f320 100644 --- a/tests/EnumTests.cpp +++ b/tests/EnumTests.cpp @@ -56,7 +56,9 @@ TEST_CASE("Enum To String", "[Utilities]") { CHECK(strcmp(TestEnumHelper::ToString(TestEnum::Val1), "Val1") == 0); CHECK(strcmp(TestEnumHelper::ToString(TestEnum::Val2), "Val2") == 0); CHECK(strcmp(TestEnumHelper::ToString(TestEnum::Val3), "Val3") == 0); - CHECK(strcmp(TestEnumHelper::ToString((TestEnum)100), "100") == 0); + auto s = TestEnumHelper::ToString((TestEnum)100); + CHECK(strcmp(s, "100") == 0); + delete[] s; } TEST_CASE("Enum Get Values", "[Utilities]") {