Skip to content

Commit e1bb879

Browse files
committed
patch 8.0.1669: :vimgrep may add entries to the wrong quickfix list
Problem: :vimgrep may add entries to the wrong quickfix list. Solution: Use the list identifier. (Yegappan Lakshmanan)
1 parent c4b533e commit e1bb879

3 files changed

Lines changed: 80 additions & 51 deletions

File tree

‎src/quickfix.c‎

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4159,6 +4159,21 @@ ex_cfile(exarg_T *eap)
41594159
qf_jump(qi, 0, 0, eap->forceit); /* display first error */
41604160
}
41614161

4162+
/*
4163+
* Return the quickfix/location list number with the given identifier.
4164+
* Returns -1 if list is not found.
4165+
*/
4166+
static int
4167+
qf_id2nr(qf_info_T *qi, int_u qfid)
4168+
{
4169+
int qf_idx;
4170+
4171+
for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
4172+
if (qi->qf_lists[qf_idx].qf_id == qfid)
4173+
return qf_idx;
4174+
return -1;
4175+
}
4176+
41624177
/*
41634178
* Return the vimgrep autocmd name.
41644179
*/
@@ -4272,40 +4287,32 @@ vgr_load_dummy_buf(
42724287
*/
42734288
static int
42744289
vgr_qflist_valid(
4290+
win_T *wp,
42754291
qf_info_T *qi,
4276-
int_u save_qfid,
4277-
qfline_T *cur_qf_start,
4278-
int loclist_cmd,
4292+
int_u qfid,
42794293
char_u *title)
42804294
{
4281-
if (loclist_cmd)
4295+
/* Verify that the quickfix/location list was not freed by an autocmd */
4296+
if (!qflist_valid(wp, qfid))
42824297
{
4283-
/*
4284-
* Verify that the location list is still valid. An autocmd might
4285-
* have freed the location list.
4286-
*/
4287-
if (!qflist_valid(curwin, save_qfid))
4298+
if (wp != NULL)
42884299
{
4300+
/* An autocmd has freed the location list. */
42894301
EMSG(_(e_loc_list_changed));
42904302
return FALSE;
42914303
}
4304+
else
4305+
{
4306+
/* Quickfix list is not found, create a new one. */
4307+
qf_new_list(qi, title);
4308+
return TRUE;
4309+
}
42924310
}
4293-
if (cur_qf_start != qi->qf_lists[qi->qf_curlist].qf_start)
4294-
{
4295-
int idx;
42964311

4312+
if (qi->qf_lists[qi->qf_curlist].qf_id != qfid)
42974313
/* Autocommands changed the quickfix list. Find the one we were
42984314
* using and restore it. */
4299-
for (idx = 0; idx < LISTCOUNT; ++idx)
4300-
if (cur_qf_start == qi->qf_lists[idx].qf_start)
4301-
{
4302-
qi->qf_curlist = idx;
4303-
break;
4304-
}
4305-
if (idx == LISTCOUNT)
4306-
/* List cannot be found, create a new one. */
4307-
qf_new_list(qi, title);
4308-
}
4315+
qi->qf_curlist = qf_id2nr(qi, qfid);
43094316

43104317
return TRUE;
43114318
}
@@ -4424,10 +4431,8 @@ ex_vimgrep(exarg_T *eap)
44244431
char_u *p;
44254432
int fi;
44264433
qf_info_T *qi = &ql_info;
4427-
int loclist_cmd = FALSE;
44284434
int_u save_qfid;
4429-
qfline_T *cur_qf_start;
4430-
win_T *wp;
4435+
win_T *wp = NULL;
44314436
buf_T *buf;
44324437
int duplicate_name = FALSE;
44334438
int using_dummy;
@@ -4461,7 +4466,7 @@ ex_vimgrep(exarg_T *eap)
44614466
qi = ll_get_or_alloc_list(curwin);
44624467
if (qi == NULL)
44634468
return;
4464-
loclist_cmd = TRUE;
4469+
wp = curwin;
44654470
}
44664471

44674472
if (eap->addr_count > 0)
@@ -4518,10 +4523,9 @@ ex_vimgrep(exarg_T *eap)
45184523
* ":lcd %:p:h" changes the meaning of short path names. */
45194524
mch_dirname(dirname_start, MAXPATHL);
45204525

4521-
/* Remember the current values of the quickfix list and qf_start, so that
4522-
* we can check for autocommands changing the current quickfix list. */
4526+
/* Remember the current quickfix list identifier, so that we can check for
4527+
* autocommands changing the current quickfix list. */
45234528
save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
4524-
cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
45254529

45264530
seconds = (time_t)0;
45274531
for (fi = 0; fi < fcount && !got_int && tomatch > 0; ++fi)
@@ -4549,11 +4553,11 @@ ex_vimgrep(exarg_T *eap)
45494553
/* Use existing, loaded buffer. */
45504554
using_dummy = FALSE;
45514555

4552-
/* Check whether the quickfix list is still valid */
4553-
if (!vgr_qflist_valid(qi, save_qfid, cur_qf_start, loclist_cmd,
4554-
*eap->cmdlinep))
4556+
/* Check whether the quickfix list is still valid. When loading a
4557+
* buffer above, autocommands might have changed the quickfix list. */
4558+
if (!vgr_qflist_valid(wp, qi, save_qfid, *eap->cmdlinep))
45554559
goto theend;
4556-
cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4560+
save_qfid = qi->qf_lists[qi->qf_curlist].qf_id;
45574561

45584562
if (buf == NULL)
45594563
{
@@ -4567,8 +4571,6 @@ ex_vimgrep(exarg_T *eap)
45674571
found_match = vgr_match_buflines(qi, fname, buf, &regmatch,
45684572
tomatch, duplicate_name, flags);
45694573

4570-
cur_qf_start = qi->qf_lists[qi->qf_curlist].qf_start;
4571-
45724574
if (using_dummy)
45734575
{
45744576
if (found_match && first_match_buf == NULL)
@@ -4649,7 +4651,6 @@ ex_vimgrep(exarg_T *eap)
46494651
* The QuickFixCmdPost autocmd may free the quickfix list. Check the list
46504652
* is still valid.
46514653
*/
4652-
wp = loclist_cmd ? curwin : NULL;
46534654
if (!qflist_valid(wp, save_qfid))
46544655
goto theend;
46554656

@@ -4994,21 +4995,6 @@ qf_get_list_from_lines(dict_T *what, dictitem_T *di, dict_T *retdict)
49944995
return status;
49954996
}
49964997

4997-
/*
4998-
* Return the quickfix/location list number with the given identifier.
4999-
* Returns -1 if list is not found.
5000-
*/
5001-
static int
5002-
qf_id2nr(qf_info_T *qi, int_u qfid)
5003-
{
5004-
int qf_idx;
5005-
5006-
for (qf_idx = 0; qf_idx < qi->qf_listcount; qf_idx++)
5007-
if (qi->qf_lists[qf_idx].qf_id == qfid)
5008-
return qf_idx;
5009-
return -1;
5010-
}
5011-
50124998
/*
50134999
* Return the quickfix/location list window identifier in the current tabpage.
50145000
*/

‎src/testdir/test_quickfix.vim‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,3 +3111,44 @@ func Test_qfwin_pos()
31113111
call assert_equal(3, winnr())
31123112
close
31133113
endfunc
3114+
3115+
" Tests for quickfix/location lists changed by autocommands when
3116+
" :vimgrep/:lvimgrep commands are running.
3117+
func Test_vimgrep_autocmd()
3118+
call setqflist([], 'f')
3119+
call writefile(['stars'], 'Xtest1.txt')
3120+
call writefile(['stars'], 'Xtest2.txt')
3121+
3122+
" Test 1:
3123+
" When searching for a pattern using :vimgrep, if the quickfix list is
3124+
" changed by an autocmd, the results should be added to the correct quickfix
3125+
" list.
3126+
autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
3127+
silent vimgrep stars Xtest*.txt
3128+
call assert_equal(1, getqflist({'nr' : 0}).nr)
3129+
call assert_equal(3, getqflist({'nr' : '$'}).nr)
3130+
call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
3131+
au! BufRead Xtest2.txt
3132+
3133+
" Test 2:
3134+
" When searching for a pattern using :vimgrep, if the quickfix list is
3135+
" freed, then a error should be given.
3136+
silent! %bwipe!
3137+
call setqflist([], 'f')
3138+
autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
3139+
call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
3140+
au! BufRead Xtest2.txt
3141+
3142+
" Test 3:
3143+
" When searching for a pattern using :lvimgrep, if the location list is
3144+
" freed, then the command should error out.
3145+
silent! %bwipe!
3146+
let g:save_winid = win_getid()
3147+
autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
3148+
call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
3149+
au! BufRead Xtest2.txt
3150+
3151+
call delete('Xtest1.txt')
3152+
call delete('Xtest2.txt')
3153+
call setqflist([], 'f')
3154+
endfunc

‎src/version.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ static char *(features[]) =
762762

763763
static int included_patches[] =
764764
{ /* Add new patch number below this line */
765+
/**/
766+
1669,
765767
/**/
766768
1668,
767769
/**/

0 commit comments

Comments
 (0)