Skip to content

Commit 06e608b

Browse files
authored
Merge pull request #716 from zzam/format-game-save
Fix Format function to not cut of last character (fixes game save)
2 parents ac030c8 + c861882 commit 06e608b

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Diff for: CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ set(stratagus_tests_SRCS
535535
tests/main.cpp
536536
tests/stratagus/test_action_built.cpp
537537
tests/stratagus/test_depend.cpp
538+
tests/stratagus/test_format.cpp
538539
tests/stratagus/test_luacallback.cpp
539540
tests/stratagus/test_missile_fire.cpp
540541
tests/stratagus/test_trigger.cpp

Diff for: src/include/stratagus.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ std::string Format(const char *format, Ts... args)
118118
#endif
119119
const auto len = snprintf(nullptr, 0, format, args...);
120120
std::string res(len, '\0');
121-
snprintf(res.data(), res.size(), format, args...);
121+
// len+1: writing a '\0' over the existing '\0' should be fine
122+
snprintf(res.data(), len + 1, format, args...);
122123
#ifndef _MSC_VER
123124
# pragma GCC diagnostic pop
124125
#endif

Diff for: tests/stratagus/test_format.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// _________ __ __
2+
// / _____// |_____________ _/ |______ ____ __ __ ______
3+
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4+
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5+
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6+
// \/ \/ \//_____/ \/
7+
// ______________________ ______________________
8+
// T H E W A R B E G I N S
9+
// Stratagus - A free fantasy real time strategy game engine
10+
//
11+
/**@name test_format.cpp - Test file for function Format. */
12+
//
13+
// (c) Copyright 2024 by Matthias Schwarzott
14+
//
15+
// This program is free software; you can redistribute it and/or modify
16+
// it under the terms of the GNU General Public License as published by
17+
// the Free Software Foundation; only version 2 of the License.
18+
//
19+
// This program is distributed in the hope that it will be useful,
20+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
// GNU General Public License for more details.
23+
//
24+
// You should have received a copy of the GNU General Public License
25+
// along with this program; if not, write to the Free Software
26+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27+
// 02111-1307, USA.
28+
//
29+
30+
#include <doctest.h>
31+
32+
#include "stratagus.h"
33+
34+
TEST_CASE("Format")
35+
{
36+
CHECK("" == Format(""));
37+
CHECK("1" == Format("1"));
38+
CHECK("12" == Format("12"));
39+
CHECK("12\n" == Format("12\n"));
40+
CHECK("abc\n" == Format("%s", "abc\n"));
41+
CHECK("abc\n" == Format("%s\n", "abc"));
42+
CHECK("12345" == Format("%d", 12345));
43+
CHECK(" x" == Format("%*s", 10, "x"));
44+
std::string result = Format("%*s", 100, "x");
45+
REQUIRE(result.size() == 100);
46+
CHECK(result[98] == ' ');
47+
CHECK(result[99] == 'x');
48+
}

0 commit comments

Comments
 (0)