public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] readelf: Workaround stringop-truncation error
@ 2021-12-04 21:15 Mark Wielaard
  2021-12-08 23:14 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Wielaard @ 2021-12-04 21:15 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

In function ‘strncpy’,
    inlined from ‘print_ehdr’ at readelf.c:1175:4:
error: ‘__builtin_strncpy’ specified bound 512 equals destination size
       [-Werror=stringop-truncation]

strncpy doesn't terminate the copied string if there is not enough
room. We compensate later by explicitly adding a zero terminator at
buf[sizeof (buf) - 1]. Normally gcc does see this, but with
-fsanitize=address there is too much (checking) code in between. But
it is actually better to not let strncpy do too much work, so
substract one from the size.

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

diff --git a/src/ChangeLog b/src/ChangeLog
index 05b2522d..263e9faa 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2021-12-04  Mark Wielaard  <mark@klomp.org>
+
+	* readelf.c (print_ehdr): Pass sizeof (buf) - 1 to strncpy.
+
 2021-10-20  John M Mellor-Crummey  <johnmc@rice.edu>
 
 	* readelf.c (print_debug_line_section): Try to read
diff --git a/src/readelf.c b/src/readelf.c
index c10038e3..93fb5989 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -1172,7 +1172,7 @@ print_ehdr (Ebl *ebl, GElf_Ehdr *ehdr)
 		  (uint32_t) shdr->sh_link);
       else
 	{
-	  strncpy (buf, _(" ([0] not available)"), sizeof (buf));
+	  strncpy (buf, _(" ([0] not available)"), sizeof (buf) - 1);
 	  buf[sizeof (buf) - 1] = '\0';
 	}
 
-- 
2.30.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] readelf: Workaround stringop-truncation error
  2021-12-04 21:15 [PATCH] readelf: Workaround stringop-truncation error Mark Wielaard
@ 2021-12-08 23:14 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2021-12-08 23:14 UTC (permalink / raw)
  To: elfutils-devel

On Sat, Dec 04, 2021 at 10:15:04PM +0100, Mark Wielaard wrote:
> In function ‘strncpy’,
>     inlined from ‘print_ehdr’ at readelf.c:1175:4:
> error: ‘__builtin_strncpy’ specified bound 512 equals destination size
>        [-Werror=stringop-truncation]
> 
> strncpy doesn't terminate the copied string if there is not enough
> room. We compensate later by explicitly adding a zero terminator at
> buf[sizeof (buf) - 1]. Normally gcc does see this, but with
> -fsanitize=address there is too much (checking) code in between. But
> it is actually better to not let strncpy do too much work, so
> substract one from the size.

Pushed,

Mark

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-08 23:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-04 21:15 [PATCH] readelf: Workaround stringop-truncation error Mark Wielaard
2021-12-08 23:14 ` Mark Wielaard

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).