Skip to content

Commit

Permalink
feat(cli): improve error messages
Browse files Browse the repository at this point in the history
Use a proper null check.
  • Loading branch information
dbohdan committed Oct 3, 2024
1 parent bf9c062 commit 0b04316
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
#define HICOLOR_CLI_CMD_VERSION "version"
#define HICOLOR_CLI_CMD_HELP "help"

const char* libpng_error_msg;
const char* png_error_msg = "no error recorded";

void libpng_error_handler(
png_structp png_ptr,
png_const_charp error_msg
)
{
libpng_error_msg = error_msg;
png_error_msg = error_msg;
longjmp(png_jmpbuf(png_ptr), 1);
}

Expand All @@ -46,24 +46,27 @@ bool load_png(
{
FILE* fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, HICOLOR_CLI_ERROR "can't open file %s for reading\n", filename);
png_error_msg = "failed to open for reading";
return false;
}

png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, libpng_error_handler, NULL);
if (!png) {
if (png == NULL) {
png_error_msg = "`png_create_read_struct` returned null";
fclose(fp);
return false;
}

png_infop info = png_create_info_struct(png);
if (!info) {
if (info == NULL) {
png_error_msg = "`png_create_info_struct` returned null";
png_destroy_read_struct(&png, NULL, NULL);
fclose(fp);
return false;
}

if (setjmp(png_jmpbuf(png))) {
/* Do not overwrite `png_error_msg` set by the handler here. */
png_destroy_read_struct(&png, &info, NULL);
fclose(fp);
return false;
Expand Down Expand Up @@ -110,13 +113,15 @@ bool load_png(
*alpha = malloc(sizeof(uint8_t) * *width * *height);

if (*rgb_img == NULL || *alpha == NULL) {
png_error_msg = "failed to allocate memory for `rgb_img` or `alpha`";
png_destroy_read_struct(&png, &info, NULL);
fclose(fp);
return false;
}

png_bytep row = malloc(png_get_rowbytes(png, info));
if (row == NULL) {
png_error_msg = "failed to allocate memory for `row`";
free(*rgb_img);
free(*alpha);
png_destroy_read_struct(&png, &info, NULL);
Expand Down Expand Up @@ -153,24 +158,27 @@ bool save_png(
{
FILE* fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, HICOLOR_CLI_ERROR "can't open file %s for writing\n", filename);
png_error_msg = "failed to open for writing";
return false;
}

png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, libpng_error_handler, NULL);
if (!png) {
if (png == NULL) {
png_error_msg = "`png_create_write_struct` returned null";
fclose(fp);
return false;
}

png_infop info = png_create_info_struct(png);
if (!info) {
if (info == NULL) {
png_error_msg = "`png_create_info_struct` returned null";
png_destroy_write_struct(&png, NULL);
fclose(fp);
return false;
}

if (setjmp(png_jmpbuf(png))) {
/* Do not overwrite `png_error_msg` set by the handler here. */
png_destroy_write_struct(&png, &info);
fclose(fp);
return false;
Expand All @@ -194,6 +202,7 @@ bool save_png(

png_bytep row = malloc(png_get_rowbytes(png, info));
if (row == NULL) {
png_error_msg = "failed to allocate memory for `row`";
png_destroy_write_struct(&png, &info);
fclose(fp);
return false;
Expand Down Expand Up @@ -276,7 +285,7 @@ bool png_to_hicolor(
stderr,
HICOLOR_CLI_ERROR "can't load PNG file \"%s\": %s\n",
src,
libpng_error_msg
png_error_msg
);
return false;
}
Expand Down Expand Up @@ -348,7 +357,7 @@ bool png_quantize(
stderr,
HICOLOR_CLI_ERROR "can't load PNG file \"%s\": %s\n",
src,
libpng_error_msg
png_error_msg
);
return false;
}
Expand All @@ -366,7 +375,11 @@ bool png_quantize(
}

if (!save_png(dest, width, height, rgb_img, alpha)) {
fprintf(stderr, HICOLOR_CLI_ERROR "can't save PNG\n");
fprintf(
stderr,
HICOLOR_CLI_ERROR "can't save PNG: %s\n",
png_error_msg
);
goto clean_up_images;
}

Expand Down Expand Up @@ -413,7 +426,11 @@ bool hicolor_to_png(
}

if (!save_png(dest, meta.width, meta.height, rgb_img, NULL)) {
fprintf(stderr, HICOLOR_CLI_ERROR "can't save PNG\n");
fprintf(
stderr,
HICOLOR_CLI_ERROR "can't save PNG: %s\n",
png_error_msg
);
goto clean_up_rgb_img;
}

Expand Down

0 comments on commit 0b04316

Please sign in to comment.