From 3f0c0ed87dc99a62989df86c57150986251abb5e Mon Sep 17 00:00:00 2001 From: Mike Erwin Date: Fri, 14 Oct 2016 19:56:50 -0400 Subject: BLF: fix crash on text input fields Some strings have known length, others are NULL terminated (len = INT_MAX in this case). Now font drawing knows to treat INT_MAX special. --- source/blender/blenfont/intern/blf_font.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'source/blender/blenfont') diff --git a/source/blender/blenfont/intern/blf_font.c b/source/blender/blenfont/intern/blf_font.c index 7d734653ffc..42717e65d9b 100644 --- a/source/blender/blenfont/intern/blf_font.c +++ b/source/blender/blenfont/intern/blf_font.c @@ -174,6 +174,21 @@ static void blf_font_ensure_ascii_table(FontBLF *font) } \ } (void)0 +static unsigned verts_needed(const FontBLF *font, const char *str, size_t len) +{ + unsigned length = (unsigned)((len == INT_MAX) ? strlen(str) : len); + return length * 40; + /* (5 shadow + 5 blur) * 4 verts per quad + * TODO: determine exact count of quads, somthing like this: */ +#if 0 + unsigned quad_ct = 1 + (unsigned)font->blur; + if (font->flags & BLF_SHADOW) + quad_ct += (unsigned)font->shadow; + + immBegin(GL_QUADS, length * quad_ct * 4); +#endif +} + static void blf_font_draw_ex( FontBLF *font, const char *str, size_t len, struct ResultBLF *r_info, int pen_y) @@ -189,16 +204,7 @@ static void blf_font_draw_ex( blf_font_ensure_ascii_table(font); - immBeginAtMost(GL_QUADS, (unsigned)len * 40); - /* (5 shadow + 5 blur) * 4 verts per quad - * TODO: determine exact count of quads, somthing like this: */ -#if 0 - unsigned quad_ct = 1 + (unsigned)font->blur; - if (font->flags & BLF_SHADOW) - quad_ct += (unsigned)font->shadow; - - immBegin(GL_QUADS, (unsigned)len * quad_ct * 4); -#endif + immBeginAtMost(GL_QUADS, verts_needed(font, str, len)); while ((i < len) && str[i]) { BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); @@ -244,7 +250,7 @@ static void blf_font_draw_ascii_ex( blf_font_ensure_ascii_table(font); - immBeginAtMost(GL_QUADS, (unsigned)len * 40); + immBeginAtMost(GL_QUADS, verts_needed(font, str, len)); while ((c = *(str++)) && len--) { BLI_assert(c < 128); @@ -284,7 +290,7 @@ int blf_font_draw_mono(FontBLF *font, const char *str, size_t len, int cwidth) blf_font_ensure_ascii_table(font); - immBeginAtMost(GL_QUADS, (unsigned)len * 40); + immBeginAtMost(GL_QUADS, verts_needed(font, str, len)); while ((i < len) && str[i]) { BLF_UTF8_NEXT_FAST(font, g, str, i, c, glyph_ascii_table); -- cgit v1.2.3