bug-bash
[Top][All Lists]
Advanced

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

caller builtin returns wrong lineno when sourced


From: Hugo Mildenberger
Subject: caller builtin returns wrong lineno when sourced
Date: Mon, 23 Nov 2009 13:24:56 +0100
User-agent: KMail/1.12.2 (Linux/2.6.31.6-grsec; KDE/4.3.2; i686; ; )

Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: i686-pc-linux-gnu-gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' -
DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' -
DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -
DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib  -
DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
 
-DSTANDARD_UTILS_PATH='/bin:/usr/bin:/sbin:/usr/sbin' -
DSYS_BASHRC='/etc/bash/bashrc' -DSYS_BASH_LOGOUT='/etc/bash/bash_logout' -
DNON_INTERACTIVE_LOGIN_SHELLS -DSSH_SOURCE_BASHRC -march=pentium4 -g -O2 -pipe
uname output: Linux localhost 2.6.31.6-grsec #1 Fri Nov 13 21:21:04 CET 2009 
i686 Intel(R) Pentium(R) 4 CPU 2.80GHz GenuineIntel GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 4.0
Patch Level: 33
Release Status: release

Description:
        The bash builtin function "caller" outputs wrong line numbers if the
        script was sourced within the current shell. This may have to do with
        subshells, but the output is correct if a newly spawned shell 
        runs the script. The contents of "bash-caller-bug.sh" documented
        below.

        $ bash bash-caller-bug.sh       
        calling 'func2' in a subshell from line 33 
        this is 'func1' at line 25 -- so where we end?
        Bash backtrace:
                25 func1 ./bash-caller-bug.sh
                29 func2 ./bash-caller-bug.sh
                33 main ./bash-caller-bug.sh

        $ source bash-caller-bug.sh     
        calling 'func2' in a subshell from line 33 
        this is 'func1' at line 1 -- so where we end?
        Bash backtrace:
                1 func1 ./bash-caller-bug.sh
                -27 func2 ./bash-caller-bug.sh
                33 source ./bash-caller-bug.sh


Repeat-By:
        #!/bin/bash -e

        # besides kill, there is no reliable way to terminate a script 
        # from a subshell [= anything running between (), i.e. $()].
        die() {
                local -i n=0
                echo -n "$1" >&2
                [[ "$2" != "0" ]] \
                        && echo "( function returned error "$2" )" >&2 \
                        || echo >&2;
                echo "Bash backtrace:" >&2
                while ( [[ true ]]  ); do
                        local frame=$(caller $n)
                        local msg="\t$frame"
                        [[ $frame ]] && echo -e "$msg" >&2 || break;
                        n=$((++n))
                done
                
        #       kill $$;
                exit 1; # don't exec subsequent statements from subshell
        }


        func1() {
                die "this is '${FUNCNAME}' at line ${LINENO} -- so where we 
end?" 0 
        }

        func2() {
                echo "$(func1)";
        }


        echo "calling 'func2' in a subshell from line ${LINENO} $(func2)";
        # END OF FILE






reply via email to

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