From acdfc73ccebe3c030b010f76eda534d3f37c4cee Mon Sep 17 00:00:00 2001 From: gitlost Date: Thu, 17 Dec 2020 17:18:43 +0000 Subject: [PATCH] gif: move fopen down to avoid possible FILE leak, props Andre Maute --- backend/gif.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/backend/gif.c b/backend/gif.c index ddfcd045..55077bb8 100644 --- a/backend/gif.c +++ b/backend/gif.c @@ -310,22 +310,6 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf) lzwoutbuf = (char *) _alloca(lzoutbufSize); #endif /* _MSC_VER */ - /* Open output file in binary mode */ - if ((symbol->output_options & BARCODE_STDOUT) != 0) { -#ifdef _MSC_VER - if (-1 == _setmode(_fileno(stdout), _O_BINARY)) { - strcpy(symbol->errtxt, "610: Can't open output file"); - return ZINT_ERROR_FILE_ACCESS; - } -#endif - gif_file = stdout; - } else { - if (!(gif_file = fopen(symbol->outfile, "wb"))) { - strcpy(symbol->errtxt, "611: Can't open output file"); - return ZINT_ERROR_FILE_ACCESS; - } - } - /* * Build a table of the used palette items. * Currently, there are the following 10 colour codes: @@ -481,6 +465,22 @@ INTERNAL int gif_pixel_plot(struct zint_symbol *symbol, unsigned char *pixelbuf) /* palette size 2 ^ bit size */ paletteSize = 1<output_options & BARCODE_STDOUT) != 0) { +#ifdef _MSC_VER + if (-1 == _setmode(_fileno(stdout), _O_BINARY)) { + strcpy(symbol->errtxt, "610: Can't open output file"); + return ZINT_ERROR_FILE_ACCESS; + } +#endif + gif_file = stdout; + } else { + if (!(gif_file = fopen(symbol->outfile, "wb"))) { + strcpy(symbol->errtxt, "611: Can't open output file"); + return ZINT_ERROR_FILE_ACCESS; + } + } + /* GIF signature (6) */ memcpy(outbuf, "GIF87a", 6); if (transparent_index != -1)