Added security check in case init fails #1135

This commit is contained in:
raysan5 2020-03-17 20:57:01 +01:00
parent 2344941974
commit 4af4483f5f

View file

@ -504,10 +504,11 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
if (fileData != NULL) if (fileData != NULL)
{ {
// Init font for data reading int genFontChars = false;
stbtt_fontinfo fontInfo; stbtt_fontinfo fontInfo = { 0 };
if (!stbtt_InitFont(&fontInfo, fileData, 0)) TRACELOG(LOG_WARNING, "Failed to init font!");
if (stbtt_InitFont(&fontInfo, fileData, 0)) // Init font for data reading
{
// Calculate font scale factor // Calculate font scale factor
float scaleFactor = stbtt_ScaleForPixelHeight(&fontInfo, (float)fontSize); float scaleFactor = stbtt_ScaleForPixelHeight(&fontInfo, (float)fontSize);
@ -521,7 +522,7 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
// Fill fontChars in case not provided externally // Fill fontChars in case not provided externally
// NOTE: By default we fill charsCount consecutevely, starting at 32 (Space) // NOTE: By default we fill charsCount consecutevely, starting at 32 (Space)
int genFontChars = false;
if (fontChars == NULL) if (fontChars == NULL)
{ {
fontChars = (int *)RL_MALLOC(charsCount*sizeof(int)); fontChars = (int *)RL_MALLOC(charsCount*sizeof(int));
@ -585,6 +586,8 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
TRACELOGD("Character offsetY: %i", (int)((float)ascent*scaleFactor) + chY1); TRACELOGD("Character offsetY: %i", (int)((float)ascent*scaleFactor) + chY1);
*/ */
} }
}
else TRACELOG(LOG_WARNING, "Failed to init font!");
RL_FREE(fileData); RL_FREE(fileData);
if (genFontChars) RL_FREE(fontChars); if (genFontChars) RL_FREE(fontChars);