Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

build(wwlib): add strlcpy/strlcat, the bounded copies the port has none of

Olcay Seygan52600d00f612
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/stringex.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/stringex.h
new file mode 100644
index 000000000..d53dd70bd
--- /dev/null
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/stringex.h
@@ -0,0 +1,91 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** This program is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+//----------------------------------------------------------------------------
+// Bounded string copies.
+//
+// This codebase is full of strncpy/strcat into fixed buffers, and strncpy does
+// not terminate when the source fills the destination - so half of those call
+// sites truncate into an unterminated buffer and the next read runs off the
+// end. strlcpy/strlcat always terminate, and return the length the call
+// *wanted* to write, so a caller can tell truncation happened.
+//
+// Names and semantics are the well known BSD ones, so a call site converted
+// from strncpy/strcat reads the way anyone would expect it to.
+//----------------------------------------------------------------------------
+
+#pragma once
+
+#include <stddef.h>
+#include <string.h>
+
+// Number of characters before the first zero character.
+template<typename T> size_t strlen_t(const T *str)
+{
+ const T *begin = str;
+ while (*str)
+ ++str;
+ return static_cast<size_t>(str - begin);
+}
+
+// As strlen_t, but never looks past maxlen characters.
+template<typename T> size_t strnlen_t(const T *str, size_t maxlen)
+{
+ const T *begin = str;
+ const T *end = str + maxlen;
+ while (str < end && *str)
+ ++str;
+ return static_cast<size_t>(str - begin);
+}
+
+// Copies src into dst, at most dstsize-1 characters, and always terminates.
+// Returns the length of src - so a return >= dstsize means it was truncated.
+template<typename T> size_t strlcpy_t(T *dst, const T *src, size_t dstsize)
+{
+ const size_t srclen = strlen_t(src);
+ if (dstsize != 0)
+ {
+ const size_t copylen = (srclen >= dstsize) ? dstsize - 1 : srclen;
+ memcpy(dst, src, copylen * sizeof(T));
+ dst[copylen] = T(0);
+ }
+ return srclen; // length it tried to create
+}
+
+// Appends src to dst within dstsize, and always terminates.
+// Returns the length dst+src would have had - a return >= dstsize means truncation.
+template<typename T> size_t strlcat_t(T *dst, const T *src, size_t dstsize)
+{
+ const size_t dstlen = strnlen_t(dst, dstsize);
+ const size_t srclen = strlen_t(src);
+ if (dstlen == dstsize)
+ return dstsize + srclen; // no room at all, dst was not terminated
+
+ size_t copylen = dstsize - dstlen - 1;
+ if (copylen > srclen)
+ copylen = srclen;
+ if (copylen > 0)
+ memcpy(dst + dstlen, src, copylen * sizeof(T));
+ dst[dstlen + copylen] = T(0);
+ return dstlen + srclen; // length it tried to create
+}
+
+inline size_t strlcpy(char *dst, const char *src, size_t dstsize) { return strlcpy_t(dst, src, dstsize); }
+inline size_t strlcat(char *dst, const char *src, size_t dstsize) { return strlcat_t(dst, src, dstsize); }
+inline size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t dstsize) { return strlcpy_t(dst, src, dstsize); }
+inline size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t dstsize) { return strlcat_t(dst, src, dstsize); }
diff --git a/GeneralsMD/Code/Tests/test_wwlib.cpp b/GeneralsMD/Code/Tests/test_wwlib.cpp
index 23613b6c6..dcff44da1 100644
--- a/GeneralsMD/Code/Tests/test_wwlib.cpp
+++ b/GeneralsMD/Code/Tests/test_wwlib.cpp
@@ -27,6 +27,7 @@
#include "random.h"
#include "fixed.h"
#include "wwstring.h"
+#include "stringex.h"
#include "widestring.h"
#include "trim.h"
#include "nstrdup.h"
@@ -1376,3 +1377,95 @@ TEST(threadclass_stop_returns_promptly_when_called_unlocked)
CHECK(elapsed < 250);
}
+
+/* ---------------------------------------------------------------------------------------------
+ * stringex - bounded string copies.
+ *
+ * strncpy is the trap these replace: given a source at least as long as the destination it copies
+ * dstsize characters and writes no terminator, so every later read runs off the end of the buffer.
+ * strlcpy always terminates and reports the length it wanted, which is the only way a caller can
+ * tell that truncation happened.
+ * --------------------------------------------------------------------------------------------- */
+TEST(strlcpy_terminates_and_reports_the_length_it_wanted)
+{
+ char dst[8];
+
+ /* fits: full copy, terminated, return is the source length */
+ memset(dst, 'X', sizeof(dst));
+ CHECK_EQ(6u, (unsigned)strlcpy(dst, "abcdef", sizeof(dst)));
+ CHECK_STR("abcdef", dst);
+
+ /* exactly fills: still terminated inside the buffer */
+ memset(dst, 'X', sizeof(dst));
+ CHECK_EQ(7u, (unsigned)strlcpy(dst, "abcdefg", sizeof(dst)));
+ CHECK_STR("abcdefg", dst);
+ CHECK_EQ(0, (int)dst[7]);
+
+ /* too long: truncated, still terminated, and the return says so */
+ memset(dst, 'X', sizeof(dst));
+ CHECK_EQ(11u, (unsigned)strlcpy(dst, "abcdefghijk", sizeof(dst)));
+ CHECK_STR("abcdefg", dst);
+ CHECK(11u >= sizeof(dst)); /* return >= dstsize is the truncation signal */
+
+ /* a zero-sized destination is not written at all */
+ char guard[2] = { 'A', 'B' };
+ CHECK_EQ(3u, (unsigned)strlcpy(guard, "abc", 0));
+ CHECK_EQ('A', guard[0]);
+ CHECK_EQ('B', guard[1]);
+
+ /* empty source */
+ CHECK_EQ(0u, (unsigned)strlcpy(dst, "", sizeof(dst)));
+ CHECK_STR("", dst);
+}
+
+TEST(strlcpy_does_not_write_past_the_destination)
+{
+ /* The overflow this exists to stop: a long source into a short buffer must leave the byte
+ after the buffer untouched. */
+ struct { char buf[4]; char canary; } s;
+ s.canary = (char)0x7E;
+ CHECK_EQ(9u, (unsigned)strlcpy(s.buf, "123456789", sizeof(s.buf)));
+ CHECK_STR("123", s.buf);
+ CHECK_EQ((char)0x7E, s.canary);
+}
+
+TEST(strlcat_appends_within_the_buffer_and_terminates)
+{
+ char dst[8];
+
+ strcpy(dst, "abc");
+ CHECK_EQ(6u, (unsigned)strlcat(dst, "def", sizeof(dst)));
+ CHECK_STR("abcdef", dst);
+
+ /* appending past the end truncates and reports what it wanted */
+ strcpy(dst, "abcde");
+ CHECK_EQ(10u, (unsigned)strlcat(dst, "fghij", sizeof(dst)));
+ CHECK_STR("abcdefg", dst);
+
+ /* a full destination is left alone */
+ strcpy(dst, "abcdefg");
+ CHECK_EQ(10u, (unsigned)strlcat(dst, "hij", sizeof(dst)));
+ CHECK_STR("abcdefg", dst);
+
+ /* appending nothing changes nothing */
+ strcpy(dst, "abc");
+ CHECK_EQ(3u, (unsigned)strlcat(dst, "", sizeof(dst)));
+ CHECK_STR("abc", dst);
+}
+
+TEST(wcslcpy_and_wcslcat_count_characters_not_bytes)
+{
+ /* The template is instantiated per character type, so the wide forms have to bound by
+ character count - bounding by bytes would halve the usable buffer. */
+ wchar_t dst[8];
+
+ CHECK_EQ(6u, (unsigned)wcslcpy(dst, L"abcdef", 8));
+ CHECK(wcscmp(dst, L"abcdef") == 0);
+
+ CHECK_EQ(11u, (unsigned)wcslcpy(dst, L"abcdefghijk", 8));
+ CHECK(wcscmp(dst, L"abcdefg") == 0);
+
+ wcscpy(dst, L"abc");
+ CHECK_EQ(6u, (unsigned)wcslcat(dst, L"def", 8));
+ CHECK(wcscmp(dst, L"abcdef") == 0);
+}