public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [COMMITTED] readelf: Check phdr != NULL or shdr != NULL in handle_dynamic.
Date: Thu,  3 Nov 2022 14:17:42 +0100	[thread overview]
Message-ID: <20221103131742.18109-1-mark@klomp.org> (raw)

The compiler doesn't know that when use_dynamic_segment is true,
then phdr should/will be non-NULL and otherwise shdr is non-NULL.
Add explicit checks to help the compiler out and in case an error
is made calling the handle_dynamic function.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog |  5 +++++
 src/readelf.c | 10 +++++-----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index d3399a5c..0c5ab37e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2022-11-03  Mark Wielaard  <mark@klomp.org>
+
+	* readelf.c (handle_dynamic): Check phdr != NULL when
+	use_dynamic_segment, otherwise check shdr != NULL.
+
 2022-10-28  Arsen Arsenović  <arsen@aarsen.me>
 
 	* readelf.c (options): Add Binutils-style --syms alias.
diff --git a/src/readelf.c b/src/readelf.c
index 0e0b05c4..e721a209 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -1828,7 +1828,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
   size_t dyn_ents;
 
   /* Get the data of the section.  */
-  if (use_dynamic_segment)
+  if (use_dynamic_segment && phdr != NULL)
     data = elf_getdata_rawchunk(ebl->elf, phdr->p_offset,
 				phdr->p_filesz, ELF_T_DYN);
   else
@@ -1840,7 +1840,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
   /* Get the dynamic section entry number */
   dyn_ents = get_dyn_ents (data);
 
-  if (!use_dynamic_segment)
+  if (!use_dynamic_segment && shdr != NULL)
     {
       /* Get the section header string table index.  */
       if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
@@ -1862,7 +1862,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
 	      (int) shdr->sh_link,
 	      elf_strptr (ebl->elf, shstrndx, glink->sh_name));
     }
-  else
+  else if (phdr != NULL)
     {
       printf (ngettext ("\
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "\n",
@@ -1879,7 +1879,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
   /* if --use-dynamic option is enabled,
      use the string table to get the related library info.  */
   Elf_Data *strtab_data = NULL;
-  if (use_dynamic_segment)
+  if (use_dynamic_segment && phdr != NULL)
     {
       strtab_data = get_dynscn_strtab(ebl->elf, phdr);
       if (strtab_data == NULL)
@@ -1903,7 +1903,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, GElf_Phdr *phdr)
 	  || dyn->d_tag == DT_RPATH
 	  || dyn->d_tag == DT_RUNPATH)
 	{
-	  if (! use_dynamic_segment)
+	  if (! use_dynamic_segment && shdr != NULL)
 	    name = elf_strptr (ebl->elf, shdr->sh_link, dyn->d_un.d_val);
 	  else if (dyn->d_un.d_val < strtab_data->d_size
 		   && memrchr (strtab_data->d_buf + dyn->d_un.d_val, '\0',
-- 
2.18.4


                 reply	other threads:[~2022-11-03 13:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221103131742.18109-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).