ref: bf93334b94968dc84bfabe1d1d1fcfb7fb643765
parent: 5e7f514b6f4d8474f2ed82550f1db37436835e66
author: Clownacy <Clownacy@users.noreply.github.com>
date: Sat Sep 12 20:41:32 EDT 2020
Fix out-of-bounds font atlas accesses
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -12,6 +12,8 @@
#include "File.h"
#include "Backends/Rendering.h"
+#define MAX(a,b) ((a) > (b) ? (a) : (b))
+
// Cave Story wasn't intended to use font anti-aliasing. It's only because Microsoft enabled it
// by default from Windows Vista onwards that the game started using it.
// Font anti-aliasing conflicts with the game's colour-keying, causing ugly artifacting around
@@ -1080,11 +1082,12 @@
size_t atlas_entry_width = FT_MulFix(font_object->face->bbox.xMax - font_object->face->bbox.xMin + 1, font_object->face->size->metrics.x_scale) / 64;
size_t atlas_entry_height = FT_MulFix(font_object->face->bbox.yMax - font_object->face->bbox.yMin + 1, font_object->face->size->metrics.y_scale) / 64;
- font_object->atlas_row_length = ceil(sqrt(atlas_entry_width * atlas_entry_height * TOTAL_GLYPH_SLOTS) / atlas_entry_width);
+ size_t atlas_columns = ceil(sqrt(atlas_entry_width * atlas_entry_height * TOTAL_GLYPH_SLOTS) / atlas_entry_width);
+ size_t atlas_rows = ceil(sqrt(atlas_entry_width * atlas_entry_height * TOTAL_GLYPH_SLOTS) / atlas_entry_height);
- size_t texture_size = font_object->atlas_row_length * atlas_entry_width;
+ font_object->atlas_row_length = atlas_columns;
- font_object->atlas = RenderBackend_CreateGlyphAtlas(texture_size);
+ font_object->atlas = RenderBackend_CreateGlyphAtlas(MAX(atlas_columns * atlas_entry_width, atlas_rows * atlas_entry_height));
if (font_object->atlas != NULL)
{