libansilove

Library for converting ANSI, ASCII, and other formats to PNG
Log | Files | Refs | README | LICENSE

commit c23a65aed053dd1f30c7e127d3a7e70df92b350c
parent 5ed4e0af04b14514686c680d3d85ff8cc71d9be1
Author: Frederic Cambus <fred@statdns.com>
Date:   Tue, 29 Sep 2020 16:26:31 +0200

Error gracefully if canvas' width or height is equal to zero.

Diffstat:
Msrc/loaders/ansi.c | 6++++++
Msrc/loaders/artworx.c | 13+++++++++++--
Msrc/loaders/binary.c | 5+++++
Msrc/loaders/icedraw.c | 12+++++++++++-
Msrc/loaders/pcboard.c | 12+++++++++++-
Msrc/loaders/tundra.c | 11+++++++++--
Msrc/loaders/xbin.c | 11++++++++++-
7 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/src/loaders/ansi.c b/src/loaders/ansi.c @@ -481,6 +481,12 @@ ansilove_ansi(struct ansilove_ctx *ctx, struct ansilove_options *options) uint32_t width = columns * options->bits; uint32_t height = rowMax * fontData.height; + if (!width || !height) { + ctx->error = ANSILOVE_FORMAT_ERROR; + free(ansi_buffer); + return -1; + } + /* create that damn thingy */ canvas = options->truecolor ? gdImageCreateTrueColor(width, height) : diff --git a/src/loaders/artworx.c b/src/loaders/artworx.c @@ -42,8 +42,17 @@ ansilove_artworx(struct ansilove_ctx *ctx, struct ansilove_options *options) gdImagePtr canvas; /* create ADF instance */ - canvas = - gdImageCreate(640, (ctx->length - ADF_HEADER_LENGTH) / 2 / 80 * 16); + + uint32_t width, height; + width = 640; + height = (ctx->length - ADF_HEADER_LENGTH) / 2 / 80 * 16; + + if (!width || !height) { + ctx->error = ANSILOVE_FORMAT_ERROR; + return -1; + } + + canvas = gdImageCreate(width, height); if (!canvas) { ctx->error = ANSILOVE_GD_ERROR; diff --git a/src/loaders/binary.c b/src/loaders/binary.c @@ -49,6 +49,11 @@ ansilove_binary(struct ansilove_ctx *ctx, struct ansilove_options *options) width = options->columns * options->bits; height = ctx->length / 2 / options->columns * fontData.height; + if (!width || !height) { + ctx->error = ANSILOVE_FORMAT_ERROR; + return -1; + } + /* allocate buffer image memory */ canvas = gdImageCreate(width, height); diff --git a/src/loaders/icedraw.c b/src/loaders/icedraw.c @@ -93,8 +93,18 @@ ansilove_icedraw(struct ansilove_ctx *ctx, struct ansilove_options *options) loop += 2; } + uint32_t width, height; + width = x2 * 8; + height = i / 2 / 80 * 16; + + if (!width || !height) { + ctx->error = ANSILOVE_FORMAT_ERROR; + free(idf_buffer); + return -1; + } + /* create IDF instance */ - canvas = gdImageCreate(x2 * 8, i / 2 / 80 * 16); + canvas = gdImageCreate(width, height); /* error output */ if (!canvas) { diff --git a/src/loaders/pcboard.c b/src/loaders/pcboard.c @@ -164,8 +164,18 @@ ansilove_pcboard(struct ansilove_ctx *ctx, struct ansilove_options *options) } rowMax++; + uint32_t width, height; + width = columns * options->bits; + height = rowMax * fontData.height; + + if (!width || !height) { + ctx->error = ANSILOVE_FORMAT_ERROR; + free(pcboard_buffer); + return -1; + } + /* allocate buffer image memory */ - canvas = gdImageCreate(columns * options->bits, rowMax*fontData.height); + canvas = gdImageCreate(width, height); if (!canvas) { ctx->error = ANSILOVE_GD_ERROR; diff --git a/src/loaders/tundra.c b/src/loaders/tundra.c @@ -112,10 +112,17 @@ ansilove_tundra(struct ansilove_ctx *ctx, struct ansilove_options *options) loop++; } + uint32_t width, height; + width = columns * options->bits; + height = row * fontData.height; + + if (!width || !height) { + ctx->error = ANSILOVE_FORMAT_ERROR; + return -1; + } /* allocate buffer image memory */ - canvas = gdImageCreateTrueColor(columns * options->bits, - row * fontData.height); + canvas = gdImageCreate(width, height); if (!canvas) { ctx->error = ANSILOVE_GD_ERROR; diff --git a/src/loaders/xbin.c b/src/loaders/xbin.c @@ -62,7 +62,16 @@ ansilove_xbin(struct ansilove_ctx *ctx, struct ansilove_options *options) gdImagePtr canvas; - canvas = gdImageCreate(8 * xbin_width, xbin_fontsize * xbin_height); + uint32_t width, height; + width = 8 * xbin_width; + height = xbin_fontsize * xbin_height; + + if (!width || !height) { + ctx->error = ANSILOVE_FORMAT_ERROR; + return -1; + } + + canvas = gdImageCreate(width, height); if (!canvas) { ctx->error = ANSILOVE_GD_ERROR;