Fix resource leaks & unchecked resource allocations
authormayeut <mayeut@users.noreply.github.com>
Thu, 24 Sep 2015 22:08:34 +0000 (00:08 +0200)
committermayeut <mayeut@users.noreply.github.com>
Thu, 24 Sep 2015 22:08:34 +0000 (00:08 +0200)
src/bin/jp2/convertbmp.c
src/bin/jp2/convertpng.c
src/bin/jp2/opj_compress.c
src/bin/jp2/opj_decompress.c
src/bin/jp2/opj_dump.c

index 78ca4af50a58c4f4bb6280dfce1ab132cf94e709..910574b8aabe89ba3bbd5691a2b3a00566d1be34 100644 (file)
@@ -903,6 +903,10 @@ int imagetobmp(opj_image_t * image, const char *outfile) {
         <<-- <<-- <<-- <<-- */
 
         fdest = fopen(outfile, "wb");
+        if (!fdest) {
+            fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
+            return 1;
+        }
         w = (int)image->comps[0].w;
         h = (int)image->comps[0].h;
 
index 5a72d9fe392f6a8aba95d276926159c51b9e48f0..0999edbf61841bf60eb1e371a098315da054382a 100644 (file)
@@ -486,7 +486,7 @@ fin:
        }
        fclose(writer);
        
-       if(fails) remove(write_idf);
+       if(fails) (void)remove(write_idf); /* ignore return value */
        
        return fails;
 }/* imagetopng() */
index d3dc62ffbbb1952c1dd58f12e0533fb36aece2f4..e5f985323cf76d5497eb8f7fff7b0393615d0189 100644 (file)
@@ -456,7 +456,7 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_c
     if (parameters->decod_format == -1)
         return 1;
     sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
-    strncpy(parameters->infile, infilename, sizeof(infilename));
+    strncpy(parameters->infile, infilename, sizeof(infilename) - 1U);
 
     /*Set output file*/
     strcpy(temp_ofname,get_file_name(image_filename));
@@ -466,7 +466,7 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_c
     }
     if(img_fol->set_out_format==1){
         sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
-        strncpy(parameters->outfile, outfilename, sizeof(outfilename));
+        strncpy(parameters->outfile, outfilename, sizeof(outfilename) - 1U);
     }
     return 0;
 }
@@ -612,6 +612,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
                 substr2++; /* skip '@' character */
             }
             substr1 = (char*) malloc((len+1)*sizeof(char));
+            if (substr1 == NULL) {
+                return 1;
+            }
             memcpy(substr1,opj_optarg,len);
             substr1[len] = '\0';
             if (sscanf(substr1, "%d,%d,%d,%d,%c", &width, &height, &ncomp, &bitdepth, &signo) == 5) {
@@ -663,7 +666,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param
                     }
                 }
             }
-            if (substr1) free(substr1);
+            free(substr1);
             if (wrong) {
                 fprintf(stderr,"\nError: invalid raw image parameters\n");
                 fprintf(stderr,"Please use the Format option -F:\n");
index da566ac21ee614c13cd25626294425b1e83cafb5..bd755c82d5d54c3577bc8be5612e98c087ce7b59 100644 (file)
@@ -1550,7 +1550,7 @@ int main(int argc, char **argv)
                /* destroy the codestream index */
                opj_destroy_cstr_index(&cstr_index);
 
-               if(failed) remove(parameters.outfile);
+               if(failed) (void)remove(parameters.outfile); /* ignore return value */
        }
        destroy_parameters(&parameters);
        if (numDecompressedImages) {
index a85cfe9069bc997a3382751837cc00ebe64afea6..4005f1c25502a47dfe9ace5dcb29d22e2d306098 100644 (file)
@@ -134,6 +134,7 @@ static int get_num_images(char *imgdirpath){
                        continue;
                num_images++;
        }
+       closedir(dir);
        return num_images;
 }
 
@@ -160,6 +161,7 @@ static int load_images(dircnt_t *dirptr, char *imgdirpath){
                strcpy(dirptr->filename[i],content->d_name);
                i++;
        }
+       closedir(dir);
        return 0;       
 }