CodeQL File{MayNot,Never}BeClosed.ql file-handle resource leaks

Guided by CodeQL static code analyser.

FileMayNotBeClosed.ql
FileMayNeverBeClosed.ql

The trick with "if(fp != stdout)" is problematic (to analyser) as
technically 'stdout' can be a global pointer that COULD be modified any
time, so it might have changed between the fopen() and fclose() calls so
the close MAY NEVER occurs (which is problem the analyzer can see).

So local state is maintained as a bool which will also clarify to the
compiler see the intention without concern for external stdout
modification.

Some items appear to be out and out leaks when certain commands are use.
This commit is contained in:
Darryl L. Miles
2025-02-22 21:02:49 -05:00
committed by Tim Edwards
parent 7960020f7c
commit e88dcba1c5
10 changed files with 104 additions and 55 deletions
+18 -2
View File
@@ -576,7 +576,7 @@ PlotPNM(fileName, scx, layers, xMask, width)
* plot, in pixels.
*/
{
FILE *fp;
FILE *fp = NULL;
Rect bbox;
int bb_ysize, bb_xsize;
int i, x, y, tile_ydelta;
@@ -920,6 +920,7 @@ PlotPNM(fileName, scx, layers, xMask, width)
}
fflush(rtl_args.outfile);
fclose(rtl_args.outfile);
rtl_args.outfile = NULL;
freeMagic(rtl_args.outbytes);
/* Run spooler */
@@ -932,7 +933,13 @@ PlotPNM(fileName, scx, layers, xMask, width)
}
else
#endif
fclose (fp);
{
if(fp)
{
fclose (fp);
fp = NULL;
}
}
done:
PlotPNMdownsample = save_ds;
@@ -941,6 +948,15 @@ done:
freeMagic(strip);
freeMagic(lkstep);
lkstep = NULL;
#ifdef VERSATEC
if(rtl_args.outfile) /* theoretical fp leak */
{
fclose(rtl_args.outfile);
rtl_args.outfile = NULL;
}
#endif
if(fp)
fclose(fp);
return;
}
+11 -7
View File
@@ -1135,7 +1135,6 @@ PlotPS(fileName, scx, layers, xMask)
{
int xsize, ysize;
float yscale;
FILE *infile;
int i, j;
int twidth, theight;
char *fontptr, *fptr2, *fptr3;
@@ -1207,12 +1206,17 @@ PlotPS(fileName, scx, layers, xMask)
/* Insert the prolog here */
infile = PaOpen("magicps", "r", ".pro", ".", SysLibPath, NULL);
if (infile != NULL)
while(fgets(line_in, 99, infile) != NULL)
fputs(line_in, file);
else
fprintf(file, "\npostscript_prolog_is_missing\n\n");
{
FILE *infile = PaOpen("magicps", "r", ".pro", ".", SysLibPath, NULL);
if (infile != NULL)
{
while(fgets(line_in, 99, infile) != NULL)
fputs(line_in, file);
fclose(infile);
}
else
fprintf(file, "\npostscript_prolog_is_missing\n\n");
}
/* Insert the font definitions here. */
+7 -3
View File
@@ -1353,7 +1353,7 @@ PlotVersatec(scx, layers, xMask, user_scale)
{
TxError("Warning: No color versatec styles are defined"
" in the technology file!\nPlotting aborted.\n");
return;
goto error_close_only;
}
break;
default:
@@ -1372,7 +1372,7 @@ PlotVersatec(scx, layers, xMask, user_scale)
TxError("Warning: No monochrome versatec styles are"
" defined in the technology file!\nPlotting"
" aborted.\n");
return;
goto error_close_only;
}
for ( ; curStyle != NULL; curStyle = curStyle->vs_next)
@@ -1475,10 +1475,14 @@ PlotVersatec(scx, layers, xMask, user_scale)
}
return;
error:
error:
TxError("\nVersatec plot aborted.\n");
fclose(file);
unlink(fileName);
return;
error_close_only:
fclose(file);
}
#endif /* VERSATEC */