Bug 28725 - eu-ar -x -N COUNT uses instance [COUNT+1] of name
Summary: eu-ar -x -N COUNT uses instance [COUNT+1] of name
Status: RESOLVED FIXED
Alias: None
Product: elfutils
Classification: Unclassified
Component: tools (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Mark Wielaard
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-12-25 12:19 UTC by panxiaohe
Modified: 2022-09-14 19:00 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2022-08-28 00:00:00


Attachments
the result of eu-ar -x -N COUNT (88.23 KB, image/jpeg)
2021-12-25 12:19 UTC, panxiaohe
Details
the result of ar -x -N COUNT (77.49 KB, image/jpeg)
2021-12-25 12:20 UTC, panxiaohe
Details

Note You need to log in before you can comment on or make changes to this bug.
Description panxiaohe 2021-12-25 12:19:49 UTC
Created attachment 13880 [details]
the result of eu-ar -x -N COUNT

eu-ar -x -N COUNT should use instance [COUNT] of name, 
but it uses instance [COUNT+1] of name incorrectly.
Usage: -N  Use instance [COUNT] of name.

The attachments show the differences of command result between "eu-ar -x -N COUNT"
and "ar -x -N COUNT".

do_oper_extract() in src/ar.c
Is it should be instance-- at Line522?
 522           if (res != NULL && (instance < 0 || instance-- == 0)
 523               && !found[(char **) res->data - argv])
 524             found[(char **) res->data - argv] = do_extract = true;
 525         }
Comment 1 panxiaohe 2021-12-25 12:20:32 UTC
Created attachment 13881 [details]
the result of ar -x -N COUNT
Comment 2 panxiaohe 2022-08-04 00:05:15 UTC
Please pay attention on this issue.
When using instance [COUNT] of name, the instance of check is wrong in do_oper_extract().
instance-- should be --instance.
 522           if (res != NULL && (instance < 0 || instance-- == 0)
 523               && !found[(char **) res->data - argv])
 524             found[(char **) res->data - argv] = do_extract = true;
 525         }
Comment 3 Mark Wielaard 2022-08-28 15:59:36 UTC
Thanks for the report and analysis.
Next time please post output as text instead of an image.
I posted a patch that includes a testcase:
https://inbox.sourceware.org/elfutils-devel/20220828155746.99653-1-mark@klomp.org/T/#u
Comment 4 Mark Wielaard 2022-09-14 19:00:38 UTC
commit 6cb01b4ef3cdd4f83d21bbcec72fa66712ac14e6
Author: Mark Wielaard <mark@klomp.org>
Date:   Sun Aug 28 17:46:08 2022 +0200

    ar: Correct -N COUNT off-by-one
    
    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 <panxh_ran@163.com>
    Signed-off-by: Mark Wielaard <mark@klomp.org>