martanne / vis

A vi-like editor based on Plan 9's structural regular expressions
Other
4.19k stars 260 forks source link

`:2x/foo/<cr>` in a `[No Name]` file with only one line, makes vis get stuck in an infinite loop #1152

Closed emanuele6 closed 8 months ago

emanuele6 commented 8 months ago
  1. Run vis
  2. enter a line: ihello<esc>
  3. enter :2x/foo/
  4. press enter
  5. vis gets stuck in an infinite loop and you have to SIGKILL it to make it stop

gdb backtrace (top frames could be different depending on when you press ^C):

#0  0x0000555555571d3e in text_mark_get ()
#1  0x000055555557b1e8 in view_draw ()   
#2  0x000055555557d556 in selections_new ()
#3  0x000055555556a52d in cmd_print ()    
#4  0x000055555556bf2b in extract ()
#5  0x000055555556c183 in cmd_extract () 
#6  0x000055555556b855 in cmd_select ()  
#7  0x000055555556efd6 in sam_cmd ()      
#8  0x000055555557e952 in vis_cmd.part ()
#9  0x0000555555591472 in prompt_enter ()
#10 0x000055555558230b in vis_keys_push ()
#11 0x0000555555582d30 in vis_run ()
#12 0x00005555555630f5 in main ()
rnpnr commented 8 months ago

This should be fixed by:

diff --git a/sam.c b/sam.c
index 9902a66..afd1233 100644
--- a/sam.c
+++ b/sam.c
@@ -1414,6 +1412,9 @@ static int extract(Vis *vis, Win *win, Command *cmd, const char *argv[], Selecti
    int count = 0;
    Text *txt = win->file->text;

+   if (!text_range_valid(range))
+       return 0;
+
    if (cmd->regex) {
        size_t start = range->start, end = range->end;
        size_t last_start = argv[0][0] == 'x' ? EPOS : start;

But I will need some time to verify if this is the correct place for the fix.