Skip to content

Commit

Permalink
modpost: Stop grab_file() from leaking filedescriptors if fstat() fails
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
jjuhl authored and rustyrussell committed May 23, 2012
1 parent 6101167 commit eb3d5cc
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions scripts/mod/modpost.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit eb3d5cc

Please sign in to comment.