The gzdopen() API used, takes ownership of the fd. To make that more
explicit we clear it (-1) as applicable.
Yet again, kmod has explicit API to return the fd to the user - which
currently is used solely when uncompressed, so we're safe.
Regardless - simply duplicate the fd locally and use that with zlib.
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
int err = 0;
off_t did = 0, total = 0;
_cleanup_free_ unsigned char *p = NULL;
+ int gzfd;
errno = 0;
- file->gzf = gzdopen(file->fd, "rb");
- if (file->gzf == NULL)
+ gzfd = fcntl(file->fd, F_DUPFD_CLOEXEC, 3);
+ if (gzfd < 0)
+ return -errno;
+
+ file->gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
+ if (file->gzf == NULL) {
+ close(gzfd);
return -errno;
- file->fd = -1; /* now owned by gzf due gzdopen() */
+ }
for (;;) {
int r;
return 0;
error:
- gzclose(file->gzf);
+ gzclose(file->gzf); /* closes the gzfd */
return err;
}
if (file->gzf == NULL)
return;
free(file->memory);
- gzclose(file->gzf); /* closes file->fd */
+ gzclose(file->gzf);
}
static const char magic_zlib[] = {0x1f, 0x8b};
if (file->memory)
file->ops->unload(file);
- if (file->fd >= 0)
- close(file->fd);
+ close(file->fd);
free(file);
}