Skip to content

Commit

Permalink
Fix memory leas if decompression failed or if skipping compression.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjko committed Dec 23, 2022
1 parent c26744c commit 14d3d6f
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions jpegoptim.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
int j;
int oldquality, searchcount, searchdone;
double ratio;
int res = -1;

jpeg_log_fh = log_fh;

Expand All @@ -583,7 +584,8 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
if (filename) {
if ((infile = fopen(filename, "rb")) == NULL) {
warn("cannot open file: %s", filename);
return 1;
res = 1;
goto exit_point;
}
} else {
infile = stdin;
Expand All @@ -595,16 +597,13 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
abort_decompress:
jpeg_abort_decompress(&dinfo);
fclose(infile);
if (inbuffer)
free(inbuffer);
if (outbuffer)
free(outbuffer);
if (buf)
FREE_LINE_BUF(buf,dinfo.output_height);
if (!quiet_mode || csv)
fprintf(log_fh,csv ? ",,,,,error\n" : " [ERROR]\n");
jderr.jump_set=0;
return 1;
res = 1;
goto exit_point;
} else {
jderr.jump_set=1;
}
Expand Down Expand Up @@ -753,16 +752,11 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
fclose(infile);
if (!quiet_mode)
fprintf(log_fh," [Compress ERROR: %s]\n",last_error);
if (inbuffer)
free(inbuffer);
if (outbuffer)
free(outbuffer);
if (buf)
FREE_LINE_BUF(buf,dinfo.output_height);
jcerr.jump_set=0;
jpeg_destroy_compress(&cinfo);
jpeg_destroy_decompress(&dinfo);
return 2;
res = 2;
goto exit_point;
} else {
jcerr.jump_set=1;
}
Expand Down Expand Up @@ -945,8 +939,10 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
}
if (!quiet_mode || csv)
fprintf(log_fh,csv ? "optimized\n" : "optimized.\n");
if (noaction)
return 0;
if (noaction) {
res = 0;
goto exit_point;
}

if (stdout_mode) {
outfname=NULL;
Expand Down Expand Up @@ -1065,15 +1061,17 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
}
}

res = 0;

exit_point:
if (inbuffer)
free(inbuffer);
if (outbuffer)
free(outbuffer);
jpeg_destroy_compress(&cinfo);
jpeg_destroy_decompress(&dinfo);

return 0;
return res;
}


Expand Down

0 comments on commit 14d3d6f

Please sign in to comment.