libdwfl: Declare possible zero sized arrays only when non-zero
authorMark Wielaard <mark@klomp.org>
Thu, 6 Jan 2022 16:58:59 +0000 (17:58 +0100)
committerMark Wielaard <mark@klomp.org>
Thu, 6 Jan 2022 16:58:59 +0000 (17:58 +0100)
The gcc undefined sanitizer complains when seeing a zero sized array
declaration. Move the declaration to the point in the code where we
know they aren't zero sized.

https://sourceware.org/bugzilla/show_bug.cgi?id=28720

Signed-off-by: Mark Wielaard <mark@klomp.org>
libdwfl/ChangeLog
libdwfl/link_map.c

index aace969..b2588b1 100644 (file)
@@ -1,5 +1,10 @@
 2022-01-03  Mark Wielaard  <mark@klomp.org>
 
+       * link_map.c (dwfl_link_map_report): Only declare d32 and d64 before
+       actual use.
+
+2022-01-03  Mark Wielaard  <mark@klomp.org>
+
        * dwfl_segment_report_module.c (dwfl_segment_report_module): Copy
        dyn_data and set xlatefrom.d_buf to dyns when dyns is not aligned.
 
index 99222bb..c0207cd 100644 (file)
@@ -1037,8 +1037,6 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
                  return false;
                }
              void *buf = malloc (dyn_filesz);
-             Elf32_Dyn (*d32)[dyn_filesz / sizeof (Elf32_Dyn)] = buf;
-             Elf64_Dyn (*d64)[dyn_filesz / sizeof (Elf64_Dyn)] = buf;
              if (unlikely (buf == NULL))
                {
                  __libdwfl_seterrno (DWFL_E_NOMEM);
@@ -1068,6 +1066,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
                  /* We are looking for DT_DEBUG.  */
                  if (elfclass == ELFCLASS32)
                    {
+                     Elf32_Dyn (*d32)[dyn_filesz / sizeof (Elf32_Dyn)] = buf;
                      size_t n = dyn_filesz / sizeof (Elf32_Dyn);
                      for (size_t i = 0; i < n; ++i)
                        if ((*d32)[i].d_tag == DT_DEBUG)
@@ -1078,6 +1077,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
                    }
                  else
                    {
+                     Elf64_Dyn (*d64)[dyn_filesz / sizeof (Elf64_Dyn)] = buf;
                      size_t n = dyn_filesz / sizeof (Elf64_Dyn);
                      for (size_t i = 0; i < n; ++i)
                        if ((*d64)[i].d_tag == DT_DEBUG)