public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: David Malcolm <dmalcolm@redhat.com>, Mark Wielaard <mark@klomp.org>
Subject: [PATCH 2/7] tests: Make sure to not call memcmp with NULL arguments.
Date: Sun, 10 May 2020 21:53:35 +0200	[thread overview]
Message-ID: <20200510195339.37191-3-mark@klomp.org> (raw)
In-Reply-To: <20200510195339.37191-1-mark@klomp.org>

GCC10 -fanalyzer thinks we are too clever:

elfputzdata.c: In function ‘main’:
elfputzdata.c:178:8: warning: use of possibly-NULL ‘orig_buf’ where
                     non-null expected [CWE-690]
                     [-Wanalyzer-possible-null-argument]
  178 |     && memcmp (orig_buf, d->d_buf, orig_size) == 0)

orig_buf can only be NULL when orig_size is zero, but it might still
be undefined behaviour. So don't try to be too smart and just check
whether we actually have an buffer.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 tests/ChangeLog     |  5 +++++
 tests/elfputzdata.c | 21 +++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index 301b0fb6..083e138d 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,8 @@
+2020-05-08  Mark Wielaard  <mark@klomp.org>
+
+	* elfputzdata.c (main): Explicitly check orig_buf is not NULL
+	before calling memcmp.
+
 2020-04-17  Mark Wielaard  <mark@klomp.org>
 
 	* test-subr.sh (testrun_on_self_obj): New function.
diff --git a/tests/elfputzdata.c b/tests/elfputzdata.c
index 66ab77ba..0d9c020e 100644
--- a/tests/elfputzdata.c
+++ b/tests/elfputzdata.c
@@ -105,14 +105,17 @@ main (int argc, char *argv[])
 		  printf ("Unexpected data size for orig section %zd\n", idx);
 		  return -1;
 		}
-	      char *orig_buf = malloc (d->d_size);
-	      if (orig_size > 0 && orig_buf == NULL)
+	      char *orig_buf = NULL;
+	      if (orig_size > 0)
 		{
-		  printf ("No memory to copy section %zd data\n", idx);
-		  return -1;
+		  orig_buf = malloc (d->d_size);
+		  if (orig_buf == NULL)
+		    {
+		      printf ("No memory to copy section %zd data\n", idx);
+		      return -1;
+		    }
+		  memcpy (orig_buf, d->d_buf, orig_size);
 		}
-	      if (orig_size > 0)
-		memcpy (orig_buf, d->d_buf, orig_size);
 
 	      bool forced = false;
 	      if (gnu)
@@ -175,7 +178,8 @@ main (int argc, char *argv[])
 		}
 
 	      if (new_size == orig_size
-		  && memcmp (orig_buf, d->d_buf, orig_size) == 0)
+		  && (orig_buf == NULL
+		      || memcmp (orig_buf, d->d_buf, orig_size) == 0))
 		{
 		  printf ("section %zd didn't compress\n", idx);
 		  return -1;
@@ -211,7 +215,8 @@ main (int argc, char *argv[])
 		  return -1;
 		}
 	      if (newer_size != orig_size
-		  && memcmp (orig_buf, d->d_buf, orig_size) != 0)
+		  && (orig_buf == NULL
+		      || memcmp (orig_buf, d->d_buf, orig_size) != 0))
 		{
 		  printf ("section %zd didn't correctly uncompress\n", idx);
 		  return -1;
-- 
2.20.1


  parent reply	other threads:[~2020-05-10 19:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-10 19:53 Some more GCC10 -fanalyzer inspired patches Mark Wielaard
2020-05-10 19:53 ` [PATCH 1/7] libdwfl: Cleanup user_core resources on failure in dwfl_core_file_report Mark Wielaard
2020-05-10 19:53 ` Mark Wielaard [this message]
2020-05-10 19:53 ` [PATCH 3/7] libelf: Check __gelf_getehdr_rdlock call doesn't fail in elf_getdata Mark Wielaard
2020-05-10 19:53 ` [PATCH 4/7] libelf: Check for NULL shdr in elf_strptr Mark Wielaard
2020-05-10 19:53 ` [PATCH 5/7] src: Check ebl_openbackend result before using ebl handle Mark Wielaard
2020-05-10 19:53 ` [PATCH 6/7] libdwfl: Return failure from dwfl_standard_find_debuginfo for NULL module Mark Wielaard
2020-05-10 19:53 ` [PATCH 7/7] libcpu: Free unused new bitfield on error in i386_parse.y new_bitfield Mark Wielaard
2020-05-14 12:44 ` Some more GCC10 -fanalyzer inspired patches 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=20200510195339.37191-3-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=dmalcolm@redhat.com \
    --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).