diff options
author | Alan Modra <amodra@gmail.com> | 2023-09-19 09:39:31 +0930 |
---|---|---|
committer | Alan Modra <amodra@gmail.com> | 2023-09-20 08:23:47 +0930 |
commit | a1d1634d0012ddeecc59bb0e6012d4455edae6e8 (patch) | |
tree | ba213a2d27da84a2ec41bc2a1e7fc6ac23730131 /bfd | |
parent | a56e5dce69bfad45ee6977a916ccea283e087e8b (diff) | |
download | gdb-a1d1634d0012ddeecc59bb0e6012d4455edae6e8.zip gdb-a1d1634d0012ddeecc59bb0e6012d4455edae6e8.tar.gz gdb-a1d1634d0012ddeecc59bb0e6012d4455edae6e8.tar.bz2 |
elf-attrs.c memory allocation fail
Report errors rather than segfaulting.
bfd/
* elf-attrs.c (elf_new_obj_attr): Return NULL on bfd_alloc fail.
(bfd_elf_add_obj_attr_int): Handle NULL return from the above,
and propagate return to callers.
(elf_add_obj_attr_string, elf_add_obj_attr_int_string): Likewise.
(bfd_elf_add_obj_attr_string): Similarly.
(_bfd_elf_copy_obj_attributes): Report error on alloc fails.
(_bfd_elf_parse_attributes): Likewise.
* elf-bfd.h (bfd_elf_add_obj_attr_int): Update prototype.
(bfd_elf_add_obj_attr_string): Likewise.
(bfd_elf_add_obj_attr_int_string): Likewise.
gas/
* config/obj-elf.c (obj_elf_vendor_attribute): Report fatal
error on out of memory from bfd attribute functions.
* config/tc-arc.c (arc_set_attribute_int): Likewise.
(arc_set_attribute_string, arc_set_public_attributes): Likewise.
* config/tc-arm.c (aeabi_set_attribute_int): Likewise.
(aeabi_set_attribute_string): Likewise.
* config/tc-mips.c (mips_md_finish): Likewise.
* config/tc-msp430.c (msp430_md_finish): Likewise.
* config/tc-riscv.c (riscv_write_out_attrs): Likewise.
* config/tc-sparc.c (sparc_md_finish): Likewise.
* config/tc-tic6x.c (tic6x_set_attribute_int): Likewise.
* config/tc-csky.c (md_begin): Likewise.
(set_csky_attribute): Return ok status.
Diffstat (limited to 'bfd')
-rw-r--r-- | bfd/elf-attrs.c | 83 | ||||
-rw-r--r-- | bfd/elf-bfd.h | 10 |
2 files changed, 62 insertions, 31 deletions
diff --git a/bfd/elf-attrs.c b/bfd/elf-attrs.c index 9b19d4a..9a8ef23 100644 --- a/bfd/elf-attrs.c +++ b/bfd/elf-attrs.c @@ -247,6 +247,8 @@ elf_new_obj_attr (bfd *abfd, int vendor, unsigned int tag) /* Create a new tag. */ list = (obj_attribute_list *) bfd_alloc (abfd, sizeof (obj_attribute_list)); + if (list == NULL) + return NULL; memset (list, 0, sizeof (obj_attribute_list)); list->tag = tag; /* Keep the tag list in order. */ @@ -292,14 +294,18 @@ bfd_elf_get_obj_attr_int (bfd *abfd, int vendor, unsigned int tag) } /* Add an integer object attribute. */ -void +obj_attribute * bfd_elf_add_obj_attr_int (bfd *abfd, int vendor, unsigned int tag, unsigned int i) { obj_attribute *attr; attr = elf_new_obj_attr (abfd, vendor, tag); - attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); - attr->i = i; + if (attr != NULL) + { + attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); + attr->i = i; + } + return attr; } /* Duplicate an object attribute string value. */ @@ -330,42 +336,54 @@ _bfd_elf_attr_strdup (bfd *abfd, const char *s) } /* Add a string object attribute. */ -static void +static obj_attribute * elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag, const char *s, const char *end) { obj_attribute *attr; attr = elf_new_obj_attr (abfd, vendor, tag); - attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); - attr->s = elf_attr_strdup (abfd, s, end); + if (attr != NULL) + { + attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); + attr->s = elf_attr_strdup (abfd, s, end); + if (attr->s == NULL) + return NULL; + } + return attr; } -void +obj_attribute * bfd_elf_add_obj_attr_string (bfd *abfd, int vendor, unsigned int tag, const char *s) { - elf_add_obj_attr_string (abfd, vendor, tag, s, NULL); + return elf_add_obj_attr_string (abfd, vendor, tag, s, NULL); } /* Add a int+string object attribute. */ -static void +static obj_attribute * elf_add_obj_attr_int_string (bfd *abfd, int vendor, unsigned int tag, unsigned int i, const char *s, const char *end) { obj_attribute *attr; attr = elf_new_obj_attr (abfd, vendor, tag); - attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); - attr->i = i; - attr->s = elf_attr_strdup (abfd, s, end); + if (attr != NULL) + { + attr->type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); + attr->i = i; + attr->s = elf_attr_strdup (abfd, s, end); + if (attr->s == NULL) + return NULL; + } + return attr; } -void +obj_attribute * bfd_elf_add_obj_attr_int_string (bfd *abfd, int vendor, unsigned int tag, unsigned int i, const char *s) { - elf_add_obj_attr_int_string (abfd, vendor, tag, i, s, NULL); + return elf_add_obj_attr_int_string (abfd, vendor, tag, i, s, NULL); } /* Copy the object attributes from IBFD to OBFD. */ @@ -393,7 +411,11 @@ _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd) out_attr->type = in_attr->type; out_attr->i = in_attr->i; if (in_attr->s && *in_attr->s) - out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s); + { + out_attr->s = _bfd_elf_attr_strdup (obfd, in_attr->s); + if (out_attr->s == NULL) + bfd_perror (_("error adding attribute")); + } in_attr++; out_attr++; } @@ -402,23 +424,27 @@ _bfd_elf_copy_obj_attributes (bfd *ibfd, bfd *obfd) list; list = list->next) { + bool ok = false; in_attr = &list->attr; switch (in_attr->type & (ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL)) { case ATTR_TYPE_FLAG_INT_VAL: - bfd_elf_add_obj_attr_int (obfd, vendor, list->tag, in_attr->i); + ok = bfd_elf_add_obj_attr_int (obfd, vendor, + list->tag, in_attr->i); break; case ATTR_TYPE_FLAG_STR_VAL: - bfd_elf_add_obj_attr_string (obfd, vendor, list->tag, - in_attr->s); + ok = bfd_elf_add_obj_attr_string (obfd, vendor, list->tag, + in_attr->s); break; case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: - bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag, - in_attr->i, in_attr->s); + ok = bfd_elf_add_obj_attr_int_string (obfd, vendor, list->tag, + in_attr->i, in_attr->s); break; default: abort (); } + if (!ok) + bfd_perror (_("error adding attribute")); } } } @@ -563,6 +589,7 @@ _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr) while (p < end) { int type; + bool ok = false; tag = _bfd_safe_read_leb128 (abfd, &p, false, end); type = _bfd_elf_obj_attrs_arg_type (abfd, vendor, tag); @@ -570,28 +597,30 @@ _bfd_elf_parse_attributes (bfd *abfd, Elf_Internal_Shdr * hdr) { case ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL: val = _bfd_safe_read_leb128 (abfd, &p, false, end); - elf_add_obj_attr_int_string (abfd, vendor, tag, val, - (char *) p, - (char *) end); + ok = elf_add_obj_attr_int_string (abfd, vendor, tag, + val, (char *) p, + (char *) end); p += strnlen ((char *) p, end - p); if (p < end) p++; break; case ATTR_TYPE_FLAG_STR_VAL: - elf_add_obj_attr_string (abfd, vendor, tag, - (char *) p, - (char *) end); + ok = elf_add_obj_attr_string (abfd, vendor, tag, + (char *) p, + (char *) end); p += strnlen ((char *) p, end - p); if (p < end) p++; break; case ATTR_TYPE_FLAG_INT_VAL: val = _bfd_safe_read_leb128 (abfd, &p, false, end); - bfd_elf_add_obj_attr_int (abfd, vendor, tag, val); + ok = bfd_elf_add_obj_attr_int (abfd, vendor, tag, val); break; default: abort (); } + if (!ok) + bfd_perror (_("error adding attribute")); } break; case Tag_Section: diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index d9ab5bd..52d4d39 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -3009,14 +3009,16 @@ extern bfd *_bfd_elf64_bfd_from_remote_memory extern bfd_vma bfd_elf_obj_attr_size (bfd *); extern void bfd_elf_set_obj_attr_contents (bfd *, bfd_byte *, bfd_vma); extern int bfd_elf_get_obj_attr_int (bfd *, int, unsigned int); -extern void bfd_elf_add_obj_attr_int (bfd *, int, unsigned int, unsigned int); +extern obj_attribute *bfd_elf_add_obj_attr_int + (bfd *, int, unsigned int, unsigned int); #define bfd_elf_add_proc_attr_int(BFD, TAG, VALUE) \ bfd_elf_add_obj_attr_int ((BFD), OBJ_ATTR_PROC, (TAG), (VALUE)) -extern void bfd_elf_add_obj_attr_string (bfd *, int, unsigned int, const char *); +extern obj_attribute *bfd_elf_add_obj_attr_string + (bfd *, int, unsigned int, const char *); #define bfd_elf_add_proc_attr_string(BFD, TAG, VALUE) \ bfd_elf_add_obj_attr_string ((BFD), OBJ_ATTR_PROC, (TAG), (VALUE)) -extern void bfd_elf_add_obj_attr_int_string (bfd *, int, unsigned int, - unsigned int, const char *); +extern obj_attribute *bfd_elf_add_obj_attr_int_string + (bfd *, int, unsigned int, unsigned int, const char *); #define bfd_elf_add_proc_attr_int_string(BFD, TAG, INTVAL, STRVAL) \ bfd_elf_add_obj_attr_int_string ((BFD), OBJ_ATTR_PROC, (TAG), \ (INTVAL), (STRVAL)) |