public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: Jan-Benedict Glaw <jbglaw@lug-owl.de>
Cc: elfutils-devel@sourceware.org
Subject: Re: [PATCH] Tests: Fix warning in show-die-info.c
Date: Wed, 6 Oct 2021 23:44:22 +0200	[thread overview]
Message-ID: <YV4YtovT/5Uscsl6@wildebeest.org> (raw)
In-Reply-To: <20211005153216.fadk42msbpz4xvx5@lug-owl.de>

[-- Attachment #1: Type: text/plain, Size: 1621 bytes --]

Hi,

On Tue, Oct 05, 2021 at 05:32:16PM +0200, Jan-Benedict Glaw wrote:
> I'm running automated test compiles on Binutils, GCC, Linux, NetBSD
> and, since a few days ago, elfutils.
> 
> Building/running the tests, I noticed this little warning:
> 
> [make 2021-10-01 12:18:15] elflint.c: In function 'check_sections':
> [make 2021-10-01 12:18:15] elflint.c:4105:48: error: null pointer dereference [-Werror=null-dereference]
> [make 2021-10-01 12:18:15]  4105 |                                  idx < databits->d_size && ! bad;
> [make 2021-10-01 12:18:15]       |                                        ~~~~~~~~^~~~~~~~
> [make 2021-10-01 12:18:18] cc1: all warnings being treated as errors
> [make 2021-10-01 12:18:18] make[2]: *** [Makefile:799: elflint.o] Error 1
> [make 2021-10-01 12:18:18] make[1]: *** [Makefile:532: all-recursive] Error 1
> [make 2021-10-01 12:18:18] make: *** [Makefile:448: all] Error 2
> 
> 
> As it is tested beforehand that we should not run into this, this
> patch should fix the warning:
> 
> 
> diff --git a/src/elflint.c b/src/elflint.c
> index 1ce75684..ef7725ce 100644
> --- a/src/elflint.c
> +++ b/src/elflint.c
> @@ -4102,7 +4102,7 @@ section [%2zu] '%s' has type NOBITS but is read from the file in segment of prog
>  			    bad = (databits == NULL
>  				   || databits->d_size != shdr->sh_size);
>  			    for (size_t idx = 0;
> -				 idx < databits->d_size && ! bad;
> +				 ! bad && idx < databits->d_size;
>  				 idx++)
>  			      bad = ((char *) databits->d_buf)[idx] != 0;
>  

Thanks, that warning and the fix look correct.
I committed the attached fix.

Cheers,

Mark



[-- Attachment #2: 0001-elflint.c-Don-t-dereference-databits-if-bad.patch --]
[-- Type: text/x-diff, Size: 1520 bytes --]

From 3d9f12883d0c131bd4ab6045e1f60d3fe6d150ea Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mark@klomp.org>
Date: Wed, 6 Oct 2021 23:37:42 +0200
Subject: [PATCH] elflint.c: Don't dereference databits if bad

elflint.c: In function 'check_sections':
elflint.c:4105:48: error: null pointer dereference [-Werror=null-dereference]
4105 |                                  idx < databits->d_size && ! bad;
     |                                        ~~~~~~~~^~~~~~~~

Fix this by testing for ! bad first.

Reported-by: Jan-Benedict Glaw <jbglaw@lug-owl.de>
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog | 4 ++++
 src/elflint.c | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 87b3dd46..316bcb6d 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2021-10-06  Mark Wielaard  <mark@klomp.org>
+
+	* elflint.c (check_sections): Don't dereference databits if bad.
+
 2021-09-09  Dmitry V. Levin  <ldv@altlinux.org>
 
 	* findtextrel.c: Include "libeu.h".
diff --git a/src/elflint.c b/src/elflint.c
index 1ce75684..ef7725ce 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -4102,7 +4102,7 @@ section [%2zu] '%s' has type NOBITS but is read from the file in segment of prog
 			    bad = (databits == NULL
 				   || databits->d_size != shdr->sh_size);
 			    for (size_t idx = 0;
-				 idx < databits->d_size && ! bad;
+				 ! bad && idx < databits->d_size;
 				 idx++)
 			      bad = ((char *) databits->d_buf)[idx] != 0;
 
-- 
2.32.0


  reply	other threads:[~2021-10-06 21:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-05 15:32 Jan-Benedict Glaw
2021-10-06 21:44 ` Mark Wielaard [this message]
2021-10-05 15:36 Jan-Benedict Glaw
2021-10-06 21:55 ` Mark Wielaard

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=YV4YtovT/5Uscsl6@wildebeest.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    --cc=jbglaw@lug-owl.de \
    /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).