{"thread":{"id":"27318","subject":"[BUG] Autocompletion fails with \"bash: words: bad array subscript\"","startedAt":"2011-05-10T20:13:11Z","lastAt":"2011-05-11T21:09:41Z","messageCount":12,"participants":["Sverre Rabbelier","Jeff King","SZEDER Gábor","Jonathan Nieder"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"167620","messageId":"BANLkTi=nOUEp_J+2dkZZp=HvER-eAdG9eg@mail.gmail.com","threadId":"27318","inReplyTo":null,"subject":"[BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2011-05-10T20:13:11Z","receivedAt":"2011-05-10T20:13:11Z","isPatch":false,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya,\n\n[cced: git completion people]\n\nThis happens if I try use ctrl-shift-r (reverse-i-search) for the\nstring `git commit -am \"S`, resulting in the following:\n\n(reverse-i-search)`git commit -am \"S': git commit -am \"Set new Melange\nversion number to 2-0-20110501 in app.yaml.template.\"\n\nIf I then hit tab, I get:\n\n$ bash: words: bad array subscriptversion number to 2-0-20110501 in\napp.yaml.template.\"\n\nHitting tab again gives:\n\nbash: words: bad array subscript\n\nDisplay all 3032 possibilities? (y or n)\n\nI have no clue how to debug this, other than that it doesn't happen if\nI don't source ~/code/git/contrib/completion/git-completion.bash.\n\nAnyone have any idea's?\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"167623","messageId":"20110510203101.GG14456@sigill.intra.peff.net","threadId":"27318","inReplyTo":"BANLkTi=nOUEp_J+2dkZZp=HvER-eAdG9eg@mail.gmail.com","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-05-10T20:31:01Z","receivedAt":"2011-05-10T20:31:01Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, May 10, 2011 at 10:13:11PM +0200, Sverre Rabbelier wrote:\n\n> This happens if I try use ctrl-shift-r (reverse-i-search) for the\n> string `git commit -am \"S`, resulting in the following:\n> \n> (reverse-i-search)`git commit -am \"S': git commit -am \"Set new Melange\n> version number to 2-0-20110501 in app.yaml.template.\"\n> \n> If I then hit tab, I get:\n> \n> $ bash: words: bad array subscriptversion number to 2-0-20110501 in\n> app.yaml.template.\"\n\nI can reproduce it here pretty easily.\n\n> Hitting tab again gives:\n> \n> bash: words: bad array subscript\n> \n> Display all 3032 possibilities? (y or n)\n> \n> I have no clue how to debug this, other than that it doesn't happen if\n> I don't source ~/code/git/contrib/completion/git-completion.bash.\n> \n> Anyone have any idea's?\n\nYou can try \"set -x\" which will show you what was executing. Of course\nthat will spew pages of output. So you'll want to use something like\n\"script\", or if you're man enough, \"exec 2>stderr\" and then do the whole\nthing blind. ;)\n\nIt looks like we set $cword too low at some point, as the problematic\ncode seems to be:\n\n  + upargs+=(-v $vprev \"${words[cword - 1]}\")\n  bash: words: bad array subscript\n\nbut I haven't figured out yet where that happens.\n\n-Peff\n"},{"id":"167624","messageId":"20110510203943.GH14456@sigill.intra.peff.net","threadId":"27318","inReplyTo":"20110510203101.GG14456@sigill.intra.peff.net","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-05-10T20:39:43Z","receivedAt":"2011-05-10T20:39:43Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, May 10, 2011 at 04:31:01PM -0400, Jeff King wrote:\n\n> It looks like we set $cword too low at some point, as the problematic\n> code seems to be:\n> \n>   + upargs+=(-v $vprev \"${words[cword - 1]}\")\n>   bash: words: bad array subscript\n> \n> but I haven't figured out yet where that happens.\n\nHrm. That code doesn't appear in our completion at all. We provide our\nown _get_comp_words_by_ref, but if it is already defined, we use\nwhatever is there. So on my box, the problematic code comes from\n/etc/bash_completion. And I think it is a bug there, as this is one of\nthe first things called (so git's completion hasn't had a change to\nintroduce any bugs yet :) ).\n\n-Peff\n"},{"id":"167626","messageId":"BANLkTi=0r_hCgt3wh4EkRqS6gnCyyKrY2g@mail.gmail.com","threadId":"27318","inReplyTo":"20110510203943.GH14456@sigill.intra.peff.net","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2011-05-10T20:47:49Z","receivedAt":"2011-05-10T20:47:49Z","isPatch":false,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya,\n\n[also +Jonathan, who I seem to have forgotten to cc]\n\nOn Tue, May 10, 2011 at 22:39, Jeff King <peff@peff.net> wrote:\n> Hrm. That code doesn't appear in our completion at all. We provide our\n> own _get_comp_words_by_ref, but if it is already defined, we use\n> whatever is there. So on my box, the problematic code comes from\n> /etc/bash_completion. And I think it is a bug there, as this is one of\n> the first things called (so git's completion hasn't had a change to\n> introduce any bugs yet :) ).\n\nMost curious, since it doesn't happen when I don't source git\ncompletion. Perhaps it'll happen for any completion, or maybe we're\nusing the completion wrong somehow? I'm on Debian wheezy/sid.\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"167627","messageId":"20110510210116.GA25224@sigill.intra.peff.net","threadId":"27318","inReplyTo":"BANLkTi=0r_hCgt3wh4EkRqS6gnCyyKrY2g@mail.gmail.com","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-05-10T21:01:16Z","receivedAt":"2011-05-10T21:01:16Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, May 10, 2011 at 10:47:49PM +0200, Sverre Rabbelier wrote:\n\n> On Tue, May 10, 2011 at 22:39, Jeff King <peff@peff.net> wrote:\n> > Hrm. That code doesn't appear in our completion at all. We provide our\n> > own _get_comp_words_by_ref, but if it is already defined, we use\n> > whatever is there. So on my box, the problematic code comes from\n> > /etc/bash_completion. And I think it is a bug there, as this is one of\n> > the first things called (so git's completion hasn't had a change to\n> > introduce any bugs yet :) ).\n> \n> Most curious, since it doesn't happen when I don't source git\n> completion. Perhaps it'll happen for any completion, or maybe we're\n> using the completion wrong somehow? I'm on Debian wheezy/sid.\n\nRight. Bash calls into our __git() completion function, which calls the\nimplementation of _get_comp_words_by_ref from /etc/bash_completion,\nwhich has the bug. If you don't source git completion, then you are just\ngetting bash's default file completion.\n\n-Peff\n"},{"id":"167629","messageId":"BANLkTin9=7y+2eqUgo_ObcYeGs3TQ7YMGw@mail.gmail.com","threadId":"27318","inReplyTo":"20110510210116.GA25224@sigill.intra.peff.net","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2011-05-10T21:02:53Z","receivedAt":"2011-05-10T21:02:53Z","isPatch":false,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya,\n\nOn Tue, May 10, 2011 at 23:01, Jeff King <peff@peff.net> wrote:\n> Right. Bash calls into our __git() completion function, which calls the\n> implementation of _get_comp_words_by_ref from /etc/bash_completion,\n> which has the bug. If you don't source git completion, then you are just\n> getting bash's default file completion.\n\nSo should we file this bug with bash's completion people? Also, how\ndid you manage to reproduce? It doesn't happen all the time for me.\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"167630","messageId":"20110510211016.GB26231@sigill.intra.peff.net","threadId":"27318","inReplyTo":"BANLkTin9=7y+2eqUgo_ObcYeGs3TQ7YMGw@mail.gmail.com","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-05-10T21:10:16Z","receivedAt":"2011-05-10T21:10:16Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, May 10, 2011 at 11:02:53PM +0200, Sverre Rabbelier wrote:\n\n> On Tue, May 10, 2011 at 23:01, Jeff King <peff@peff.net> wrote:\n> > Right. Bash calls into our __git() completion function, which calls the\n> > implementation of _get_comp_words_by_ref from /etc/bash_completion,\n> > which has the bug. If you don't source git completion, then you are just\n> > getting bash's default file completion.\n> \n> So should we file this bug with bash's completion people?\n\nProbably, but it would be nice to reduce it to a smaller test case (or\none that happens just with completions shipped by Debian) just to rule\nout anything git is doing.\n\n> Also, how did you manage to reproduce?  It doesn't happen all the time\n> for me.\n\nInitially, with:\n\n  $ git init repo && cd repo\n  $ echo content >file && git add file\n  $ git commit -am foo\n  $ <C-R>git commit -am f<Tab>\n\nbut I also get it with just:\n\n  $ cd /anywhere ;# do not have to even be in a git repo\n  $ <C-R>git<Tab>\n\n-Peff\n"},{"id":"167634","messageId":"20110510221430.GM1377@goldbirke","threadId":"27318","inReplyTo":"BANLkTi=nOUEp_J+2dkZZp=HvER-eAdG9eg@mail.gmail.com","subject":"[PATCH] completion: fix array indexing error after reverse history search","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2011-05-10T22:14:30Z","receivedAt":"2011-05-10T22:14:30Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"In v1.7.4-rc0~11^2~2 (bash: get --pretty=m<tab> completion to work\nwith bash v4, 2010-12-02) we started to use _get_comp_words_by_ref()\nto access completion-related variables.  This function is usually\nprovided by the bash-completion package, but if it's not present when\ngit-completion.bash is loaded, then we use our simplified\nre-implementation of that function.  We also provide a bare bones\n_get_comp_words_by_ref() implementation that works with zsh.\n\nUnfortunately (and interestingly!), since the recent commit da4902a\n(completion: remove unnecessary _get_comp_words_by_ref() invocations,\n2011-04-28) the same bug can be triggered in all of these three\nimplementations when doing reverse history search:\n\n - Hit ctrl-r to start reverse history search.\n - Search for a git command, e.g. by typing 'git '.\n   The prompt will look like this: (reverse-i-search)`git ': git diff\n - Press TAB to attempt completion.\n\nThis will lead to an error like:\n\n  $ bash: words: bad array subscript\n\nThe reason for this is that since commit da4902a we always tell\n_get_comp_words_by_ref() to set the $prev variable in _git(), which\nshould contain the word preceeding the word containing the current\ncursor position.  The value of this variable is assigned in all three\n_get_comp_words_by_ref() implementations with something like\n\n  prev=${COMP_WORDS[COMP_CWORD-1]}\n\nHowever, in the above bug-triggering input the cursor is considered to\nbe at the very beginning of the command line, i.e. in the nullth word,\nso the array index in the above line ends up to be -1, hence the\nerror.  In this case the only sensible value for $prev would be an\nempty string, because there is no previous word on the command line.\n\nThe same applies to the _get_comp_words_by_ref() invocation in\n_gitk(), too.\n\nThis patch fixes both of our _get_comp_words_by_ref() implementations\nby assigning an empty string to $prev when there can't be any previous\nword.  It also works around the bug in _get_comp_words_by_ref() from\nthe bash-completion package by not asking for the $prev variable at\nall when there can't be any previous word.  In the latter case we\ncheck the value of $COMP_CWORD; we usually refrain from using this\nvariable directly in completion functions because of the word\nsplitting changes introduced in bash v4 (see v1.7.4-rc0~11^2~2), but\nin this case, i.e. at the nullth word the different word splitting\nrules don't make a difference.\n\nNoticed-by: Sverre Rabbelier <srabbelier@gmail.com>\nSigned-off-by: SZEDER Gábor <szeder@ira.uka.de>\n---\n\nOn Tue, May 10, 2011 at 10:13:11PM +0200, Sverre Rabbelier wrote:\n> This happens if I try use ctrl-shift-r (reverse-i-search) for the\n> string `git commit -am \"S`, resulting in the following:\n> \n> (reverse-i-search)`git commit -am \"S': git commit -am \"Set new Melange\n> version number to 2-0-20110501 in app.yaml.template.\"\n> \n> If I then hit tab, I get:\n> \n> $ bash: words: bad array subscriptversion number to 2-0-20110501 in\n> app.yaml.template.\"\n\nNice catch, although I have no idea why anyone would attempt\ncompletion at that point.\n\n\nBest,\nGábor\n\n contrib/completion/git-completion.bash |   26 ++++++++++++++++++++++----\n 1 files changed, 22 insertions(+), 4 deletions(-)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex 00691fc..0b12c66 100755\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -445,7 +445,11 @@ _get_comp_words_by_ref ()\n \t\t\tcur=$cur_\n \t\t\t;;\n \t\tprev)\n-\t\t\tprev=${words_[$cword_-1]}\n+\t\t\tif [ \"$cword_\" = 0 ]; then\n+\t\t\t\tprev=\"\"\n+\t\t\telse\n+\t\t\t\tprev=${words_[$cword_-1]}\n+\t\t\tfi\n \t\t\t;;\n \t\twords)\n \t\t\twords=(\"${words_[@]}\")\n@@ -466,7 +470,11 @@ _get_comp_words_by_ref ()\n \t\t\tcur=${COMP_WORDS[COMP_CWORD]}\n \t\t\t;;\n \t\tprev)\n-\t\t\tprev=${COMP_WORDS[COMP_CWORD-1]}\n+\t\t\tif [ \"$COMP_CWORD\" = 0 ]; then\n+\t\t\t\tprev=\"\"\n+\t\t\telse\n+\t\t\t\tprev=${COMP_WORDS[COMP_CWORD-1]}\n+\t\t\tfi\n \t\t\t;;\n \t\twords)\n \t\t\twords=(\"${COMP_WORDS[@]}\")\n@@ -2611,7 +2619,12 @@ _git ()\n \tfi\n \n \tlocal cur words cword prev\n-\t_get_comp_words_by_ref -n =: cur words cword prev\n+\tif [ \"$COMP_CWORD\" = 0 ]; then\n+\t\t_get_comp_words_by_ref -n =: cur words cword\n+\t\tprev=\"\"\n+\telse\n+\t\t_get_comp_words_by_ref -n =: cur words cword prev\n+\tfi\n \twhile [ $c -lt $cword ]; do\n \t\ti=\"${words[c]}\"\n \t\tcase \"$i\" in\n@@ -2662,7 +2675,12 @@ _gitk ()\n \tfi\n \n \tlocal cur words cword prev\n-\t_get_comp_words_by_ref -n =: cur words cword prev\n+\tif [ \"$COMP_CWORD\" = \"0\" ]; then\n+\t\t_get_comp_words_by_ref -n =: cur words cword\n+\t\tprev=\"\"\n+\telse\n+\t\t_get_comp_words_by_ref -n =: cur words cword prev\n+\tfi\n \n \t__git_has_doubledash && return\n \n-- \n1.7.5.1.248.g2ba0c6\n"},{"id":"167637","messageId":"BANLkTimQmX+W1VQcmo9UkPP7cvkw+Se_xA@mail.gmail.com","threadId":"27318","inReplyTo":"20110510221430.GM1377@goldbirke","subject":"Re: [PATCH] completion: fix array indexing error after reverse history search","fromName":"Sverre Rabbelier","fromEmail":"srabbelier@gmail.com","sentAt":"2011-05-10T22:19:53Z","receivedAt":"2011-05-10T22:19:53Z","isPatch":true,"sender":{"key":"srabbelier@gmail.com","avatar":"https://avatars.githubusercontent.com/u/3098?v=4"},"body":"Heya,\n\n2011/5/11 SZEDER Gábor <szeder@ira.uka.de>:\n> Nice catch, although I have no idea why anyone would attempt\n> completion at that point.\n\nWow, amazing turnaround, will test tomorrow :). I hit tab instead of\nenter because I don't want to accidentally run the command.\n\n-- \nCheers,\n\nSverre Rabbelier\n"},{"id":"167641","messageId":"20110510223935.GA31029@goldbirke","threadId":"27318","inReplyTo":"20110510211016.GB26231@sigill.intra.peff.net","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2011-05-10T22:39:35Z","receivedAt":"2011-05-10T22:39:35Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Hi,\n\n\nOn Tue, May 10, 2011 at 05:10:16PM -0400, Jeff King wrote:\n> On Tue, May 10, 2011 at 11:02:53PM +0200, Sverre Rabbelier wrote:\n> \n> > On Tue, May 10, 2011 at 23:01, Jeff King <peff@peff.net> wrote:\n> > > Right. Bash calls into our __git() completion function, which calls the\n> > > implementation of _get_comp_words_by_ref from /etc/bash_completion,\n> > > which has the bug. If you don't source git completion, then you are just\n> > > getting bash's default file completion.\n> > \n> > So should we file this bug with bash's completion people?\n> \n> Probably, but it would be nice to reduce it to a smaller test case (or\n> one that happens just with completions shipped by Debian) just to rule\n> out anything git is doing.\n\nThinking a bit more about it, you don't even need to search history to\nreproduce.  Try this:\n\n  _foo ()\n  {\n          local prev\n          _get_comp_words_by_ref prev\n  }\n  complete -F _foo foo\n\nThen type \"foo\", go back to the very beginning of the command line,\nand then press TAB, and the same \"bash: words: bad array subscript\"\nerror appears.\n\nSo the bug is definitely not git-related.  The fix would be a check\nalong the lines of the first two hunks of the patch I just sent out.\nOddly enough, the bash-completion folks had a similar check in the now\ndeprecated _get_pword() function, that didn't made it into\n_get_comp_words_by_ref()...\n\n\nBest,\nGábor\n"},{"id":"167649","messageId":"20110510234552.GG1994@elie","threadId":"27318","inReplyTo":"20110510223935.GA31029@goldbirke","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2011-05-10T23:45:52Z","receivedAt":"2011-05-10T23:45:52Z","isPatch":false,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"SZEDER Gábor wrote:\n\n> Thinking a bit more about it, you don't even need to search history to\n> reproduce.\n\nYep.  Doing\n\n $ . /etc/bash_completion\n $ ls<^A><TAB>\n bash: words: bad array subscript\n\nproduces the same.\n\n> So the bug is definitely not git-related.  The fix would be a check\n> along the lines of the first two hunks of the patch I just sent out.\n\nFWIW it looks like the bash-completion lib adopted a different fix\nrecently: see [1] (_init_completion: Indicate that completion should\nnot continue if cword == 0, 2011-05-02).\n\n[1] http://git.debian.org/?p=bash-completion/bash-completion.git;a=commitdiff;h=457dbf6061eea5f2d1e3bccacf1691265f7321cc\n"},{"id":"167691","messageId":"20110511210941.GC31029@goldbirke","threadId":"27318","inReplyTo":"20110510234552.GG1994@elie","subject":"Re: [BUG] Autocompletion fails with \"bash: words: bad array subscript\"","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2011-05-11T21:09:41Z","receivedAt":"2011-05-11T21:09:41Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Hi,\n\n\nOn Tue, May 10, 2011 at 06:45:52PM -0500, Jonathan Nieder wrote:\n> SZEDER Gábor wrote:\n> > So the bug is definitely not git-related.  The fix would be a check\n> > along the lines of the first two hunks of the patch I just sent out.\n> \n> FWIW it looks like the bash-completion lib adopted a different fix\n> recently: see [1] (_init_completion: Indicate that completion should\n> not continue if cword == 0, 2011-05-02).\n> \n> [1] http://git.debian.org/?p=bash-completion/bash-completion.git;a=commitdiff;h=457dbf6061eea5f2d1e3bccacf1691265f7321cc\n\nThat doesn't fix the issue at hand.  _init_completion() invokes\n_get_comp_words_by_ref() before that check to set all variables [1],\nwhich will in turn try to access the -1th element of the array when\nsetting $prev, producing the same error.\n\n\nBest,\nGábor\n\n\n[1]: http://git.debian.org/?p=bash-completion/bash-completion.git;a=blob;f=bash_completion;h=e88e2fc8cd97f2a43173b45a1448451bb53e55ab;hb=457dbf6061eea5f2d1e3bccacf1691265f7321cc#l708\n"}]}