From fb3479aff259899eadc99a75de2474e3b9dbb0a9 Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Tue, 18 Jun 2019 17:49:49 +0200 Subject: Avoid "uninitialized memory" error in gl_draw As discussed in fltk.general, valgrind reported errors when gl_draw() is called and the text is converted to a texture (i.e. when testing whether the texture already exists). We need a length check to make sure we don't overrun text buffers. See threads "gl_draw" and "gl_draw - [General Use]", respectively, started on Jun 19, 2019. --- src/gl_draw.cxx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/gl_draw.cxx b/src/gl_draw.cxx index e45c870f3..5fe5822a8 100644 --- a/src/gl_draw.cxx +++ b/src/gl_draw.cxx @@ -263,6 +263,7 @@ private: char *utf8; //its text Fl_Font_Descriptor *fdesc; // its font float scale; // scaling factor of the GUI + int str_len; // the length of the utf8 text } data; data *fifo; // array of pile elements int size_; // pile height @@ -300,9 +301,11 @@ int gl_texture_fifo::already_known(const char *str, int n) { int rank; for ( rank = 0; rank <= last; rank++) { - if ( (memcmp(str, fifo[rank].utf8, n) == 0) && (fifo[rank].utf8[n] == 0) && - (fifo[rank].fdesc == gl_fontsize) && (fifo[rank].scale == gl_scale) ) { - return rank; + if ((fifo[rank].str_len == n) && + (fifo[rank].fdesc == gl_fontsize) && + (fifo[rank].scale == gl_scale) && + (memcmp(str, fifo[rank].utf8, n) == 0)) { + return rank; } } return -1; // means no texture exists yet for that string @@ -403,6 +406,7 @@ int gl_texture_fifo::compute_texture(const char* str, int n) fifo[current].utf8 = (char *)malloc(n + 1); memcpy(fifo[current].utf8, str, n); fifo[current].utf8[n] = 0; + fifo[current].str_len = n; // record length of text in utf8 fl_graphics_driver->font_descriptor(gl_fontsize); int w, h; w = fl_width(fifo[current].utf8, n) * gl_scale; -- cgit v1.2.3