commit ddc0d4d824df10aea8e6f79a6408d9e1c61a6156
parent b5fe5aaec0c9e48f230abb52722ef536787b1cd0
Author: Frederic Cambus <fred@statdns.com>
Date: Tue, 29 Sep 2020 19:25:16 +0200
Refactor error handling in several loaders.
Diffstat:
3 files changed, 20 insertions(+), 26 deletions(-)
diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c
@@ -173,8 +173,7 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options)
if (ptr == NULL) {
ctx->error = ANSILOVE_MEMORY_ERROR;
- free(ansi_buffer);
- return -1;
+ goto error;
}
ansi_buffer = ptr;
@@ -488,8 +487,7 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options)
if (!width || !height) {
ctx->error = ANSILOVE_FORMAT_ERROR;
- free(ansi_buffer);
- return -1;
+ goto error;
}
/* create that damn thingy */
@@ -499,8 +497,7 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options)
if (!canvas) {
ctx->error = ANSILOVE_GD_ERROR;
- free(ansi_buffer);
- return -1;
+ goto error;
}
uint32_t colors[16];
@@ -552,13 +549,14 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options)
}
/* create output image */
- if (output(ctx, options, canvas) != 0) {
- free(ansi_buffer);
- return -1;
- }
+ if (output(ctx, options, canvas) != 0)
+ goto error;
/* free memory */
free(ansi_buffer);
-
return 0;
+
+error:
+ free(ansi_buffer);
+ return -1;
}
diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c
@@ -68,8 +68,7 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options)
ptr = realloc(idf_buffer, i + 2);
if (ptr == NULL) {
ctx->error = ANSILOVE_MEMORY_ERROR;
- free(idf_buffer);
- return -1;
+ goto error;
}
idf_buffer = ptr;
@@ -84,8 +83,7 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options)
ptr = realloc(idf_buffer, i + 2);
if (ptr == NULL) {
ctx->error = ANSILOVE_MEMORY_ERROR;
- free(idf_buffer);
- return -1;
+ goto error;
}
idf_buffer = ptr;
@@ -104,8 +102,7 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options)
if (!width || !height) {
ctx->error = ANSILOVE_FORMAT_ERROR;
- free(idf_buffer);
- return -1;
+ goto error;
}
/* create IDF instance */
@@ -114,8 +111,7 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options)
/* error output */
if (!canvas) {
ctx->error = ANSILOVE_GD_ERROR;
- free(idf_buffer);
- return -1;
+ goto error;
}
/* process IDF palette */
@@ -151,12 +147,13 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options)
}
/* create output file */
- if (output(ctx, options, canvas) != 0) {
- free(idf_buffer);
- return -1;
- }
+ if (output(ctx, options, canvas) != 0)
+ goto error;
free(idf_buffer);
-
return 0;
+
+error:
+ free(idf_buffer);
+ return -1;
}
diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c
@@ -175,8 +175,7 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options)
if (!width || !height) {
ctx->error = ANSILOVE_FORMAT_ERROR;
- free(pcboard_buffer);
- return -1;
+ goto error;
}
/* allocate buffer image memory */