[PATCH v3 05/26] readelf: last tag as empty string triggers corrupt string data incorrectly

Matthieu Longo matthieu.longo@arm.com
Fri May 9 15:12:55 GMT 2025
---
 binutils/readelf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index dd1871d8c75..88edf9246cc 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -17779,12 +17779,14 @@ display_tag_value (signed int tag,
   else if (tag & 1)
     {
       /* PR 17531 file: 027-19978-0.004.  */
-      size_t maxlen = (end - p) - 1;
+      size_t maxlen = end - p;
 
       putchar ('"');
       if (maxlen > 0)
 	{
-	  print_symbol_name ((int) maxlen, (const char *) p);
+	  maxlen -= 1; /* Remove \0 from the character count.  */
+	  if (maxlen > 0) /* Don't try to print an empty string.  */
+	    print_symbol_name ((int) maxlen, (const char *) p);
 	  p += strnlen ((char *) p, maxlen) + 1;
 	}
       else
-- 
2.49.0



More information about the Binutils mailing list