commit 73b4cc342a57462fbc05fdc12437fc9fecb6ec3d
parent 5899f54a0467ba5d31f63fc56214a06ba6d36d95
Author: Frederic Cambus <fred@statdns.com>
Date: Tue, 30 Apr 2019 17:24:46 +0200
Perform format validation checks before allocating memory, avoids leaking memory in case we error out.
Diffstat:
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c
@@ -93,6 +93,11 @@ ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options)
uint32_t numchars = (xbin_flags & 0x10 ? 512 : 256);
size_t fontsz = xbin_fontsize * numchars;
+ if (offset + fontsz > ctx->length) {
+ ctx->error = ANSILOVE_FORMAT_ERROR;
+ return -1;
+ }
+
/* allocate memory to contain the XBin font */
font_data_xbin = (uint8_t *)malloc(fontsz);
if (font_data_xbin == NULL) {
@@ -100,11 +105,6 @@ ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options)
return -1;
}
- if (offset + fontsz > ctx->length) {
- ctx->error = ANSILOVE_FORMAT_ERROR;
- return -1;
- }
-
memcpy(font_data_xbin, ctx->buffer+offset, fontsz);
font_data = font_data_xbin;