From d48824195009b2497b4233c9d3f2a8f9c1e6a2d7 Mon Sep 17 00:00:00 2001 From: Ulrich Sibiller Date: Thu, 20 Jun 2019 19:51:00 +0200 Subject: compext/Png.c: late image_index allocation Move allocation of image_index close before first_use. This way we do not need to free it if previous step fail. And we cannot forget that free() call. While at it replace malloc+memset by calloc. --- nx-X11/programs/Xserver/hw/nxagent/compext/Png.c | 35 ++++++++++++------------ 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/nx-X11/programs/Xserver/hw/nxagent/compext/Png.c b/nx-X11/programs/Xserver/hw/nxagent/compext/Png.c index 0a258c5a3..e80f3879d 100644 --- a/nx-X11/programs/Xserver/hw/nxagent/compext/Png.c +++ b/nx-X11/programs/Xserver/hw/nxagent/compext/Png.c @@ -253,17 +253,6 @@ char *PngCompressData(XImage *image, int *compressed_size) NXColorTable color_table[NB_COLOR_MAX]; CARD8 *image_index; - image_index = (CARD8 *) malloc((image -> height) * (image -> width) * sizeof(CARD8)); - - /* - * TODO: Be sure the padded bytes are cleaned. - * It would be better to set to zero the bytes - * that are not aligned to the word boundary - * at the end of the procedure. - */ - - memset(image_index, 0, (image -> height) * (image -> width) * sizeof(CARD8)); - *compressed_size = 0; pngDataLen = 0; @@ -283,7 +272,6 @@ char *PngCompressData(XImage *image, int *compressed_size) bitsPerPixel); #endif - free(image_index); return NULL; } @@ -316,7 +304,6 @@ char *PngCompressData(XImage *image, int *compressed_size) fprintf(stderr, "******PngCompressData: PANIC! Failed creating the png_create_write_struct.\n"); #endif - free(image_index); return NULL; } @@ -329,7 +316,6 @@ char *PngCompressData(XImage *image, int *compressed_size) #endif png_destroy_write_struct(&png_ptr, NULL); - free(image_index); return NULL; } @@ -341,7 +327,6 @@ char *PngCompressData(XImage *image, int *compressed_size) #endif png_destroy_write_struct(&png_ptr, &info_ptr); - free(image_index); return NULL; } @@ -364,8 +349,6 @@ char *PngCompressData(XImage *image, int *compressed_size) PNG_DEST_SIZE(w, h)); #endif - free(image_index); - return NULL; } @@ -383,6 +366,24 @@ char *PngCompressData(XImage *image, int *compressed_size) return NULL; } + image_index = (CARD8 *) calloc(1, (image -> height) * (image -> width) * sizeof(CARD8)); + if (image_index == NULL) + { + #ifdef PANIC + fprintf(stderr, "******PngCompressData: PANIC! Could not alloc image_index.\n"); + #endif + + free(pngCompBuf); + return NULL; + } + + /* + * TODO: Be sure the padded bytes are cleaned. + * It would be better to set to zero the bytes + * that are not aligned to the word boundary + * at the end of the procedure. + */ + png_set_compression_level(png_ptr, PNG_Z_LEVEL); if (bitsPerPixel == 16) -- cgit v1.2.3