From 920c6d16b17ff9977369bf7dcca7e55dd886a6db Mon Sep 17 00:00:00 2001 From: Christophe CURIS Date: Thu, 13 May 2021 18:47:38 +0200 Subject: [PATCH] wraster: Remove duplicate code Duplicating things makes maintenance error-prone, which is not a good idea. In case the abort procedure would need an update, it would be easy then to forget some place, leading to leaks, if not worse. Beside, goto is not as bad as academics would like people to believe, when it is used correctly (and this case is one of them). The name for the label was given an explicit meaning to make code easy to understand. Signed-off-by: Christophe CURIS --- wrlib/load_jpeg.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/wrlib/load_jpeg.c b/wrlib/load_jpeg.c index d3938e5c..1ba0834a 100644 --- a/wrlib/load_jpeg.c +++ b/wrlib/load_jpeg.c @@ -130,18 +130,15 @@ RImage *RLoadJPEG(const char *file_name) jpeg_stdio_src(&cinfo, file); jpeg_read_header(&cinfo, TRUE); if (cinfo.image_width < 1 || cinfo.image_height < 1) { + buffer[0] = NULL; /* Initialize pointer to avoid spurious free in cleanup code */ RErrorCode = RERR_BADIMAGEFILE; - jpeg_destroy_decompress(&cinfo); - fclose(file); - return NULL; + goto abort_and_release_resources; } buffer[0] = (JSAMPROW) malloc(cinfo.image_width * cinfo.num_components); if (!buffer[0]) { RErrorCode = RERR_NOMEMORY; - jpeg_destroy_decompress(&cinfo); - fclose(file); - return NULL; + goto abort_and_release_resources; } if (cinfo.jpeg_color_space == JCS_GRAYSCALE) @@ -156,12 +153,7 @@ RImage *RLoadJPEG(const char *file_name) image = RCreateImage(cinfo.image_width, cinfo.image_height, False); if (!image) { RErrorCode = RERR_NOMEMORY; - jpeg_destroy_decompress(&cinfo); - fclose(file); - if (buffer[0]) - free(buffer[0]); - - return NULL; + goto abort_and_release_resources; } jpeg_start_decompress(&cinfo); @@ -186,6 +178,8 @@ RImage *RLoadJPEG(const char *file_name) } jpeg_finish_decompress(&cinfo); + + abort_and_release_resources: jpeg_destroy_decompress(&cinfo); fclose(file); if (buffer[0])