diff options
| author | Ray <[email protected]> | 2022-08-18 15:11:23 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2022-08-18 15:11:23 +0200 |
| commit | 35c777ef2ccdad0b3a94b508ec13df5f6cd9ea49 (patch) | |
| tree | 2c594f081573d2e64b98110554603d878bb48f91 /src | |
| parent | 904c5051251ff348234de20ea2336bb17ae5f3e8 (diff) | |
| download | raylib-35c777ef2ccdad0b3a94b508ec13df5f6cd9ea49.tar.gz raylib-35c777ef2ccdad0b3a94b508ec13df5f6cd9ea49.zip | |
REVIEWED: Avoid crash on bad data provided
Diffstat (limited to 'src')
| -rw-r--r-- | src/rtext.c | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/rtext.c b/src/rtext.c index 40f6d3b5..ef5debd5 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -818,9 +818,12 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC // Unload font glyphs info data (RAM) void UnloadFontData(GlyphInfo *glyphs, int glyphCount) { - for (int i = 0; i < glyphCount; i++) UnloadImage(glyphs[i].image); + if (glyphs != NULL) + { + for (int i = 0; i < glyphCount; i++) UnloadImage(glyphs[i].image); - RL_FREE(glyphs); + RL_FREE(glyphs); + } } // Unload Font from GPU memory (VRAM) @@ -1142,7 +1145,7 @@ void DrawTextCodepoints(Font font, const int *codepoints, int count, Vector2 pos // Measure string width for default font int MeasureText(const char *text, int fontSize) { - Vector2 vec = { 0.0f, 0.0f }; + Vector2 textSize = { 0.0f, 0.0f }; // Check if default font has been loaded if (GetFontDefault().texture.id != 0) @@ -1151,15 +1154,19 @@ int MeasureText(const char *text, int fontSize) if (fontSize < defaultFontSize) fontSize = defaultFontSize; int spacing = fontSize/defaultFontSize; - vec = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)spacing); + textSize = MeasureTextEx(GetFontDefault(), text, (float)fontSize, (float)spacing); } - return (int)vec.x; + return (int)textSize.x; } // Measure string size for Font Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing) { + Vector2 textSize = { 0 }; + + if ((font.texture.id == 0) || (text == NULL)) return textSize; + int size = TextLength(text); // Get size in bytes of text int tempByteCounter = 0; // Used to count longer text line num chars int byteCounter = 0; @@ -1204,11 +1211,10 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing if (tempTextWidth < textWidth) tempTextWidth = textWidth; - Vector2 vec = { 0 }; - vec.x = tempTextWidth*scaleFactor + (float)((tempByteCounter - 1)*spacing); // Adds chars spacing to measure - vec.y = textHeight*scaleFactor; + textSize.x = tempTextWidth*scaleFactor + (float)((tempByteCounter - 1)*spacing); // Adds chars spacing to measure + textSize.y = textHeight*scaleFactor; - return vec; + return textSize; } // Get index position for a unicode character on font |
