From 8ce18f9cdf53c846e0486130a66ba55c96fc2b14 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Tue, 26 Nov 2019 16:49:44 +1030 Subject: Avoid ubsan bug complaining about &p->field I reckon it's quite OK to write &p->field in C when p might be NULL, and lots of old C programmers probably agree with me. However, ubsan disagrees and so do some people I respect. I suspect C++ influence is to blame for the ubsan behaviour. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92634. So far no one has educated me as to why I'm wrong to claim that there isn't anything in the C standard to say that p->field is always (*p).field. Note 79 doesn't quite do that because it doesn't cover null pointers. If there was such an equivalence then you could claim &p->field has a null pointer reference when p is NULL, even though no C compiler would ever dereference p. Anyway, to silence ubsan I'm going to apply the following though I prefer to avoid casts when possible. And I'm using (void *) deliberately because this is C, not C++! * ldlang.c (lang_output_section_find_by_flags): Don't use &p->field when p might be NULL. * ldelf.c (output_rel_find, ldelf_place_orphan): Likewise. (insert_os_after, lang_insert_orphan, lookup_name): Likewise. (strip_excluded_output_sections, lang_clear_os_map): Likewise. (lang_check, lang_for_each_input_file): Likewise. (lang_reset_memory_regions, find_replacements_insert_point): Likewise. (find_rescan_insertion, lang_propagate_lma_regions): Likewise. (lang_record_phdrs): Likewise. * emultempl/alphaelf.em (alpha_after_open): Likewise. * emultempl/mmo.em (mmo_place_orphan): Likewise. * emultempl/pe.em (gld_${EMULATION_NAME}_place_orphan): Likewise. * emultempl/pep.em (gld_${EMULATION_NAME}_place_orphan): Likewise. * emultempl/ppc32elf.em (ppc_after_check_relocs): Likewise. * emultempl/spuelf.em (spu_before_allocation): Likewise. (embedded_spu_file): Likewise. --- ld/ldelf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ld/ldelf.c') diff --git a/ld/ldelf.c b/ld/ldelf.c index b27917c..19ec908 100644 --- a/ld/ldelf.c +++ b/ld/ldelf.c @@ -1780,7 +1780,7 @@ output_rel_find (int isdyn, int rela) lang_output_section_statement_type *last_rel = NULL; lang_output_section_statement_type *last_rel_alloc = NULL; - for (lookup = &lang_os_list.head->output_section_statement; + for (lookup = (void *) lang_os_list.head; lookup != NULL; lookup = lookup->next) { @@ -1952,7 +1952,7 @@ ldelf_place_orphan (asection *s, const char *secname, int constraint) { /* Find the output mbind section with the same type, attributes and sh_info field. */ - for (os = &lang_os_list.head->output_section_statement; + for (os = (void *) lang_os_list.head; os != NULL; os = os->next) if (os->bfd_section != NULL @@ -2129,7 +2129,7 @@ ldelf_place_orphan (asection *s, const char *secname, int constraint) _bfd_elf_match_sections_by_type); if (after == NULL) /* *ABS* is always the first output section statement. */ - after = &lang_os_list.head->output_section_statement; + after = (void *) lang_os_list.head; } return lang_insert_orphan (s, secname, constraint, after, place, NULL, NULL); -- cgit v1.1