Subject: Crash with zle-isearch-update and history-incremental-pattern-search-backward
From: Daniel Hahler <[email-masked]>
To: Zsh Hackers' List <[email-masked]>
Date: Sun, 29 May 2016 23:41:57 +0200
List-Id: Zsh Workers List <zsh-workers.zsh.org>

I'm seeing the following crash with Ctrl-R (bindkey "^R"
history-incremental-pattern-search-backward), when searching for
e.g. "ssh*sl".  It crashes after no more matches are found, and then being
invoked again - after displaying "failing bck-i-search: ssh*sl".

This is related / triggered by the fact that I was not returning 1 in
the zle-isearch-update hook.

Without the "return 1":

1. Searching for "foo" will not find the 2nd result when pressing Ctrl-R
   again, but does not crash.
2. Searching for "ssh*sl" will find subsequent results, but crashes at
   the end.

It's probably related to the history itself, and not necessarily (only)
to using a wildcard, because "foo*bar" behaves not like "ssh*sl". 

This is the relevant code I am using:

  # Display "^C" when aborting zle, but not for aborting isearch.
  # The "return 1" is necessary for subsequent C-R calls to work.
  zle-isearch-update() {
    _zsh_dont_print_ctrlc=1
    return 1  # This is the problematic "return".
  }
  zle -N zle-isearch-update
  zle-isearch-exit() {
    unset _zsh_dont_print_ctrlc
    return 1
  }
  zle -N zle-isearch-exit
  TRAPINT() {
    if ! (( $+_zsh_dont_print_ctrlc )); then
      print -nP %F{red}%B\^C%f%b
    fi
    return $(( 128 + $1 ))
  }

The zle hook is called via 'zlecallhook("zle-isearch-update", NULL);'.

It looks like there is some global state not handled correctly in
'execzlefunc' called from there (because commenting that will not cause the
crash).

Is it required for zle-isearch-update to return 1 (or rather non-zero)?
From looking at the source it does not look like the return code is
used.

The crash:

Program received signal SIGSEGV, Segmentation fault.
next = PATNEXT(scan);
(gdb) bt
#0  0x000000000048641d in patmatch (prog=0x7ffff7aad578) at pattern.c:2706
#1  0x0000000000485c16 in pattryrefs (prog=0x862950, string=0x7ffff7fe2748 "pstree -apl|less", stringlen=16, unmetalenin=0, patstralloc=0x7fffffff8480, patoffset=0, nump=0x0, begp=0x0, 
    endp=0x0) at pattern.c:2468
#2  0x000000000048560b in pattrylen (prog=0x862950, string=0x7ffff7fe2748 "pstree -apl|less", len=16, unmetalen=0, patstralloc=0x7fffffff8480, offset=0) at pattern.c:2213
#3  0x000000000043fa80 in igetmatch (sp=0x7fffffff8578, p=0x862950, fl=8710, n=0, replstr=0x0, repllistp=0x7fffffff85d0) at glob.c:2949
#4  0x000000000043f0ab in getmatchlist (str=0x9fe7d0 "pstree -apl|less", p=0x862950, repllistp=0x7fffffff85d0) at glob.c:2684
#5  0x00007ffff63201a3 in doisearch (args=0x7ffff7feb188, dir=-1, pattern=1) at zle_hist.c:1314
#6  0x00007ffff631f6d1 in historyincrementalpatternsearchbackward (args=0x7ffff7feb188) at zle_hist.c:921
#7  0x00007ffff6328f90 in execzlefunc (func=0x7ffff655c760 <thingies+10080>, args=0x7ffff7feb188, set_bindk=0) at zle_main.c:1413
#8  0x00007ffff633b6cc in bin_zle_call (name=0x7ffff7feb118 "zle", args=0x7ffff7feb188, ops=0x7fffffff89a0, func=0 '\000') at zle_thingy.c:759
#9  0x00007ffff633a630 in bin_zle (name=0x7ffff7feb118 "zle", args=0x7ffff7feb178, ops=0x7fffffff89a0, func=0) at zle_thingy.c:388
#10 0x000000000040fef0 in execbuiltin (args=0x7ffff7feb0b0, assigns=0x0, bn=0x7ffff655dc60 <bintab+128>) at builtin.c:484
#11 0x000000000043293f in execcmd (state=0x7fffffff93e0, input=0, output=0, how=2, last1=2) at exec.c:3660
#12 0x000000000042cf01 in execpline2 (state=0x7fffffff93e0, pcode=195, how=2, input=0, output=0, last1=0) at exec.c:1758
#13 0x000000000042be40 in execpline (state=0x7fffffff93e0, slcode=5154, how=2, last1=0) at exec.c:1536
#14 0x000000000042b2c1 in execlist (state=0x7fffffff93e0, dont_change_job=1, exiting=0) at exec.c:1302
#15 0x000000000042aac2 in execode (p=0x861d90, dont_change_job=1, exiting=0, context=0x4b104a "shfunc") at exec.c:1085
#16 0x00000000004367d1 in runshfunc (prog=0x861d90, wrap=0x0, name=0x7ffff7feb040 "_zsh_highlight_call_widget") at exec.c:5375
#17 0x00007ffff5cc7776 in zprof_wrapper (prog=0x861d90, w=0x0, name=0x7ffff7feb040 "_zsh_highlight_call_widget") at zprof.c:261
#18 0x0000000000436717 in runshfunc (prog=0x861d90, wrap=0x7ffff5ec8360 <wrapper>, name=0x7ffff7feb040 "_zsh_highlight_call_widget") at exec.c:5360
#19 0x00000000004361e9 in doshfunc (shfunc=0x8619e0, doshargs=0x7ffff7fef128, noreturnval=0) at exec.c:5241
#20 0x00000000004354f9 in execshfunc (shf=0x8619e0, args=0x7ffff7fef128) at exec.c:4872
#21 0x000000000043228d in execcmd (state=0x7fffffffa470, input=0, output=0, how=18, last1=2) at exec.c:3541
#22 0x000000000042cf01 in execpline2 (state=0x7fffffffa470, pcode=67, how=18, input=0, output=0, last1=0) at exec.c:1758
#23 0x000000000042be40 in execpline (state=0x7fffffffa470, slcode=6146, how=18, last1=0) at exec.c:1536
#24 0x000000000042b274 in execlist (state=0x7fffffffa470, dont_change_job=1, exiting=0) at exec.c:1294
#25 0x000000000042aac2 in execode (p=0x86e0d0, dont_change_job=1, exiting=0, context=0x4b104a "shfunc") at exec.c:1085
#26 0x00000000004367d1 in runshfunc (prog=0x86e0d0, wrap=0x0, name=0x7ffff7fef070 "_zsh_highlight_widget_history-incremental-pattern-search-backward") at exec.c:5375
#27 0x00007ffff5cc7776 in zprof_wrapper (prog=0x86e0d0, w=0x0, name=0x7ffff7fef070 "_zsh_highlight_widget_history-incremental-pattern-search-backward") at zprof.c:261
#28 0x0000000000436717 in runshfunc (prog=0x86e0d0, wrap=0x7ffff5ec8360 <wrapper>, name=0x7ffff7fef070 "_zsh_highlight_widget_history-incremental-pattern-search-backward") at exec.c:5360
#29 0x00000000004361e9 in doshfunc (shfunc=0x86e480, doshargs=0x0, noreturnval=1) at exec.c:5241
#30 0x00007ffff6329316 in execzlefunc (func=0x7ffff655a988 <thingies+2440>, args=0x7ffff655e370 <zlenoargs>, set_bindk=0) at zle_main.c:1455
#31 0x00007ffff63280e6 in zlecore () at zle_main.c:1113
#32 0x00007ffff6328a43 in zleread (lp=0x6e0040 <prompt>, rp=0x6e0078 <rprompt>, flags=3, context=0, init=0x7ffff634f6d8 "zle-line-init", finish=0x7ffff634f6c8 "zle-line-finish")
    at zle_main.c:1307
#33 0x00007ffff632b262 in zle_main_entry (cmd=1, ap=0x7fffffffae90) at zle_main.c:2011
#34 0x000000000044f727 in zleentry (cmd=1) at init.c:1531
#35 0x00000000004505de in inputline () at input.c:293
#36 0x000000000045043d in ingetc () at input.c:226
#37 0x00000000004440e0 in ihgetc () at hist.c:391
#38 0x00000000004594f5 in gettok () at lex.c:611
#39 0x0000000000458c28 in zshlex () at lex.c:275
#40 0x00000000004790e2 in parse_event (endtok=37) at parse.c:570
#41 0x000000000044c0f7 in loop (toplevel=1, justonce=0) at init.c:146
#42 0x000000000044fc0a in zsh_main (argc=1, argv=0x7fffffffb378) at init.c:1687
#43 0x000000000040f266 in main (argc=1, argv=0x7fffffffb378) at ./main.c:93

Thanks,
Daniel.