warn unused result for bfd IO functions

This patch fixes all the warnings I found in bfd, binutils and ld,
plus some bitrotted COFF_GO32 code that tried to allocate -168ul
bytes.  When the malloc fail was reported these testsuite fails
resulted:

i386-go32  +FAIL: go32 stub
i386-go32  +ERROR: tcl error sourcing /home/alan/src/binutils-gdb/ld/testsuite/ld-i386/i386.exp.
i386-go32  +ERROR: couldn't open "tmpdir/go32stub": no such file or directory
i386-go32  +FAIL: ld-scripts/sane1
i386-go32  +FAIL: ld-scripts/assign-loc
i386-go32  +FAIL: ld-scripts/pr18963

This does result in some warnings in gdb which are fixed in a followup
patch.

bfd/
	* bfdio.c (bfd_read, bfd_write): Add ATTRIBUTE_WARN_UNUSED_RESULT.
	(bfd_tell, bfd_stat, bfd_seek, bfd_mmap): Likewise.
	* bfd-in2.h: Regenerate.
	* coff-rs6000.c (xcoff_write_armap_big) Don't ignore bfd_write
	return value.
	(xcoff_generate_rtinit): Likewise.  Also free data_buffer and
	string_table before returning.
	* coff64-rs6000.c (xcoff64_generate_rtinit): Likewise.
	* coff-stgo32.c (go32exe_check_format): Don't ignore bfd_seek
	return value.
	* coffcode.h (coff_apply_checksum): Don't ignore bfd_write return.
	(coff_write_object_contents <COFF_GO32>): Likewise, and bfd_malloc.
	Fix bitrotted code to look for first section with non-zero filepos.
	* elf64-ia64-vms.c (elf64_vms_write_shdrs_and_ehdr): Don't ignore
	bfd_seek or bfd_write return values.
	* pef.c (bfd_pef_scan_section): Likewise.
	(bfd_pef_read_header, bfd_pef_xlib_read_header): Likewise.
	* vms-misc.c (_bfd_vms_output_end): Likewise.  Return status.
	* vms.h (_bfd_vms_output_end): Update prototype.
	* vms-alpha.c: Pass _bfd_vms_output_end status up call chains.
	* wasm-module.c (wasm_compute_custom_section_file_position): Don't
	ignore bfd_seek or bfd_write return values.
	(wasm_compute_section_file_positions): Likewise.
	* xsym.c (bfd_sym_scan): Don't ignore bfd_seek return value.
	(bfd_sym_read_name_table): Likewise.
binutils/
	* ar.c (print_contents, extract_file): Don't ignore bfd_seek
	return value.
ld/
	* pdb.c (create_section_contrib_substream): Don't ignore bfd_seek
	return value.
	(create_section_header_stream): Likewise.
	* pe-dll.c (pe_get16, pe_get32): Add fail param to return results
	from bfd_seek and bfd_read.
	(pe_implied_import_dll): Handle these fails, and other bfd_seek
	and bfd_read return values.
This commit is contained in:
Alan Modra
2023-08-07 20:59:08 +09:30
parent 934ee74bc0
commit e416bd75c3
16 changed files with 250 additions and 159 deletions

View File

@@ -3419,22 +3419,30 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
handled, FALSE if not. */
static unsigned int
pe_get16 (bfd *abfd, int where)
pe_get16 (bfd *abfd, int where, bool *fail)
{
unsigned char b[2];
bfd_seek (abfd, where, SEEK_SET);
bfd_read (b, 2, abfd);
if (bfd_seek (abfd, where, SEEK_SET) != 0
|| bfd_read (b, 2, abfd) != 2)
{
*fail = true;
return 0;
}
return b[0] + (b[1] << 8);
}
static unsigned int
pe_get32 (bfd *abfd, int where)
pe_get32 (bfd *abfd, int where, bool *fail)
{
unsigned char b[4];
bfd_seek (abfd, where, SEEK_SET);
bfd_read (b, 4, abfd);
if (bfd_seek (abfd, where, SEEK_SET) != 0
|| bfd_read (b, 4, abfd) != 4)
{
*fail = true;
return 0;
}
return b[0] + (b[1] << 8) + (b[2] << 16) + ((unsigned) b[3] << 24);
}
@@ -3481,38 +3489,49 @@ pe_implied_import_dll (const char *filename)
/* PEI dlls seem to be bfd_objects. */
if (!bfd_check_format (dll, bfd_object))
{
notdll:
einfo (_("%X%P: %s: this doesn't appear to be a DLL\n"), filename);
return false;
}
/* Get pe_header, optional header and numbers of directory entries. */
pe_header_offset = pe_get32 (dll, 0x3c);
bool fail = false;
pe_header_offset = pe_get32 (dll, 0x3c, &fail);
if (fail)
goto notdll;
opthdr_ofs = pe_header_offset + 4 + 20;
#ifdef pe_use_plus
num_entries = pe_get32 (dll, opthdr_ofs + 92 + 4 * 4); /* & NumberOfRvaAndSizes. */
/* NumberOfRvaAndSizes. */
num_entries = pe_get32 (dll, opthdr_ofs + 92 + 4 * 4, &fail);
#else
num_entries = pe_get32 (dll, opthdr_ofs + 92);
num_entries = pe_get32 (dll, opthdr_ofs + 92, &fail);
#endif
if (fail)
goto notdll;
/* No import or export directory entry. */
if (num_entries < 1)
return false;
#ifdef pe_use_plus
export_rva = pe_get32 (dll, opthdr_ofs + 96 + 4 * 4);
export_size = pe_get32 (dll, opthdr_ofs + 100 + 4 * 4);
export_rva = pe_get32 (dll, opthdr_ofs + 96 + 4 * 4, &fail);
export_size = pe_get32 (dll, opthdr_ofs + 100 + 4 * 4, &fail);
#else
export_rva = pe_get32 (dll, opthdr_ofs + 96);
export_size = pe_get32 (dll, opthdr_ofs + 100);
export_rva = pe_get32 (dll, opthdr_ofs + 96, &fail);
export_size = pe_get32 (dll, opthdr_ofs + 100, &fail);
#endif
if (fail)
goto notdll;
/* No export table - nothing to export. */
if (export_size == 0)
return false;
nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
nsections = pe_get16 (dll, pe_header_offset + 4 + 2, &fail);
secptr = (pe_header_offset + 4 + 20 +
pe_get16 (dll, pe_header_offset + 4 + 16));
pe_get16 (dll, pe_header_offset + 4 + 16, &fail));
if (fail)
goto notdll;
expptr = 0;
/* Get the rva and size of the export section. */
@@ -3520,12 +3539,14 @@ pe_implied_import_dll (const char *filename)
{
char sname[8];
bfd_vma secptr1 = secptr + 40 * i;
bfd_vma vaddr = pe_get32 (dll, secptr1 + 12);
bfd_vma vsize = pe_get32 (dll, secptr1 + 16);
bfd_vma fptr = pe_get32 (dll, secptr1 + 20);
bfd_vma vaddr = pe_get32 (dll, secptr1 + 12, &fail);
bfd_vma vsize = pe_get32 (dll, secptr1 + 16, &fail);
bfd_vma fptr = pe_get32 (dll, secptr1 + 20, &fail);
bfd_seek (dll, secptr1, SEEK_SET);
bfd_read (sname, 8, dll);
if (fail
|| bfd_seek (dll, secptr1, SEEK_SET) != 0
|| bfd_read (sname, 8, dll) != 8)
goto notdll;
if (vaddr <= export_rva && vaddr + vsize > export_rva)
{
@@ -3541,14 +3562,16 @@ pe_implied_import_dll (const char *filename)
for (i = 0; i < nsections; i++)
{
bfd_vma secptr1 = secptr + 40 * i;
bfd_vma vsize = pe_get32 (dll, secptr1 + 8);
bfd_vma vaddr = pe_get32 (dll, secptr1 + 12);
bfd_vma flags = pe_get32 (dll, secptr1 + 36);
bfd_vma vsize = pe_get32 (dll, secptr1 + 8, &fail);
bfd_vma vaddr = pe_get32 (dll, secptr1 + 12, &fail);
bfd_vma flags = pe_get32 (dll, secptr1 + 36, &fail);
char sec_name[9];
sec_name[8] = '\0';
bfd_seek (dll, secptr1 + 0, SEEK_SET);
bfd_read (sec_name, 8, dll);
if (fail
|| bfd_seek (dll, secptr1 + 0, SEEK_SET) != 0
|| bfd_read (sec_name, 8, dll) != 8)
goto notdll;
if (strcmp(sec_name,".data") == 0)
{
@@ -3583,8 +3606,9 @@ pe_implied_import_dll (const char *filename)
}
expdata = xmalloc (export_size);
bfd_seek (dll, expptr, SEEK_SET);
bfd_read (expdata, export_size, dll);
if (bfd_seek (dll, expptr, SEEK_SET) != 0
|| bfd_read (expdata, export_size, dll) != export_size)
goto notdll;
erva = (char *) expdata - export_rva;
if (pe_def_file == 0)