Bug 252727 - readelf returns 0 (success) even on error
Summary: readelf returns 0 (success) even on error
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Ed Maste
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-01-16 01:19 UTC by Ed Maste
Modified: 2021-04-23 15:39 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ed Maste freebsd_committer freebsd_triage 2021-01-16 01:19:08 UTC
$ readelf -l /dev/null;  echo $?
readelf: elf_begin() failed: Invalid argument
0

This prevents error handling in shell scripts.
Comment 1 Ed Maste freebsd_committer freebsd_triage 2021-01-16 01:30:19 UTC
Experimenting with GNU readelf, I see:
- rv = 1 if one or more files failed, regardless of where in the argument list it appears
- files specified after a failure are still processed
- specifying a non-ELF file results in failure (/dev/null, shell script, etc.)
- unsure of behaviour for a malformed ELF file
Comment 2 commit-hook freebsd_committer freebsd_triage 2021-04-05 01:11:18 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ea444392bb5b351c930f28a02a4e68f51b25ba69

commit ea444392bb5b351c930f28a02a4e68f51b25ba69
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2021-04-05 01:01:28 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-04-05 01:01:28 +0000

    readelf: return error in case of invalid file

    GNU readelf exits with an error for a number of invalid file cases.
    Previously ELF Tool Chain readelf always exited with 0.  Now we exit 1
    upon detecting an error with one or more input files, but in any case
    all of them are processed.

    This should catch common failure cases.  We still do not report an error
    for some types of malformed ELF files, but this is consistent with GNU
    readelf.

    PR:             252727
    Reviewed by:    jkoshy, markj
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D29377

 contrib/elftoolchain/readelf/readelf.c | 36 +++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 14 deletions(-)
Comment 3 commit-hook freebsd_committer freebsd_triage 2021-04-13 18:47:11 UTC
A commit in branch stable/12 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=14b0c8e7687e0f5917b4d272bd16a6816cab3d12

commit 14b0c8e7687e0f5917b4d272bd16a6816cab3d12
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2021-04-05 01:01:28 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-04-13 18:44:59 +0000

    readelf: return error in case of invalid file

    GNU readelf exits with an error for a number of invalid file cases.
    Previously ELF Tool Chain readelf always exited with 0.  Now we exit 1
    upon detecting an error with one or more input files, but in any case
    all of them are processed.

    This should catch common failure cases.  We still do not report an error
    for some types of malformed ELF files, but this is consistent with GNU
    readelf.

    PR:             252727
    Reviewed by:    jkoshy, markj
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D29377

    (cherry picked from commit ea444392bb5b351c930f28a02a4e68f51b25ba69)

 contrib/elftoolchain/readelf/readelf.c | 36 +++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 14 deletions(-)
Comment 4 commit-hook freebsd_committer freebsd_triage 2021-04-23 15:39:28 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=f4c3c0189f7ddc324643c843097247f6df316ec5

commit f4c3c0189f7ddc324643c843097247f6df316ec5
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2021-04-23 15:37:49 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2021-04-23 15:37:49 +0000

    readelf: return error in case of invalid file

    GNU readelf exits with an error for a number of invalid file cases.
    Previously ELF Tool Chain readelf always exited with 0.  Now we exit 1
    upon detecting an error with one or more input files, but in any case
    all of them are processed.

    This should catch common failure cases.  We still do not report an error
    for some types of malformed ELF files, but this is consistent with GNU
    readelf.

    PR:             252727
    Reviewed by:    jkoshy, markj
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D29377

    (cherry picked from commit ea444392bb5b351c930f28a02a4e68f51b25ba69)

 contrib/elftoolchain/readelf/readelf.c | 36 +++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 14 deletions(-)