]> granicus.if.org Git - openjpeg/commitdiff
Fix some resource leaks
authormayeut <mayeut@users.noreply.github.com>
Thu, 24 Sep 2015 21:32:03 +0000 (23:32 +0200)
committermayeut <mayeut@users.noreply.github.com>
Thu, 24 Sep 2015 21:32:03 +0000 (23:32 +0200)
src/bin/jp2/convert.c
src/bin/jp2/convertbmp.c
src/bin/jp2/opj_compress.c

index 1ccb34f875e3802ee637c3501fa15f249447f320..1fd64ee7ef93ffe935441258bd6611d92108647c 100644 (file)
@@ -734,12 +734,16 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
         return 0;
     }
 
-    if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
+    if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) {
+        fclose(f);
         return NULL;
+    }
 
     /* We currently only support 24 & 32 bit tga's ... */
-    if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
+    if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
+        fclose(f);
         return NULL;
+    }
 
     /* initialize image components */
     memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
@@ -772,8 +776,11 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
     /* create the image */
     image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
 
-    if (!image)
+    if (!image) {
+        fclose(f);
         return NULL;
+    }
+
 
     /* set image offset and reference grid */
     image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
@@ -801,18 +808,21 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
                 {
                     fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                     opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 if ( !fread(&g, 1, 1, f) )
                 {
                     fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                     opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 if ( !fread(&r, 1, 1, f) )
                 {
                     fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                     opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
 
@@ -831,24 +841,28 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
                 {
                     fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                     opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 if ( !fread(&g, 1, 1, f) )
                 {
                     fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                     opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 if ( !fread(&r, 1, 1, f) )
                 {
                     fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                     opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 if ( !fread(&a, 1, 1, f) )
                 {
                     fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                     opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
 
@@ -863,6 +877,7 @@ opj_image_t* tgatoimage(const char *filename, opj_cparameters_t *parameters) {
             fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
         }
     }
+    fclose(f);
     return image;
 }
 
@@ -889,6 +904,7 @@ int imagetotga(opj_image_t * image, const char *outfile) {
         if ((image->comps[0].dx != image->comps[i+1].dx)
                 ||(image->comps[0].dy != image->comps[i+1].dy)
                 ||(image->comps[0].prec != image->comps[i+1].prec))    {
+            fclose(fdest);
             fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
             return 1;
         }
@@ -1081,6 +1097,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
 
     fseek(f, 0, SEEK_SET);
     if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
+        fclose(f);
         fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
         return NULL;
     }
@@ -1098,6 +1115,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) {
     } else if (endian2=='M' && endian1=='L') {
         bigendian = 0;
     } else {
+        fclose(f);
         fprintf(stderr, "Bad pgx header, please check input file\n");
         return NULL;
     }
@@ -1212,7 +1230,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
   FILE *fdest = NULL;
 
   for (compno = 0; compno < image->numcomps; compno++) 
-    {
+       {
     opj_image_comp_t *comp = &image->comps[compno];
     char bname[256]; /* buffer for name */
     char *name = bname; /* pointer */
@@ -1223,25 +1241,30 @@ int imagetopgx(opj_image_t * image, const char *outfile)
     const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
 
     if( outfile[dotpos] != '.' ) 
-      {
+               {
       /* `pgx` was recognized but there is no dot at expected position */
       fprintf(stderr, "ERROR -> Impossible happen." );
       goto fin;
-      }
+               }
     if( total > 256 ) 
-      {
+               {
       name = (char*)malloc(total+1);
-      }
+                       if (name == NULL) {
+                               goto fin;
+                       }
+               }
     strncpy(name, outfile, dotpos);
     sprintf(name+dotpos, "_%d.pgx", compno);
     fdest = fopen(name, "wb");
     /* dont need name anymore */
-    if( total > 256 ) free(name);
+                       
     if (!fdest) 
-      {
+               {
+                               
       fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
+                       if( total > 256 ) free(name);
       goto fin;
-      }
+               }
 
     w = (int)image->comps[compno].w;
     h = (int)image->comps[compno].h;
@@ -1257,26 +1280,28 @@ int imagetopgx(opj_image_t * image, const char *outfile)
       nbytes = 4;
 
     for (i = 0; i < w * h; i++) 
-      {
+               {
       /* FIXME: clamp func is being called within a loop */
       const int val = clamp(image->comps[compno].data[i],
         (int)comp->prec, (int)comp->sgnd);
 
       for (j = nbytes - 1; j >= 0; j--) 
-        {
+                       {
         int v = (int)(val >> (j * 8));
         unsigned char byte = (unsigned char)v;
         res = fwrite(&byte, 1, 1, fdest);
 
         if( res < 1 ) 
-          {
+                               {
           fprintf(stderr, "failed to write 1 byte for %s\n", name);
+                                       if( total > 256 ) free(name);
           goto fin;
-          }
-        }
-      }
+                               }
+                       }
+               }
+               if( total > 256 ) free(name);
     fclose(fdest); fdest = NULL;
-    }
+       }
   fails = 0;
 fin:
   if(fdest) fclose(fdest);
@@ -1650,6 +1675,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
                   {
                   fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
                   opj_image_destroy(image);
+                  fclose(fp);
                   return NULL;
                   }
                     if(one)
@@ -2003,6 +2029,7 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
     if (!cmptparm) {
         fprintf(stderr, "Failed to allocate image components parameters !!\n");
         fprintf(stderr,"Aborting\n");
+        fclose(f);
         return NULL;
     }
     /* initialize image components */
@@ -2036,6 +2063,8 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
             for (i = 0; i < nloop; i++) {
                 if (!fread(&value, 1, 1, f)) {
                     fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+                    opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
@@ -2052,10 +2081,14 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
                 unsigned char temp2;
                 if (!fread(&temp1, 1, 1, f)) {
                     fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+                    opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 if (!fread(&temp2, 1, 1, f)) {
                     fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+                    opj_image_destroy(image);
+                    fclose(f);
                     return NULL;
                 }
                 if( big_endian )
@@ -2072,6 +2105,8 @@ static opj_image_t* rawtoimage_common(const char *filename, opj_cparameters_t *p
     }
     else {
         fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
+        opj_image_destroy(image);
+        fclose(f);
         return NULL;
     }
 
index 0678d9d39f51dc185cc9de2460342aa8a2c7d0ff..78ca4af50a58c4f4bb6280dfce1ab132cf94e709 100644 (file)
@@ -729,6 +729,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
        image = opj_image_create(numcmpts, &cmptparm[0], (numcmpts == 1U) ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB);
        if(!image) {
                fclose(IN);
+               free(pData);
                return NULL;
        }
        if (numcmpts == 4U) {
index ee473ce3cfd73d0597f14fba9c668fce11d6ecf3..d3dc62ffbbb1952c1dd58f12e0533fb36aece2f4 100644 (file)
@@ -391,6 +391,7 @@ static unsigned int get_num_images(char *imgdirpath){
             continue;
         num_images++;
     }
+    closedir(dir);
     return num_images;
 }
 
@@ -416,6 +417,7 @@ static int load_images(dircnt_t *dirptr, char *imgdirpath){
         strcpy(dirptr->filename[i],content->d_name);
         i++;
     }
+       closedir(dir);
     return 0;
 }
 
@@ -1058,9 +1060,16 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
             lStrLen = (size_t)ftell(lFile);
             fseek(lFile,0,SEEK_SET);
             lMatrix = (char *) malloc(lStrLen + 1);
+            if (lMatrix == NULL) {
+                fclose(lFile);
+                return 1;
+            }
             lStrFread = fread(lMatrix, 1, lStrLen, lFile);
             fclose(lFile);
-            if( lStrLen != lStrFread ) return 1;
+            if( lStrLen != lStrFread ) {
+                free(lMatrix);
+                return 1;
+            }
 
             lMatrix[lStrLen] = 0;
             lCurrentPtr = lMatrix;
@@ -1080,6 +1089,10 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
             lMctComp = lNbComp * lNbComp;
             lTotalComp = lMctComp + lNbComp;
             lSpace = (float *) malloc((size_t)lTotalComp * sizeof(float));
+            if(lSpace == NULL) {
+                free(lMatrix);
+                return 1;
+            }
             lCurrentDoublePtr = lSpace;
             for (i2=0;i2<lMctComp;++i2) {
                 lStrLen = strlen(lCurrentPtr) + 1;