commit: 1a1008c3e415b99f191a977c2a01d1ac9eaf1295
parent 7eb90fd3a7a48876114c76f2345dd33b2442cfe9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 24 Jul 2025 20:07:18 +0200
cmd/head: fix fopen leak
Diffstat:
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/cmd/head.c b/cmd/head.c
@@ -8,6 +8,7 @@
#include "../libutils/getopt_nolong.h"
#include "../libutils/truncation.h" // apply_size_suffix
+#include <assert.h>
#include <ctype.h> // isdigit
#include <errno.h>
#include <fcntl.h> // open
@@ -90,6 +91,7 @@ copy_lines(const char *filename)
stderr, "%s: error: Failed opening file '%s': %s\n", argv0, filename, strerror(errno));
return 1;
}
+ assert(in != stdin);
}
// Relative to EOF, GNU extension
@@ -107,7 +109,8 @@ copy_lines(const char *filename)
if(!win)
{
fprintf(stderr, "%s: error: Failed allocating lines array: %s\n", argv0, strerror(errno));
- return 1;
+ err = 1;
+ goto copy_lines_end;
}
while(true)
@@ -150,7 +153,8 @@ copy_lines(const char *filename)
{
fprintf(
stderr, "%s: error: Failed (re)allocating line buffer: %s\n", argv0, strerror(errno));
- return 1;
+ err = 1;
+ break;
}
win[wini].cap = nread;
}
@@ -197,6 +201,7 @@ copy_lines(const char *filename)
}
}
+copy_lines_end:
if(in != stdin)
if(fclose(in) != 0)
{