aboutsummaryrefslogtreecommitdiff
path: root/host
diff options
context:
space:
mode:
authorjaseg <jaseg@jaseg.net>2014-01-10 12:40:45 +0100
committerjaseg <jaseg@jaseg.net>2014-01-10 12:40:45 +0100
commit8c0d16a46e7c4a40224f12016f9cf6d7318d9e99 (patch)
tree91ed3014e6524dcf140b789a39b478dcc120053b /host
parent1e72c8221210bbc24fd34d5cab75818193f2d962 (diff)
downloadmatelight-8c0d16a46e7c4a40224f12016f9cf6d7318d9e99.tar.gz
matelight-8c0d16a46e7c4a40224f12016f9cf6d7318d9e99.tar.bz2
matelight-8c0d16a46e7c4a40224f12016f9cf6d7318d9e99.zip
Fixed some resource leaks. Thanks, cppcheck!
Diffstat (limited to 'host')
-rw-r--r--host/main.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/host/main.c b/host/main.c
index 07a99dd..a8c5231 100644
--- a/host/main.c
+++ b/host/main.c
@@ -346,6 +346,8 @@ void console_render_buffer(framebuffer_t *fb){
}
int main(int argc, char **argv){
+ FILE *f = NULL;
+
if(argc != 2){
fprintf(stderr, "No or too much input text given\n");
return 1;
@@ -359,7 +361,8 @@ int main(int argc, char **argv){
glyph_t* glyph_table[BLP_SIZE];
if(read_bdf(fontfile, glyph_table, BLP_SIZE)){
fprintf(stderr, "Error reading font file.\n");
- return 1;
+ fclose(fontfile);
+ goto error;
}
fclose(fontfile);
@@ -368,10 +371,10 @@ int main(int argc, char **argv){
gifAnimationState_t *gifstate = NULL;
char *p = strstr(argv[1], ".gif");
if(p && p[4] == '\0'){ /* Argument ends with ".gif", try to read it as a gif file */
- FILE *f = fopen(argv[1], "r");
+ f = fopen(argv[1], "r");
if(!f){
fprintf(stderr, "Error opening gif file from argument (\"%s\"): %s\n", argv[1], strerror(errno));
- return 1;
+ goto error;
}
uint8_t *buf = NULL;
size_t size = 0;
@@ -379,16 +382,20 @@ int main(int argc, char **argv){
const size_t READ_INC = 1024;
do{
size_t newsize = size+READ_INC;
+ uint8_t *oldbuf = buf;
buf = realloc(buf, newsize);
if(!buf){
+ free(oldbuf);
+ fclose(f);
fprintf(stderr, "Error opening gif file from argument (\"%s\"): Cannot allocate %lu bytes.\n", argv[1], newsize);
- return 1;
+ goto error;
}
read += fread(buf+size, 1, newsize, f);
size = newsize;
}while(read == size);
fb = framebuffer_render_gif(buf, read, &gifstate, &delay);
free(buf);
+ fclose(f);
}else{
fb = framebuffer_render_text(argv[1], glyph_table, BLP_SIZE);
}
@@ -398,7 +405,7 @@ int main(int argc, char **argv){
if(!fb){
fprintf(stderr, "Error rendering text.\n");
- return 1;
+ goto error;
}
console_render_buffer(fb);
@@ -414,4 +421,6 @@ int main(int argc, char **argv){
}
}
return 0;
+error:
+ return 1;
}