modpost: Stop grab_file() from leaking filedescriptors if fstat() fails

In case the open() call succeeds but the subsequent fstat() call
fails, then we'll return without close()'ing the filedescriptor.

Signed-off-by: Jesper Juhl <jj@chaosbits.net>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Jesper Juhl 2012-05-23 22:28:49 +09:30 committed by Rusty Russell
parent 6101167727
commit eb3d5cc67a
1 changed files with 6 additions and 3 deletions

View File

@ -337,17 +337,20 @@ static void sym_update_crc(const char *name, struct module *mod,
void *grab_file(const char *filename, unsigned long *size)
{
struct stat st;
void *map;
void *map = MAP_FAILED;
int fd;
fd = open(filename, O_RDONLY);
if (fd < 0 || fstat(fd, &st) != 0)
if (fd < 0)
return NULL;
if (fstat(fd, &st))
goto failed;
*size = st.st_size;
map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
close(fd);
failed:
close(fd);
if (map == MAP_FAILED)
return NULL;
return map;