From patchwork Sun Aug 28 15:57:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 57122 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2EC4E385800A for ; Sun, 28 Aug 2022 15:58:23 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id AED9B3858419 for ; Sun, 28 Aug 2022 15:58:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AED9B3858419 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x16.wildebeest.org [172.31.17.152]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 58E2B302FB88; Sun, 28 Aug 2022 17:58:08 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 233162E82344; Sun, 28 Aug 2022 17:58:07 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] ar: Correct -N COUNT off-by-one Date: Sun, 28 Aug 2022 17:57:46 +0200 Message-Id: <20220828155746.99653-1-mark@klomp.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: , Cc: Mark Wielaard , panxiaohe Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" When using instance [COUNT], the instance check is wrong. instance-- == 0 should be --instance == 0. Add a testcase run-ar-N.sh that uses -N COUNT with extract and delete operations checking the right instance was extracted and deleted. https://sourceware.org/bugzilla/show_bug.cgi?id=28725 Reported-by: panxiaohe Signed-off-by: Mark Wielaard --- src/ChangeLog | 6 +++++ src/ar.c | 4 +-- tests/ChangeLog | 6 +++++ tests/Makefile.am | 2 ++ tests/run-ar-N.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100755 tests/run-ar-N.sh diff --git a/src/ChangeLog b/src/ChangeLog index 88db4051..9348c562 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2022-08-28 Mark Wielaard + + * ar.c (do_oper_extract): Predecrement instance before compare + to zero. + (do_oper_delete): Likewise. + 2022-08-10 Andreas Schwab * readelf.c (print_attributes): Also handle SHT_RISCV_ATTRIBUTES. diff --git a/src/ar.c b/src/ar.c index 9e8df120..04456c18 100644 --- a/src/ar.c +++ b/src/ar.c @@ -518,7 +518,7 @@ do_oper_extract (int oper, const char *arfname, char **argv, int argc, ENTRY entry; entry.key = arhdr->ar_name; ENTRY *res = hsearch (entry, FIND); - if (res != NULL && (instance < 0 || instance-- == 0) + if (res != NULL && (instance < 0 || --instance == 0) && !found[(char **) res->data - argv]) found[(char **) res->data - argv] = do_extract = true; } @@ -952,7 +952,7 @@ do_oper_delete (const char *arfname, char **argv, int argc, ENTRY entry; entry.key = arhdr->ar_name; ENTRY *res = hsearch (entry, FIND); - if (res != NULL && (instance < 0 || instance-- == 0) + if (res != NULL && (instance < 0 || --instance == 0) && !found[(char **) res->data - argv]) found[(char **) res->data - argv] = do_delete = true; } diff --git a/tests/ChangeLog b/tests/ChangeLog index d2952cc9..48bb28d1 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,9 @@ +2022-08-26 Mark Wielaard + + * run-ar-N.sh: New test. + * Makefile.am (TESTS): Add run-ar-N.sh. + (EXTRA_DIST): Likewise. + 2022-08-04 Sergei Trofimovich * low_high_pc.c (handle_die): Drop redundant 'lx' suffix. diff --git a/tests/Makefile.am b/tests/Makefile.am index 87988fb9..85514898 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -100,6 +100,7 @@ test-nlist$(EXEEXT): test-nlist.c $(test_nlist_CFLAGS) $(GCOV_FLAGS) -o $@ $< $(test_nlist_LDADD) TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \ + run-ar-N.sh \ update1 update2 update3 update4 \ run-show-die-info.sh run-get-files.sh run-get-lines.sh \ run-next-files.sh run-next-lines.sh \ @@ -254,6 +255,7 @@ endif endif EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \ + run-ar-N.sh \ run-show-die-info.sh run-get-files.sh run-get-lines.sh \ run-next-files.sh run-next-lines.sh testfile-only-debug-line.bz2 \ run-get-pubnames.sh run-get-aranges.sh \ diff --git a/tests/run-ar-N.sh b/tests/run-ar-N.sh new file mode 100755 index 00000000..de8f62b3 --- /dev/null +++ b/tests/run-ar-N.sh @@ -0,0 +1,65 @@ +#! /usr/bin/env bash +# Copyright (C) 2022 Mark J. Wielaard +# This file is part of elfutils. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# elfutils is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. $srcdir/test-subr.sh + +tempfiles testfile test.ar + +echo create test.ar with 3 testfile +echo 1 > testfile +testrun ${abs_top_builddir}/src/ar -vr test.ar testfile +echo 2 > testfile +testrun ${abs_top_builddir}/src/ar -vq test.ar testfile +testrun ${abs_top_builddir}/src/ar -t test.ar +echo 3 > testfile +testrun ${abs_top_builddir}/src/ar -vq test.ar testfile +testrun_compare ${abs_top_builddir}/src/ar -t test.ar << EOF +testfile +testfile +testfile +EOF + +echo list content of testfile 1 2 3 +testrun ${abs_top_builddir}/src/ar -vx -N 1 test.ar testfile +diff -u testfile - << EOF +1 +EOF +testrun ${abs_top_builddir}/src/ar -vx -N 2 test.ar testfile +diff -u testfile - << EOF +2 +EOF +testrun ${abs_top_builddir}/src/ar -vx -N 3 test.ar testfile +diff -u testfile - << EOF +3 +EOF + +echo delete testfile 2 +testrun ${abs_top_builddir}/src/ar -vd -N 2 test.ar testfile +testrun_compare ${abs_top_builddir}/src/ar -t test.ar << EOF +testfile +testfile +EOF +testrun ${abs_top_builddir}/src/ar -vx -N 1 test.ar testfile +diff -u testfile - << EOF +1 +EOF +testrun ${abs_top_builddir}/src/ar -vx -N 2 test.ar testfile +diff -u testfile - << EOF +3 +EOF + +exit 0