From 3257a5c0af634dc44fa333864108faa44c007f47 Mon Sep 17 00:00:00 2001 From: Julien Nabet Date: Wed, 28 Jun 2017 21:46:29 +0200 Subject: [PATCH] cppcheck: fix reports [backend/bmp.c:133]: (error) Memory leak: bitmap [backend/bmp.c:126]: (error) Memory leak: bitmap [backend/composite.c:464]: (error) Array index -1 is out of bounds. [backend/composite.c:507]: (error) Array index -1 is out of bounds. [backend/dmatrix.c:962]: (style) Redundant condition: i!=0. 'i==0 || (i!=0 && binary[i-1]!='b')' is equivalent to 'i==0 || binary[i-1]!='b'' [backend/raster.c:122]: (error) Memory leak: rotated_pixbuf [backend/raster.c:506]: (error) Memory leak: pixelbuf [frontend/main.c:273]: (error) Uninitialized variable: format_string [frontend_qt/exportwindow.cpp:103]: (style) Array index 'j' is used before limits check. --- backend/bmp.c | 6 +++++- backend/composite.c | 1 + backend/dmatrix.c | 2 +- backend/raster.c | 6 ++++++ frontend/main.c | 1 + frontend_qt/exportwindow.cpp | 2 +- 6 files changed, 15 insertions(+), 3 deletions(-) diff --git a/backend/bmp.c b/backend/bmp.c index cbdf4005..9689188c 100644 --- a/backend/bmp.c +++ b/backend/bmp.c @@ -123,12 +123,16 @@ int bmp_pixel_plot(struct zint_symbol *symbol, char *pixelbuf) { #ifdef _MSC_VER if (-1 == _setmode(_fileno(stdout), _O_BINARY)) { strcpy(symbol->errtxt, "Can't open output file"); + free(bitmap_file_start); + free(bitmap); return ZINT_ERROR_FILE_ACCESS; } #endif bmp_file = stdout; } else { if (!(bmp_file = fopen(symbol->outfile, "wb"))) { + free(bitmap_file_start); + free(bitmap); strcpy(symbol->errtxt, "Can't open output file (F00)"); return ZINT_ERROR_FILE_ACCESS; } @@ -140,4 +144,4 @@ int bmp_pixel_plot(struct zint_symbol *symbol, char *pixelbuf) { free(bitmap_file_start); free(bitmap); return 0; -} \ No newline at end of file +} diff --git a/backend/composite.c b/backend/composite.c index 7ccd9660..817389fa 100644 --- a/backend/composite.c +++ b/backend/composite.c @@ -461,6 +461,7 @@ static int cc_b(struct zint_symbol *symbol, char source[], int cc_width) { /* Now we have the variant we can load the data - from here on the same as MicroPDF417 code */ variant--; + assert(variant >= 0); symbol->option_2 = MicroVariants[variant]; /* columns */ symbol->rows = MicroVariants[variant + 34]; /* rows */ k = MicroVariants[variant + 68]; /* number of EC CWs */ diff --git a/backend/dmatrix.c b/backend/dmatrix.c index b282e853..4c009668 100644 --- a/backend/dmatrix.c +++ b/backend/dmatrix.c @@ -959,7 +959,7 @@ static int dm200encode(struct zint_symbol *symbol, const unsigned char source[], i = 0; while (i < tp) { if (binary[i] == 'b') { - if ((i == 0) || ((i != 0) && (binary[i - 1] != 'b'))) { + if ((i == 0) || (binary[i - 1] != 'b')) { /* start of binary data */ int binary_count; /* length of b256 data */ diff --git a/backend/raster.c b/backend/raster.c index 88d44e2f..5ef928ce 100644 --- a/backend/raster.c +++ b/backend/raster.c @@ -119,20 +119,24 @@ int save_raster_image_to_file(struct zint_symbol *symbol, int image_height, int if (strlen(symbol->fgcolour) != 6) { strcpy(symbol->errtxt, "Malformed foreground colour target (F51)"); + free(rotated_pixbuf); return ZINT_ERROR_INVALID_OPTION; } if (strlen(symbol->bgcolour) != 6) { strcpy(symbol->errtxt, "Malformed background colour target (F52)"); + free(rotated_pixbuf); return ZINT_ERROR_INVALID_OPTION; } error_number = is_sane(SSET, (unsigned char*) symbol->fgcolour, strlen(symbol->fgcolour)); if (error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Malformed foreground colour target (F53)"); + free(rotated_pixbuf); return ZINT_ERROR_INVALID_OPTION; } error_number = is_sane(SSET, (unsigned char*) symbol->bgcolour, strlen(symbol->fgcolour)); if (error_number == ZINT_ERROR_INVALID_DATA) { strcpy(symbol->errtxt, "Malformed background colour target (F54)"); + free(rotated_pixbuf); return ZINT_ERROR_INVALID_OPTION; } @@ -181,6 +185,7 @@ int save_raster_image_to_file(struct zint_symbol *symbol, int image_height, int #ifndef NO_PNG error_number = png_pixel_plot(symbol, rotated_pixbuf); #else + free(rotated_pixbuf); return ZINT_ERROR_INVALID_OPTION; #endif break; @@ -503,6 +508,7 @@ int plot_raster_maxicode(struct zint_symbol *symbol, int rotate_angle, int data_ if (!(scaled_hexagon = (char *) malloc(hexagon_size * hexagon_size))) { strcpy(symbol->errtxt, "Insufficient memory for pixel buffer (F56)"); free(scaled_hexagon); + free(pixelbuf); return ZINT_ERROR_ENCODING_PROBLEM; } else { for (i = 0; i < (hexagon_size * hexagon_size); i++) { diff --git a/frontend/main.c b/frontend/main.c index 3a17abc3..6d20f2e7 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -266,6 +266,7 @@ int batch_process(struct zint_symbol *symbol, char *filename, int mirror_mode, c char adjusted[2]; memset(buffer, 0, sizeof (unsigned char) * 7100); + memset(format_string, 0, sizeof (unsigned char) * 127); if (symbol->outfile[0] == '\0') { strcpy(format_string, "~~~~~."); strcat(format_string, filetype); diff --git a/frontend_qt/exportwindow.cpp b/frontend_qt/exportwindow.cpp index 3357e549..4cfbc2ac 100644 --- a/frontend_qt/exportwindow.cpp +++ b/frontend_qt/exportwindow.cpp @@ -100,7 +100,7 @@ void ExportWindow::process() for(i = 0; i < lines; i++) { datalen = 0; - for(j = inputpos; ((output_data[j] != '\n') && (j < output_data.length())); j++) { + for(j = inputpos; ((j < output_data.length()) && (output_data[j] != '\n') ); j++) { datalen++; } dataString = output_data.mid(inputpos, datalen);