rescoff: close bfd on failure paths

Also free malloc'd relocs.
This commit is contained in:
Alan Modra
2025-04-19 10:25:01 +09:30
parent 6e9683366d
commit d2533f464f

View File

@@ -138,12 +138,14 @@ read_coff_rsrc (const char *filename, const char *target)
if (bfd_get_error () == bfd_error_file_ambiguously_recognized) if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
list_matching_formats (matching); list_matching_formats (matching);
free (matching); free (matching);
bfd_close (abfd);
return NULL; return NULL;
} }
if (bfd_get_flavour (abfd) != bfd_target_coff_flavour if (bfd_get_flavour (abfd) != bfd_target_coff_flavour
|| !obj_pe (abfd)) || !obj_pe (abfd))
{ {
non_fatal (_("%s: not a PE file"), filename); non_fatal (_("%s: not a PE file"), filename);
bfd_close (abfd);
return NULL; return NULL;
} }
@@ -151,6 +153,7 @@ read_coff_rsrc (const char *filename, const char *target)
if (sec == NULL) if (sec == NULL)
{ {
non_fatal (_("%s: no resource section"), filename); non_fatal (_("%s: no resource section"), filename);
bfd_close (abfd);
return NULL; return NULL;
} }
@@ -163,6 +166,7 @@ read_coff_rsrc (const char *filename, const char *target)
if (size > (bfd_size_type) get_file_size (filename)) if (size > (bfd_size_type) get_file_size (filename))
{ {
non_fatal (_("%s: .rsrc section is bigger than the file!"), filename); non_fatal (_("%s: .rsrc section is bigger than the file!"), filename);
bfd_close (abfd);
return NULL; return NULL;
} }
@@ -528,6 +532,7 @@ write_coff_file (const char *filename, const char *target,
if (! bfd_set_format (abfd, bfd_object)) if (! bfd_set_format (abfd, bfd_object))
{ {
bfd_nonfatal ("bfd_set_format"); bfd_nonfatal ("bfd_set_format");
bfd_close_all_done (abfd);
return false; return false;
} }
@@ -535,24 +540,28 @@ write_coff_file (const char *filename, const char *target,
if (! bfd_set_arch_mach (abfd, bfd_arch_sh, 0)) if (! bfd_set_arch_mach (abfd, bfd_arch_sh, 0))
{ {
bfd_nonfatal ("bfd_set_arch_mach(sh)"); bfd_nonfatal ("bfd_set_arch_mach(sh)");
bfd_close_all_done (abfd);
return false; return false;
} }
#elif defined DLLTOOL_MIPS #elif defined DLLTOOL_MIPS
if (! bfd_set_arch_mach (abfd, bfd_arch_mips, 0)) if (! bfd_set_arch_mach (abfd, bfd_arch_mips, 0))
{ {
bfd_nonfatal ("bfd_set_arch_mach(mips)"); bfd_nonfatal ("bfd_set_arch_mach(mips)");
bfd_close_all_done (abfd);
return false; return false;
} }
#elif defined DLLTOOL_ARM #elif defined DLLTOOL_ARM
if (! bfd_set_arch_mach (abfd, bfd_arch_arm, 0)) if (! bfd_set_arch_mach (abfd, bfd_arch_arm, 0))
{ {
bfd_nonfatal ("bfd_set_arch_mach(arm)"); bfd_nonfatal ("bfd_set_arch_mach(arm)");
bfd_close_all_done (abfd);
return false; return false;
} }
#elif defined DLLTOOL_AARCH64 #elif defined DLLTOOL_AARCH64
if (! bfd_set_arch_mach (abfd, bfd_arch_aarch64, 0)) if (! bfd_set_arch_mach (abfd, bfd_arch_aarch64, 0))
{ {
bfd_nonfatal ("bfd_set_arch_mach(aarch64)"); bfd_nonfatal ("bfd_set_arch_mach(aarch64)");
bfd_close_all_done (abfd);
return false; return false;
} }
#else #else
@@ -560,6 +569,7 @@ write_coff_file (const char *filename, const char *target,
if (! bfd_set_arch_mach (abfd, bfd_arch_i386, 0)) if (! bfd_set_arch_mach (abfd, bfd_arch_i386, 0))
{ {
bfd_nonfatal ("bfd_set_arch_mach(i386)"); bfd_nonfatal ("bfd_set_arch_mach(i386)");
bfd_close_all_done (abfd);
return false; return false;
} }
#endif #endif
@@ -567,6 +577,7 @@ write_coff_file (const char *filename, const char *target,
if (! bfd_set_file_flags (abfd, HAS_SYMS | HAS_RELOC)) if (! bfd_set_file_flags (abfd, HAS_SYMS | HAS_RELOC))
{ {
bfd_nonfatal ("bfd_set_file_flags"); bfd_nonfatal ("bfd_set_file_flags");
bfd_close_all_done (abfd);
return false; return false;
} }
@@ -576,12 +587,14 @@ write_coff_file (const char *filename, const char *target,
if (sec == NULL) if (sec == NULL)
{ {
bfd_nonfatal ("bfd_make_section"); bfd_nonfatal ("bfd_make_section");
bfd_close_all_done (abfd);
return false; return false;
} }
if (! bfd_set_symtab (abfd, &sec->symbol, 1)) if (! bfd_set_symtab (abfd, &sec->symbol, 1))
{ {
bfd_nonfatal ("bfd_set_symtab"); bfd_nonfatal ("bfd_set_symtab");
bfd_close_all_done (abfd);
return false; return false;
} }
@@ -628,7 +641,11 @@ write_coff_file (const char *filename, const char *target,
/* Actually convert the resources to binary. */ /* Actually convert the resources to binary. */
if (!coff_to_bin (resources, &cwi)) if (!coff_to_bin (resources, &cwi))
return false; {
bfd_close_all_done (abfd);
free (cwi.relocs);
return false;
}
/* Add another few bytes to the directory strings if needed for /* Add another few bytes to the directory strings if needed for
alignment. */ alignment. */
@@ -655,6 +672,8 @@ write_coff_file (const char *filename, const char *target,
if (!bfd_set_section_size (sec, length)) if (!bfd_set_section_size (sec, length))
{ {
bfd_nonfatal ("bfd_set_section_size"); bfd_nonfatal ("bfd_set_section_size");
bfd_close_all_done (abfd);
free (cwi.relocs);
return false; return false;
} }
@@ -666,6 +685,8 @@ write_coff_file (const char *filename, const char *target,
if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length)) if (! bfd_set_section_contents (abfd, sec, d->data, offset, d->length))
{ {
bfd_nonfatal ("bfd_set_section_contents"); bfd_nonfatal ("bfd_set_section_contents");
bfd_close_all_done (abfd);
free (cwi.relocs);
return false; return false;
} }
offset += d->length; offset += d->length;
@@ -684,7 +705,11 @@ write_coff_file (const char *filename, const char *target,
{ {
if (res_to_bin (cwi.wrbfd, (rc_uint_type) offset, rd->res) if (res_to_bin (cwi.wrbfd, (rc_uint_type) offset, rd->res)
== (rc_uint_type) -1) == (rc_uint_type) -1)
return false; {
bfd_close_all_done (abfd);
free (cwi.relocs);
return false;
}
offset += rd->length; offset += rd->length;
} }
@@ -693,6 +718,7 @@ write_coff_file (const char *filename, const char *target,
if (! bfd_close (abfd)) if (! bfd_close (abfd))
{ {
bfd_nonfatal ("bfd_close"); bfd_nonfatal ("bfd_close");
free (cwi.relocs);
return false; return false;
} }