- if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sz))
- {
- got_a_section = TRUE;
- print_strings (file, (FILE *) NULL, sect->filepos, 0, sz, mem);
- }
- free (mem);
+ if (bfd_stat (abfd, &st))
+ return;
+
+ /* Cache the result so that we do not repeatedly stat this file. */
+ *filesizep = st.st_size;
+ }
+
+ /* Compare the size of the section against the size of the file.
+ If the section is bigger then the file must be corrupt and
+ we should not try dumping it. */
+ if (sectsize >= *filesizep)
+ return;
+
+ mem = xmalloc (sectsize);
+
+ if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sectsize))
+ {
+ got_a_section = TRUE;
+
+ print_strings (filename_and_sizep->filename, NULL, sect->filepos,
+ 0, sectsize, (char *) mem);