path: root/buildtools
diff options
authorBruce Richardson <>2018-02-02 12:00:58 +0000
committerThomas Monjalon <>2018-02-06 01:15:14 +0100
commitac7f4af813c5cc63b070029b08fe82f1e67e1998 (patch)
tree4e073ccc16bb686cf439c351ca2f229270175356 /buildtools
parent028e4b1dbc4acb54db9188a81d7644bccc542c74 (diff)
pmdinfogen: fix resource leak of file object
Coverity flags an issue where the resources used by the FILE object for the temporary input file are leaked. This is a very minor issue, but is easily fixed, while also avoiding later problems where we try to close an invalid file descriptor in the failure case. The fix is to use "dup()" to get a new file descriptor number rather than using the value directly from fileno. This allows us to close the file opened with tmpfile() within in scope block, while allowing the duplicate to pass to the outer block and be closed when the function terminates. As a side-effect I/O in the function is therefore changed from using stdio fread/fwrite to read/write system calls. Coverity issue: 260399 Fixes: 0d68533617e3 ("pmdinfogen: allow using stdin and stdout") Signed-off-by: Bruce Richardson <> Acked-by: Neil Horman <>
Diffstat (limited to 'buildtools')
1 files changed, 10 insertions, 6 deletions
diff --git a/buildtools/pmdinfogen/pmdinfogen.c b/buildtools/pmdinfogen/pmdinfogen.c
index 45b2673..0f35ca4 100644
--- a/buildtools/pmdinfogen/pmdinfogen.c
+++ b/buildtools/pmdinfogen/pmdinfogen.c
@@ -50,20 +50,24 @@ static void *grab_file(const char *filename, unsigned long *size)
/* from stdin, use a temporary file to mmap */
FILE *infile;
char buffer[1024];
- size_t n;
+ int n;
infile = tmpfile();
if (infile == NULL) {
return NULL;
- while (!feof(stdin)) {
- n = fread(buffer, 1, sizeof(buffer), stdin);
- if (fwrite(buffer, 1, n, infile) != n)
+ fd = dup(fileno(infile));
+ fclose(infile);
+ if (fd < 0)
+ return NULL;
+ n = read(STDIN_FILENO, buffer, sizeof(buffer));
+ while (n > 0) {
+ if (write(fd, buffer, n) != n)
goto failed;
+ n = read(STDIN_FILENO, buffer, sizeof(buffer));
- fflush(infile);
- fd = fileno(infile);
if (fstat(fd, &st))