Browse Source

* Fixed a lot of memory leaks and added a few error checks.

pull/1/head
Sam Hocevar sam 18 years ago
parent
commit
ac7d345e90
2 changed files with 39 additions and 5 deletions
  1. +36
    -4
      src/figlet.c
  2. +3
    -1
      src/main.c

+ 36
- 4
src/figlet.c View File

@@ -28,6 +28,9 @@
#include "toilet.h" #include "toilet.h"
#include "figlet.h" #include "figlet.h"


#define STD_GLYPHS (127 - 32)
#define EXT_GLYPHS (STD_GLYPHS + 7)

struct figfont struct figfont
{ {
/* From the font format */ /* From the font format */
@@ -138,20 +141,31 @@ static struct figfont *open_font(void)
font->lookup = realloc(font->lookup, font->lookup = realloc(font->lookup,
(font->glyphs + 2048) * 2 * sizeof(int)); (font->glyphs + 2048) * 2 * sizeof(int));


if(font->glyphs < 127 - 32)
if(font->glyphs < STD_GLYPHS)
{ {
font->lookup[font->glyphs * 2] = 32 + font->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 }; 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 else
{ {
char number[10]; 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') if(number[1] == 'x')
sscanf(number, "%x", &font->lookup[font->glyphs * 2]); sscanf(number, "%x", &font->lookup[font->glyphs * 2]);
@@ -175,12 +189,29 @@ static struct figfont *open_font(void)


fclose(f); 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 */ /* Import buffer into canvas */
b = cucul_load_memory(data, i); b = cucul_load_memory(data, i);
font->image = cucul_import_canvas(b, "utf8"); font->image = cucul_import_canvas(b, "utf8");
cucul_free_buffer(b); cucul_free_buffer(b);
free(data); 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 /* Remove EOL characters. For now we ignore hardblanks, don’t do any
* smushing, nor any kind of error checking. */ * smushing, nor any kind of error checking. */
for(j = 0; j < font->height * font->glyphs; j++) 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) static void free_font(struct figfont *font)
{ {
cucul_free_canvas(font->image);
free(font->lookup); free(font->lookup);
free(font); free(font);
} }


+ 3
- 1
src/main.c View File

@@ -212,10 +212,12 @@ int main(int argc, char *argv[])
else else
cv = render_figlet(string, length); cv = render_figlet(string, length);


free(string);

if(!cv) if(!cv)
return -1; return -1;


/* Do gay stuff with our string (léopard) */
/* Apply optional effects to our string */
if(flag_metal) if(flag_metal)
filter_metal(cv); filter_metal(cv);
if(flag_gay) if(flag_gay)


Loading…
Cancel
Save