From 89bdc77eabf5ede68322f6e47e003c1dc45b9ccb Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Mon, 2 Sep 2019 16:13:05 +0930 Subject: PR24955, libbfd terminating program on out of memory This patch fixes the worst of the cases where libbfd might terminate a program due to calling xstrdup or xmalloc. I've also fixed some error paths that didn't clean up properly. PR 24955 * libbfd-in.h (bfd_strdup): New inline function. * archive.c (_bfd_get_elt_at_filepos): Use bfd_strdup. Close bfd on error. * elfcode.h (_bfd_elf_bfd_from_remote_memory): Use bfd_strdup. * opncls.c (bfd_fopen): Use bfd_strdup. Close fd and stream on error. (bfd_openstreamr): Use bfd_strdup. (bfd_openr_iovec, bfd_openw, bfd_create): Likewise. * plugin.c (try_load_plugin): Use bfd_malloc. * libbfd.h: Regenerate. --- bfd/archive.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'bfd/archive.c') diff --git a/bfd/archive.c b/bfd/archive.c index 690718e..ef71e8a 100644 --- a/bfd/archive.c +++ b/bfd/archive.c @@ -728,7 +728,9 @@ _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos) else { n_bfd->origin = n_bfd->proxy_origin; - n_bfd->filename = xstrdup (filename); + n_bfd->filename = bfd_strdup (filename); + if (n_bfd->filename == NULL) + goto out; } n_bfd->arelt_data = new_areldata; @@ -745,8 +747,10 @@ _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos) || _bfd_add_bfd_to_archive_cache (archive, filepos, n_bfd)) return n_bfd; + out: free (new_areldata); n_bfd->arelt_data = NULL; + bfd_close (n_bfd); return NULL; } -- cgit v1.1