bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bugs in ERR and RETURN traps


From: Chet Ramey
Subject: Re: Bugs in ERR and RETURN traps
Date: Fri, 27 Feb 2015 09:38:41 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

On 2/19/15 2:54 PM, Paul Donohue wrote:

> When an ERR trap is called, ${BASH_SOURCE[0]}, ${FUNCNAME[0]}, and $LINENO 
> normally indicate the file/function/line which called the command that 
> returned a non-zero status. This is good.
> 
> However, if an ERR trap is called because a function returned a non-zero 
> status, then ${BASH_SOURCE[0]} and ${FUNCNAME[0]} still indicate the 
> file/function which called the function that returned a non-zero status, but 
> $LINENO indicates the line number of the last command in the function that 
> was called, and the line number of the call to the function is not available. 
>  This doesn't really make sense.
> 
> Example:
> $ cat <<'END' >script1
> test_fun() {
> return 1
> }
> END
> $ cat <<'END' >script2
> #!/bin/bash
> # Some blank lines to adjust the line numbers relative to script1
> 
> source script1
> trap 'echo error at: ${BASH_SOURCE[0]} ${FUNCNAME[0]} $LINENO 
> ${BASH_LINENO[@]}' ERR
> false # Trigger the ERR trap with a command on line 6
> test_fun # Trigger the ERR trap with a function on line 7
> END
> $ chmod 755 script2
> $ ./script2
> error at: ./script2 main 6 0
> error at: ./script2 main 2 0
> $
> 
> This seems like a bug. I would expect one of two behaviors here:
> 1) In the ERR trap on function return, $LINENO should point to the line which 
> called the function (just as ${BASH_SOURCE[0]} and ${FUNCNAME[0]} point to 
> the file/function which called the function that returned a non-zero status).
> 2) In the ERR trap on function return, ${BASH_SOURCE[0]}, ${FUNCNAME[0]}, and 
> $LINENO should point to the last command executed within the function, and 
> ${BASH_SOURCE[1]} ${FUNCNAME[1]} ${BASH_LINENO[0]} should point to the caller 
> of the function.

Thanks for the report.  I agree that this is a bug, and that your first
expected behavior is the correct one.  Function execution should save and
restore the value of LINENO that will be used in an ERR trap.  This will
be fixed in the next release.  In the meantime, I have attached a patch
you can experiment with.  It may not apply cleanly to bash-4.3, and your
line numbers will certainly vary.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/

Attachment: func-unwind-protect-lineno.patch
Description: Source code patch


reply via email to

[Prev in Thread] Current Thread [Next in Thread]