commit 2129dc158b322d9602eea735f16005433a3bb0cb
parent 992ef7ec0d8dc889bf4f1196d5b920e7ed90cb33
Author: Frederic Cambus <fred@statdns.com>
Date: Tue, 17 Jul 2018 13:54:25 +0200
Do not leak file descriptors on error paths
Diffstat:
1 file changed, 3 insertions(+), 0 deletions(-)
diff --git a/src/loadfile.c b/src/loadfile.c
@@ -23,12 +23,14 @@ ansilove_loadfile(struct ansilove_ctx *ctx, char *input) {
// load input file
if ((fd = open(input, O_RDONLY)) == -1) {
// perror("File error");
+ close(fd);
return -1;
}
// get the file size (bytes)
if (fstat(fd, &st) == -1) {
// perror("Can't stat file");
+ close(fd);
return -1;
}
@@ -38,6 +40,7 @@ ansilove_loadfile(struct ansilove_ctx *ctx, char *input) {
ctx->buffer = mmap(NULL, ctx->length, PROT_READ, MAP_PRIVATE, fd, 0);
if (ctx->buffer == MAP_FAILED) {
// perror("Memory error");
+ close(fd);
return -1;
}