* objdump.c (slurp_file): Close file if fstat fails.

This commit is contained in:
Alan Modra 2012-02-20 03:25:21 +00:00
parent fbe2ec189e
commit 6c7130122d
2 changed files with 15 additions and 8 deletions

View File

@ -1,3 +1,7 @@
2012-02-20 Namhyung Kim <namhyung.kim@lge.com>
* objdump.c (slurp_file): Close file if fstat fails.
2012-02-14 Cary Coutant <ccoutant@google.com> 2012-02-14 Cary Coutant <ccoutant@google.com>
* dwarf.c (dwarf_vmatoa64): New function. * dwarf.c (dwarf_vmatoa64): New function.

View File

@ -1123,25 +1123,28 @@ slurp_file (const char *fn, size_t *size)
if (fd < 0) if (fd < 0)
return NULL; return NULL;
if (fstat (fd, &st) < 0) if (fstat (fd, &st) < 0)
return NULL; {
close (fd);
return NULL;
}
*size = st.st_size; *size = st.st_size;
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
msize = (*size + ps - 1) & ~(ps - 1); msize = (*size + ps - 1) & ~(ps - 1);
map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0); map = mmap (NULL, msize, PROT_READ, MAP_SHARED, fd, 0);
if (map != (char *)-1L) if (map != (char *) -1L)
{ {
close(fd); close (fd);
return map; return map;
} }
#endif #endif
map = (const char *) malloc (*size); map = (const char *) malloc (*size);
if (!map || (size_t) read (fd, (char *)map, *size) != *size) if (!map || (size_t) read (fd, (char *) map, *size) != *size)
{ {
free ((void *)map); free ((void *) map);
map = NULL; map = NULL;
} }
close (fd); close (fd);
return map; return map;
} }
#define line_map_decrease 5 #define line_map_decrease 5