aboutsummaryrefslogtreecommitdiff
path: root/ld
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2025-01-11 16:13:51 +1030
committerAlan Modra <amodra@gmail.com>2025-01-17 08:35:17 +1030
commit8f5e60c87785008067c63772d9c651af626eef88 (patch)
tree78a83334d5026553181a8519865c6a3bdba53cb5 /ld
parentfc15dc69aed4e9e3f849d02f118d2e236ac935cf (diff)
downloadbinutils-8f5e60c87785008067c63772d9c651af626eef88.zip
binutils-8f5e60c87785008067c63772d9c651af626eef88.tar.gz
binutils-8f5e60c87785008067c63772d9c651af626eef88.tar.bz2
write_build_id and write_package_metadata leaks
There isn't much sense in stashing contents in sec->contents after those contents have been written. * ldelf.c (write_build_id): Don't assign sec->contents. Free contents if malloc'd here. (write_package_metadata): Likewise.
Diffstat (limited to 'ld')
-rw-r--r--ld/ldelf.c38
1 files changed, 20 insertions, 18 deletions
diff --git a/ld/ldelf.c b/ld/ldelf.c
index ce08058..4a1aa04 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1458,14 +1458,12 @@ write_build_id (bfd *abfd)
}
i_shdr = &elf_section_data (asec->output_section)->this_hdr;
- if (i_shdr->contents == NULL)
- {
- if (asec->contents == NULL)
- asec->contents = (unsigned char *) xmalloc (asec->size);
- contents = asec->contents;
- }
- else
+ if (i_shdr->contents != NULL)
contents = i_shdr->contents + asec->output_offset;
+ else if (asec->contents != NULL)
+ contents = asec->contents;
+ else
+ contents = xmalloc (asec->size);
e_note = (Elf_External_Note *) contents;
size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
@@ -1485,8 +1483,11 @@ write_build_id (bfd *abfd)
position = i_shdr->sh_offset + asec->output_offset;
size = asec->size;
- return (bfd_seek (abfd, position, SEEK_SET) == 0
- && bfd_write (contents, size, abfd) == size);
+ bool ret = (bfd_seek (abfd, position, SEEK_SET) == 0
+ && bfd_write (contents, size, abfd) == size);
+ if (i_shdr->contents == NULL && asec->contents == NULL)
+ free (contents);
+ return ret;
}
/* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
@@ -1547,14 +1548,12 @@ write_package_metadata (bfd *abfd)
}
i_shdr = &elf_section_data (asec->output_section)->this_hdr;
- if (i_shdr->contents == NULL)
- {
- if (asec->contents == NULL)
- asec->contents = (unsigned char *) xmalloc (asec->size);
- contents = asec->contents;
- }
- else
+ if (i_shdr->contents != NULL)
contents = i_shdr->contents + asec->output_offset;
+ else if (asec->contents != NULL)
+ contents = asec->contents;
+ else
+ contents = xmalloc (asec->size);
e_note = (Elf_External_Note *) contents;
size = offsetof (Elf_External_Note, name[sizeof "FDO"]);
@@ -1573,8 +1572,11 @@ write_package_metadata (bfd *abfd)
position = i_shdr->sh_offset + asec->output_offset;
size = asec->size;
- return (bfd_seek (abfd, position, SEEK_SET) == 0
- && bfd_write (contents, size, abfd) == size);
+ bool ret = (bfd_seek (abfd, position, SEEK_SET) == 0
+ && bfd_write (contents, size, abfd) == size);
+ if (i_shdr->contents == NULL && asec->contents == NULL)
+ free (contents);
+ return ret;
}
/* Make .note.package section.