Fix out-of-bounds font atlas accesses

This commit is contained in:
Clownacy 2020-09-13 00:41:32 +01:00
parent 5e7f514b6f
commit bf93334b94

View file

@ -12,6 +12,8 @@
#include "File.h" #include "File.h"
#include "Backends/Rendering.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 // 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. // 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 // Font anti-aliasing conflicts with the game's colour-keying, causing ugly artifacting around
@ -1080,11 +1082,12 @@ FontObject* LoadFontFromData(const unsigned char *data, size_t data_size, size_t
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_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; 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) if (font_object->atlas != NULL)
{ {