Fix bad call to fclose with NULL pointer

This commit is contained in:
mayeut 2016-04-30 01:50:33 +02:00
parent be42e72d22
commit f40a2ff2ad
1 changed files with 38 additions and 39 deletions

View File

@ -1762,7 +1762,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
const char *tmp = outfile; const char *tmp = outfile;
char *destname; char *destname;
alpha = NULL; alpha = NULL;
if((prec = (int)image->comps[0].prec) > 16) if((prec = (int)image->comps[0].prec) > 16)
{ {
@ -1842,7 +1842,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
if(two) if(two)
{ {
v = *red + adjustR; ++red; v = *red + adjustR; ++red;
if(v > 65535) v = 65535; else if(v < 0) v = 0; if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */ /* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@ -1850,13 +1850,13 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
if(triple) if(triple)
{ {
v = *green + adjustG; ++green; v = *green + adjustG; ++green;
if(v > 65535) v = 65535; else if(v < 0) v = 0; if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */ /* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
v = *blue + adjustB; ++blue; v = *blue + adjustB; ++blue;
if(v > 65535) v = 65535; else if(v < 0) v = 0; if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */ /* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@ -1866,7 +1866,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
if(has_alpha) if(has_alpha)
{ {
v = *alpha + adjustA; ++alpha; v = *alpha + adjustA; ++alpha;
if(v > 65535) v = 65535; else if(v < 0) v = 0; if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */ /* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@ -1876,28 +1876,28 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
} /* if(two) */ } /* if(two) */
/* prec <= 8: */ /* prec <= 8: */
v = *red++; v = *red++;
if(v > 255) v = 255; else if(v < 0) v = 0; if(v > 255) v = 255; else if(v < 0) v = 0;
fprintf(fdest, "%c", (unsigned char)v); fprintf(fdest, "%c", (unsigned char)v);
if(triple) if(triple)
{ {
v = *green++; v = *green++;
if(v > 255) v = 255; else if(v < 0) v = 0; if(v > 255) v = 255; else if(v < 0) v = 0;
fprintf(fdest, "%c", (unsigned char)v); fprintf(fdest, "%c", (unsigned char)v);
v = *blue++; v = *blue++;
if(v > 255) v = 255; else if(v < 0) v = 0; if(v > 255) v = 255; else if(v < 0) v = 0;
fprintf(fdest, "%c", (unsigned char)v); fprintf(fdest, "%c", (unsigned char)v);
} }
if(has_alpha) if(has_alpha)
{ {
v = *alpha++; v = *alpha++;
if(v > 255) v = 255; else if(v < 0) v = 0; if(v > 255) v = 255; else if(v < 0) v = 0;
fprintf(fdest, "%c", (unsigned char)v); fprintf(fdest, "%c", (unsigned char)v);
} }
} /* for(i */ } /* for(i */
fclose(fdest); return 0; fclose(fdest); return 0;
@ -1911,22 +1911,21 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
fprintf(stderr," is written to the file\n"); fprintf(stderr," is written to the file\n");
} }
destname = (char*)malloc(strlen(outfile) + 8); destname = (char*)malloc(strlen(outfile) + 8);
if(destname == NULL){ if(destname == NULL){
fprintf(stderr, "imagetopnm: memory out\n"); fprintf(stderr, "imagetopnm: memory out\n");
fclose(fdest); return 1;
return 1; }
}
for (compno = 0; compno < ncomp; compno++) for (compno = 0; compno < ncomp; compno++)
{ {
if (ncomp > 1) if (ncomp > 1)
{ {
/*sprintf(destname, "%d.%s", compno, outfile);*/ /*sprintf(destname, "%d.%s", compno, outfile);*/
const size_t olen = strlen(outfile); const size_t olen = strlen(outfile);
const size_t dotpos = olen - 4; const size_t dotpos = olen - 4;
strncpy(destname, outfile, dotpos); strncpy(destname, outfile, dotpos);
sprintf(destname+dotpos, "_%u.pgm", compno); sprintf(destname+dotpos, "_%u.pgm", compno);
} }
else else
sprintf(destname, "%s", outfile); sprintf(destname, "%s", outfile);
@ -1953,7 +1952,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
for (i = 0; i < wr * hr; i++) for (i = 0; i < wr * hr; i++)
{ {
v = *red + adjustR; ++red; v = *red + adjustR; ++red;
if(v > 65535) v = 65535; else if(v < 0) v = 0; if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */ /* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@ -1961,7 +1960,7 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
if(has_alpha) if(has_alpha)
{ {
v = *alpha++; v = *alpha++;
if(v > 65535) v = 65535; else if(v < 0) v = 0; if(v > 65535) v = 65535; else if(v < 0) v = 0;
/* netpbm: */ /* netpbm: */
fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v); fprintf(fdest, "%c%c",(unsigned char)(v>>8), (unsigned char)v);
@ -1972,10 +1971,10 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0;
{ {
for(i = 0; i < wr * hr; ++i) for(i = 0; i < wr * hr; ++i)
{ {
v = *red + adjustR; ++red; v = *red + adjustR; ++red;
if(v > 255) v = 255; else if(v < 0) v = 0; if(v > 255) v = 255; else if(v < 0) v = 0;
fprintf(fdest, "%c", (unsigned char)v); fprintf(fdest, "%c", (unsigned char)v);
} }
} }
fclose(fdest); fclose(fdest);