From ac7d345e9039064b80859335342e01f19db4c117 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 30 Sep 2006 17:53:59 +0000 Subject: [PATCH] * Fixed a lot of memory leaks and added a few error checks. --- src/figlet.c | 40 ++++++++++++++++++++++++++++++++++++---- src/main.c | 4 +++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/figlet.c b/src/figlet.c index 7c7c119..575d5a7 100644 --- a/src/figlet.c +++ b/src/figlet.c @@ -28,6 +28,9 @@ #include "toilet.h" #include "figlet.h" +#define STD_GLYPHS (127 - 32) +#define EXT_GLYPHS (STD_GLYPHS + 7) + struct figfont { /* From the font format */ @@ -138,20 +141,31 @@ static struct figfont *open_font(void) font->lookup = realloc(font->lookup, (font->glyphs + 2048) * 2 * sizeof(int)); - if(font->glyphs < 127 - 32) + if(font->glyphs < STD_GLYPHS) { font->lookup[font->glyphs * 2] = 32 + font->glyphs; } - else if(font->glyphs < (127 - 32) + 7) + else if(font->glyphs < EXT_GLYPHS) { static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 }; - font->lookup[font->glyphs * 2] = tab[font->glyphs + (127 - 32)]; + font->lookup[font->glyphs * 2] = tab[font->glyphs - STD_GLYPHS]; } else { char number[10]; + int ret = fscanf(f, "%s %*[^\n]", number); + + if(ret == EOF) + break; - fscanf(f, "%s %*[^\n]", number); + if(!ret) + { + free(data); + free(font); + fprintf(stderr, "read error at glyph %u in `%s'\n", + font->glyphs, path); + return NULL; + } if(number[1] == 'x') sscanf(number, "%x", &font->lookup[font->glyphs * 2]); @@ -175,12 +189,29 @@ static struct figfont *open_font(void) fclose(f); + if(font->glyphs < EXT_GLYPHS) + { + free(data); + free(font); + fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n", + font->glyphs, path, EXT_GLYPHS); + return NULL; + } + /* Import buffer into canvas */ b = cucul_load_memory(data, i); font->image = cucul_import_canvas(b, "utf8"); cucul_free_buffer(b); free(data); + if(!font->image) + { + cucul_free_canvas(font->image); + free(font); + fprintf(stderr, "libcucul could not load data in `%s'\n", path); + return NULL; + } + /* Remove EOL characters. For now we ignore hardblanks, don’t do any * smushing, nor any kind of error checking. */ for(j = 0; j < font->height * font->glyphs; j++) @@ -214,6 +245,7 @@ static struct figfont *open_font(void) static void free_font(struct figfont *font) { + cucul_free_canvas(font->image); free(font->lookup); free(font); } diff --git a/src/main.c b/src/main.c index f0ca5b1..95981e7 100644 --- a/src/main.c +++ b/src/main.c @@ -212,10 +212,12 @@ int main(int argc, char *argv[]) else cv = render_figlet(string, length); + free(string); + if(!cv) return -1; - /* Do gay stuff with our string (léopard) */ + /* Apply optional effects to our string */ if(flag_metal) filter_metal(cv); if(flag_gay)