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.

fix(wwlib): handle degenerate LCW input and unterminated Format buffers

Olcay Seygana3032adb45af
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/lcw.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/lcw.cpp
index 1500e6788..8d0578b85 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/lcw.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/lcw.cpp
@@ -198,6 +198,25 @@ int LCW_Uncomp(void const * source, void * dest, unsigned long )
int LCW_Comp(void const * source, void * dest, int datasize)
{
int retval = 0;
+
+ /*
+ ** The assembler below opens by unconditionally emitting a "length of 1"
+ ** code plus the first source byte, and only then starts testing for the
+ ** end of the input. With one byte of input the source pointer is already
+ ** spent at that point, so it walks off the end and emits a second, bogus
+ ** byte. Handle the two degenerate sizes here rather than restructuring
+ ** the asm.
+ */
+ if (datasize <= 1) {
+ unsigned char * dest_ptr = (unsigned char *)dest;
+ if (datasize == 1) {
+ *dest_ptr++ = 0x81; // medium copy, one byte follows
+ *dest_ptr++ = *(unsigned char const *)source;
+ }
+ *dest_ptr++ = 0x80; // end of data
+ return ((int)(dest_ptr - (unsigned char *)dest));
+ }
+
#ifdef _WINDOWS
long inlen = 0;
long a1stdest = 0;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/widestring.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/widestring.cpp
index c3c86376d..464707b9e 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/widestring.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/widestring.cpp
@@ -253,7 +253,10 @@ WideStringClass::Format_Args (const WCHAR *format, const va_list & arg_list )
//
// Make a guess at the maximum length of the resulting string
//
- WCHAR temp_buffer[512] = { 0 };
+ // _vsnwprintf writes no terminator when the result fills the buffer
+ // exactly, so the array is one WCHAR longer than the count handed to it
+ // and the extra slot is left as the zero from the initialiser.
+ WCHAR temp_buffer[512 + 1] = { 0 };
//
// Format the string
@@ -287,7 +290,10 @@ WideStringClass::Format (const WCHAR *format, ...)
//
// Make a guess at the maximum length of the resulting string
//
- WCHAR temp_buffer[512] = { 0 };
+ // _vsnwprintf writes no terminator when the result fills the buffer
+ // exactly, so the array is one WCHAR longer than the count handed to it
+ // and the extra slot is left as the zero from the initialiser.
+ WCHAR temp_buffer[512 + 1] = { 0 };
//
// Format the string
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/wwstring.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/wwstring.cpp
index 83509c100..3d8f26df1 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/wwstring.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/wwstring.cpp
@@ -243,7 +243,10 @@ StringClass::Format_Args (const TCHAR *format, const va_list & arg_list )
//
// Make a guess at the maximum length of the resulting string
//
- TCHAR temp_buffer[512] = { 0 };
+ // _vsnprintf writes no terminator when the result fills the buffer
+ // exactly, so the array is one TCHAR longer than the count handed to it
+ // and the extra slot is left as the zero from the initialiser.
+ TCHAR temp_buffer[512 + 1] = { 0 };
int retval = 0;
//
@@ -278,7 +281,10 @@ StringClass::Format (const TCHAR *format, ...)
//
// Make a guess at the maximum length of the resulting string
//
- TCHAR temp_buffer[512] = { 0 };
+ // _vsnprintf writes no terminator when the result fills the buffer
+ // exactly, so the array is one TCHAR longer than the count handed to it
+ // and the extra slot is left as the zero from the initialiser.
+ TCHAR temp_buffer[512 + 1] = { 0 };
int retval = 0;
//