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: [PATCH] libelf: Correctly decode ar_mode as octal string
Date: Sun, 28 Aug 2022 20:10:34 +0200	[thread overview]
Message-ID: <20220828181034.115031-1-mark@klomp.org> (raw)

ar_mode is encoded as an octal ascii string, not decimal. Add a new
OCT_FIELD macro to decode it.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---

This was found by the run-ar-N.sh testcase on the try builder.

 libelf/ChangeLog   |  5 +++++
 libelf/elf_begin.c | 25 +++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 35f49516..558d795e 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,8 @@
+2022-08-28  Mark Wielaard  <mark@klomp.org>
+
+	* elf_begin.c (__libelf_next_arhdr_wrlock): Add OCT_FIELD macro,
+	like INT_FIELD but use strtol with octal base 8. Use for ar_mode.
+
 2022-08-08  Andreas Schwab  <schwab@suse.de>
 
 	* elf.h: Update from glibc.
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 17d9b1f3..71eb3594 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -977,7 +977,8 @@ __libelf_next_arhdr_wrlock (Elf *elf)
      atoll depending on the size of the types.  We are also prepared
      for the case where the whole field in the `struct ar_hdr' is
      filled in which case we cannot simply use atol/l but instead have
-     to create a temporary copy.  */
+     to create a temporary copy.  Note that all fields use decimal
+     encoding, except ar_mode which uses octal.  */
 
 #define INT_FIELD(FIELD)						      \
   do									      \
@@ -997,10 +998,30 @@ __libelf_next_arhdr_wrlock (Elf *elf)
     }									      \
   while (0)
 
+#define OCT_FIELD(FIELD)						      \
+  do									      \
+    {									      \
+      char buf[sizeof (ar_hdr->FIELD) + 1];				      \
+      const char *string = ar_hdr->FIELD;				      \
+      if (ar_hdr->FIELD[sizeof (ar_hdr->FIELD) - 1] != ' ')		      \
+	{								      \
+	  *((char *) mempcpy (buf, ar_hdr->FIELD, sizeof (ar_hdr->FIELD)))  \
+	    = '\0';							      \
+	  string = buf;							      \
+	}								      \
+      if (sizeof (elf_ar_hdr->FIELD) <= sizeof (long int))		      \
+	elf_ar_hdr->FIELD						      \
+	  = (__typeof (elf_ar_hdr->FIELD)) strtol (string, NULL, 8);	      \
+      else								      \
+	elf_ar_hdr->FIELD						      \
+	  = (__typeof (elf_ar_hdr->FIELD)) strtoll (string, NULL, 8);	      \
+    }									      \
+  while (0)
+
   INT_FIELD (ar_date);
   INT_FIELD (ar_uid);
   INT_FIELD (ar_gid);
-  INT_FIELD (ar_mode);
+  OCT_FIELD (ar_mode);
   INT_FIELD (ar_size);
 
   if (elf_ar_hdr->ar_size < 0)
-- 
2.30.2


                 reply	other threads:[~2022-08-28 18:11 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=20220828181034.115031-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).