Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Bug report: segfault when function and alias names collide



On Tue, 13 Jul 2010 10:34:11 +1000
Ben Hoskings <ben@xxxxxxxxxxxx> wrote:
> Just accidentally defined a function with the same name as an alias,
> which caused a segfault. (I was replacing the alias with a function,
> and loading the new function with '. ~/.zshrc', which caused both to
> be defined at once.)
> 
> alias gls='glog -S'
> gls() {
>   query="$1"
>   shift
>   glog --pickaxe-regex "-S$query" "$@"
> }
> 
> This command segfaulted zsh:
> 
> $ gls content_for

Yes, you've created an infinite recursion.  The shell has a limit on its
recursion depth so on some systems you would get

glog:3: maximum nested function level reached

which would tell you something is screwy without crashing.  However, the
limit is a bit hit and miss.  If we reduce it we get reports that
it's too low, etc.

To avoid it, you can either define the alias after the function, or to be
safer if you know you're defining both you can quote the name of the
function, or simply use the other form of function definition:

alias gls='glog -S'
function gls {
  query="$1"
  shift
  glog --pickaxe-regex "-S$query" "$@"
}

That still defines gls as a function but the "gls" at this point isn't
expanded as an alias.

(I don't know of any good reason why it's necessary to expand aliases at
that point, beyond the fact that that's how the parser works---words in
command position are always expanded and making exceptions is tricky.)

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



Messages sorted by: Reverse Date, Date, Thread, Author