public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Tests: Fix warning in show-die-info.c
@ 2021-10-05 15:32 Jan-Benedict Glaw
  2021-10-06 21:44 ` Mark Wielaard
  0 siblings, 1 reply; 4+ messages in thread
From: Jan-Benedict Glaw @ 2021-10-05 15:32 UTC (permalink / raw)
  To: elfutils-devel

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

Hi!

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:

.../configure --enable-maintainer-mode --enable-tests-rpath --enable-gprof --enable-gcov --enable-valgrind --prefix=/tmp/elfutils

make make V=1

[make 2021-10-01 12:18:08] /usr/lib/gcc-snapshot/bin/gcc -std=gnu99 -Wall -Wshadow -Wformat=2 -Wold-style-definition -Wstrict-prototypes -Wtrampolines -Wlogical-op -Wduplicated-cond -Wnull-dereference -Wimplicit-fallthrough=5 -Werror -Wunused -Wextra -Wstack-usage=262144   -D_FORTIFY_SOURCE=2 -g -O2 -pg -fprofile-arcs -ftest-coverage -Wl,-rpath-link,../libelf:../libdw -Wno-error=stack-usage= -pg -fprofile-arcs -o strip strip.o ../libebl/libebl.a ../backends/libebl_backends.a ../libcpu/libcpu.a ../libelf/libelf.a -lz ../libdw/libdw.a -lz -lzstd -llzma -lbz2  ../libelf/libelf.a -lz -ldl -lpthread ../lib/libeu.a  
[make 2021-10-01 12:18:08] /usr/lib/gcc-snapshot/bin/gcc -D_GNU_SOURCE -DHAVE_CONFIG_H -DLOCALEDIR='"/tmp/elfutils/share/locale"'  -DDEBUGPRED=0 -DSRCDIR=\"/var/lib/laminar/run/elfutils/11/elfutils/src\" -DOBJDIR=\"/var/lib/laminar/run/elfutils/11/elfutils/src\" -I. -I..  -I. -I. -I../lib -I.. -I./../libelf -I./../libebl -I./../libdw -I./../libdwelf -I./../libdwfl -I./../libasm  -std=gnu99 -Wall -Wshadow -Wformat=2 -Wold-style-definition -Wstrict-prototypes -Wtrampolines -Wlogical-op -Wduplicated-cond -Wnull-dereference -Wimplicit-fallthrough=5 -Werror -Wunused -Wextra    -D_FORTIFY_SOURCE=2 -g -O2 -pg -fprofile-arcs -ftest-coverage -MT elflint.o -MD -MP -MF .deps/elflint.Tpo -c -o elflint.o elflint.c
[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;
 


Please keep me Cc'ed as I'm not subscribed.

Thanks,
  Jan-Benedict

-- 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

* Re: [PATCH] Tests: Fix warning in show-die-info.c
  2021-10-05 15:32 [PATCH] Tests: Fix warning in show-die-info.c Jan-Benedict Glaw
@ 2021-10-06 21:44 ` Mark Wielaard
  0 siblings, 0 replies; 4+ messages in thread
From: Mark Wielaard @ 2021-10-06 21:44 UTC (permalink / raw)
  To: Jan-Benedict Glaw; +Cc: elfutils-devel

[-- 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


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

* Re: [PATCH] Tests: Fix warning in show-die-info.c
  2021-10-05 15:36 Jan-Benedict Glaw
@ 2021-10-06 21:55 ` Mark Wielaard
  0 siblings, 0 replies; 4+ messages in thread
From: Mark Wielaard @ 2021-10-06 21:55 UTC (permalink / raw)
  To: Jan-Benedict Glaw; +Cc: elfutils-devel

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

Hi Jan-Benedict,

On Tue, Oct 05, 2021 at 05:36:40PM +0200, Jan-Benedict Glaw wrote:
> 
> My last email had a wrong subject, though the patch was correct.
> Here's a second patch, this time *actally* for tests/show-die-info.c:
> 
> diff --git a/tests/show-die-info.c b/tests/show-die-info.c
> index 34e27a3b..0823cc60 100644
> --- a/tests/show-die-info.c
> +++ b/tests/show-die-info.c
> @@ -97,7 +97,7 @@ handle (Dwarf *dbg, Dwarf_Die *die, int n)
>    printf ("%*s Attrs     :", n * 5, "");
>    for (cnt = 0; cnt < 0xffff; ++cnt)
>      if (dwarf_hasattr (die, cnt))
> -      printf (" %s", dwarf_attr_string (cnt));
> +      printf (" %s", (dwarf_attr_string (cnt)? dwarf_attr_string (cnt): ""));
>    puts ("");
>  
>    if (dwarf_hasattr (die, DW_AT_low_pc) && dwarf_lowpc (die, &addr) == 0)

This can be fixed in a shorter way using dwarf_attr_string ?: "<unknown>".
Which is what I pushed (see attached).

Thanks,

Mark



[-- Attachment #2: 0001-tests-Handle-dwarf_attr_string-returning-NULL-in-sho.patch --]
[-- Type: text/x-diff, Size: 1292 bytes --]

From 47b0ebe9033daa7ac9c732b25c85520b97f9635a Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mark@klomp.org>
Date: Wed, 6 Oct 2021 23:53:34 +0200
Subject: [PATCH] tests: Handle dwarf_attr_string returning NULL in
 show-die-info.c

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

diff --git a/tests/ChangeLog b/tests/ChangeLog
index d289b27c..07e018b0 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,7 @@
+2021-10-06  Mark Wielaard  <mark@klomp.org>
+
+	* show-die-info.c (handle): Handle dwarf_attr_string returning NULL.
+
 2021-10-06  Di Chen <dichen@redhat.com>
 
 	PR28242
diff --git a/tests/show-die-info.c b/tests/show-die-info.c
index 34e27a3b..1a3191cd 100644
--- a/tests/show-die-info.c
+++ b/tests/show-die-info.c
@@ -97,7 +97,7 @@ handle (Dwarf *dbg, Dwarf_Die *die, int n)
   printf ("%*s Attrs     :", n * 5, "");
   for (cnt = 0; cnt < 0xffff; ++cnt)
     if (dwarf_hasattr (die, cnt))
-      printf (" %s", dwarf_attr_string (cnt));
+      printf (" %s", dwarf_attr_string (cnt) ?: "<unknown>");
   puts ("");
 
   if (dwarf_hasattr (die, DW_AT_low_pc) && dwarf_lowpc (die, &addr) == 0)
-- 
2.32.0


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

* [PATCH] Tests: Fix warning in show-die-info.c
@ 2021-10-05 15:36 Jan-Benedict Glaw
  2021-10-06 21:55 ` Mark Wielaard
  0 siblings, 1 reply; 4+ messages in thread
From: Jan-Benedict Glaw @ 2021-10-05 15:36 UTC (permalink / raw)
  To: elfutils-devel

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

Hi!

My last email had a wrong subject, though the patch was correct.
Here's a second patch, this time *actally* for tests/show-die-info.c:



diff --git a/tests/show-die-info.c b/tests/show-die-info.c
index 34e27a3b..0823cc60 100644
--- a/tests/show-die-info.c
+++ b/tests/show-die-info.c
@@ -97,7 +97,7 @@ handle (Dwarf *dbg, Dwarf_Die *die, int n)
   printf ("%*s Attrs     :", n * 5, "");
   for (cnt = 0; cnt < 0xffff; ++cnt)
     if (dwarf_hasattr (die, cnt))
-      printf (" %s", dwarf_attr_string (cnt));
+      printf (" %s", (dwarf_attr_string (cnt)? dwarf_attr_string (cnt): ""));
   puts ("");
 
   if (dwarf_hasattr (die, DW_AT_low_pc) && dwarf_lowpc (die, &addr) == 0)


Please keep me Cc'ed as I'm not subscribed!

Thanks,
  Jan-Benedict
-- 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

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

end of thread, other threads:[~2021-10-06 21:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-05 15:32 [PATCH] Tests: Fix warning in show-die-info.c Jan-Benedict Glaw
2021-10-06 21:44 ` Mark Wielaard
2021-10-05 15:36 Jan-Benedict Glaw
2021-10-06 21:55 ` 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).