Ignore:
Timestamp:
Sep 30, 2006, 7:53:59 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Fixed a lot of memory leaks and added a few error checks.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • toilet/trunk/src/figlet.c

    r1143 r1150  
    2929#include "figlet.h"
    3030
     31#define STD_GLYPHS (127 - 32)
     32#define EXT_GLYPHS (STD_GLYPHS + 7)
     33
    3134struct figfont
    3235{
     
    139142                                   (font->glyphs + 2048) * 2 * sizeof(int));
    140143
    141         if(font->glyphs < 127 - 32)
     144        if(font->glyphs < STD_GLYPHS)
    142145        {
    143146            font->lookup[font->glyphs * 2] = 32 + font->glyphs;
    144147        }
    145         else if(font->glyphs < (127 - 32) + 7)
     148        else if(font->glyphs < EXT_GLYPHS)
    146149        {
    147150            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
    148             font->lookup[font->glyphs * 2] = tab[font->glyphs + (127 - 32)];
     151            font->lookup[font->glyphs * 2] = tab[font->glyphs - STD_GLYPHS];
    149152        }
    150153        else
    151154        {
    152155            char number[10];
    153 
    154             fscanf(f, "%s %*[^\n]", number);
     156            int ret = fscanf(f, "%s %*[^\n]", number);
     157
     158            if(ret == EOF)
     159                break;
     160
     161            if(!ret)
     162            {
     163                free(data);
     164                free(font);
     165                fprintf(stderr, "read error at glyph %u in `%s'\n",
     166                                font->glyphs, path);
     167                return NULL;
     168            }
    155169
    156170            if(number[1] == 'x')
     
    175189
    176190    fclose(f);
     191
     192    if(font->glyphs < EXT_GLYPHS)
     193    {
     194        free(data);
     195        free(font);
     196        fprintf(stderr, "only %u glyphs in `%s', expected at least %u\n",
     197                        font->glyphs, path, EXT_GLYPHS);
     198        return NULL;
     199    }
    177200
    178201    /* Import buffer into canvas */
     
    181204    cucul_free_buffer(b);
    182205    free(data);
     206
     207    if(!font->image)
     208    {
     209        cucul_free_canvas(font->image);
     210        free(font);
     211        fprintf(stderr, "libcucul could not load data in `%s'\n", path);
     212        return NULL;
     213    }
    183214
    184215    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
     
    215246static void free_font(struct figfont *font)
    216247{
     248    cucul_free_canvas(font->image);
    217249    free(font->lookup);
    218250    free(font);
Note: See TracChangeset for help on using the changeset viewer.