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(ww3d2): stop a large font from corrupting the heap

Olcay Seygan6c92e2d21013
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/GUI/W3DGameFont.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/GUI/W3DGameFont.cpp
index b9d42794e..355e49680 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/GUI/W3DGameFont.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/GUI/W3DGameFont.cpp
@@ -60,6 +60,9 @@
// PRIVATE DATA ///////////////////////////////////////////////////////////////
+// the biggest font WW3D2's glyph cache can build; loadFontData below says why
+enum { FONT_MAX_POINT_SIZE = 240 };
+
// PUBLIC DATA ////////////////////////////////////////////////////////////////
// PRIVATE PROTOTYPES /////////////////////////////////////////////////////////
@@ -82,12 +85,20 @@ Bool W3DFontLibrary::loadFontData( GameFont *font )
// 100 points was "probably wrong" on a 1024x768 screen. On a 4K display the interface scales
// past it and the font simply fails to load, which is a missing string on screen. A size of
// zero is still refused - that one really is wrong.
- if (font->pointSize < 1)
+ //
+ // The ceiling is real, though, and it is the glyph cache's rather than a guess about screens.
+ // A character is rasterised into a CHAR_BUFFER_LEN buffer and blitted into a texture at most
+ // 512 square (render2dsentence.h), and neither is checked on the way in: at 184 points a digit
+ // measures 280 pixels tall, a wide glyph writes past the end of the buffer, and the process
+ // dies of a corrupt heap. 240 points is the largest that leaves every character inside both.
+ const Int pointSize = font->pointSize < FONT_MAX_POINT_SIZE ? font->pointSize : FONT_MAX_POINT_SIZE;
+
+ if (pointSize < 1)
fontChar = NULL;
else
{ // get the font data from the asset manager
fontChar = WW3DAssetManager::
- Get_Instance()->Get_FontChars( font->nameString.str(), font->pointSize,
+ Get_Instance()->Get_FontChars( font->nameString.str(), pointSize,
font->bold ? true : false );
}
@@ -110,11 +121,11 @@ Bool W3DFontLibrary::loadFontData( GameFont *font )
// load unicode of same point size
if(TheGlobalLanguageData)
unicodeFontChar = WW3DAssetManager::
- Get_Instance()->Get_FontChars( TheGlobalLanguageData->m_unicodeFontName.str(), font->pointSize,
+ Get_Instance()->Get_FontChars( TheGlobalLanguageData->m_unicodeFontName.str(), pointSize,
font->bold ? true : false );
else
unicodeFontChar = WW3DAssetManager::
- Get_Instance()->Get_FontChars( "Arial Unicode MS", font->pointSize,
+ Get_Instance()->Get_FontChars( "Arial Unicode MS", pointSize,
font->bold ? true : false );
if ( unicodeFontChar )
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp
index 3742b3757..c3e086050 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp
@@ -655,9 +655,15 @@ Render2DSentenceClass::Allocate_New_Surface (const WCHAR *text, bool justCalcExt
//
// Find the best texture size for the remaining text
//
+ //
+ // 64 to 512 square. EA stopped at 256, which fits a line of text up to 255 pixels tall; a
+ // font past that gave every candidate size a rows_per_texture of zero, so none of them was
+ // ever picked, the 256 default stood, and the glyph was blitted outside the surface it was
+ // locked into. 512 covers every point size loadFontData will now build.
+ //
CurrTextureSize = 256;
int best_tex_mem_usage = 999999999;
- for (int pow2 = 6; pow2 <= 8; pow2 ++) {
+ for (int pow2 = 6; pow2 <= 9; pow2 ++) {
int size = 1 << pow2;
int row_count = (text_width / size) + 1;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.h
index 9dff6718c..95ab80116 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.h
@@ -65,7 +65,13 @@ public:
uint16 * Buffer;
};
-enum { CHAR_BUFFER_LEN = 32768 };
+// One glyph is rasterised straight into this buffer, width times height 16-bit pixels, and nothing
+// on the way in asks whether it fits: Update_Current_Buffer only starts a new buffer when the
+// *offset* would run past the end, so a character bigger than the whole buffer walks off it and
+// corrupts the heap. EA's 32768 holds a glyph up to about 190 pixels square, which is a 128 point
+// font - reachable now that the interface scales its text with the screen. Four times that covers
+// a 360 pixel glyph, and W3DFontLibrary::loadFontData caps the point size to match.
+enum { CHAR_BUFFER_LEN = 131072 };
class FontCharsBuffer : public W3DMPO
{