From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id CB569383E825 for ; Sun, 10 May 2020 19:58:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CB569383E825 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mark@klomp.org Received: from librem (deer0x15.wildebeest.org [172.31.17.151]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 8C426300B2FD; Sun, 10 May 2020 21:58:11 +0200 (CEST) Received: by librem (Postfix, from userid 1000) id 643C1C031D; Sun, 10 May 2020 21:57:46 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: David Malcolm , Mark Wielaard Subject: [PATCH 2/7] tests: Make sure to not call memcmp with NULL arguments. Date: Sun, 10 May 2020 21:53:35 +0200 Message-Id: <20200510195339.37191-3-mark@klomp.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200510195339.37191-1-mark@klomp.org> References: <20200510195339.37191-1-mark@klomp.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.7 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 May 2020 19:58:15 -0000 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 --- 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 + + * elfputzdata.c (main): Explicitly check orig_buf is not NULL + before calling memcmp. + 2020-04-17 Mark Wielaard * 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