Skip to content

Commit 978d170

Browse files
committed
patch 8.2.0151: detecting a script was already sourced is unreliable
Problem: Detecting a script was already sourced is unreliable. Solution: Do not use the inode number.
1 parent 673660a commit 978d170

4 files changed

Lines changed: 13 additions & 39 deletions

File tree

src/scriptfile.c

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,10 +1091,6 @@ do_source(
10911091
int save_debug_break_level = debug_break_level;
10921092
int sid;
10931093
scriptitem_T *si = NULL;
1094-
# ifdef UNIX
1095-
stat_T st;
1096-
int stat_ok;
1097-
# endif
10981094
#endif
10991095
#ifdef STARTUPTIME
11001096
struct timeval tv_rel;
@@ -1121,26 +1117,17 @@ do_source(
11211117

11221118
#ifdef FEAT_EVAL
11231119
// See if we loaded this script before.
1124-
# ifdef UNIX
1125-
stat_ok = (mch_stat((char *)fname_exp, &st) >= 0);
1126-
# endif
11271120
for (sid = script_items.ga_len; sid > 0; --sid)
11281121
{
1122+
// We used to check inode here, but that doesn't work:
1123+
// - If a script is edited and written, it may get a different
1124+
// inode number, even though to the user it is the same script.
1125+
// - If a script is deleted and another script is written, with a
1126+
// different name, the inode may be re-used.
11291127
si = &SCRIPT_ITEM(sid);
1130-
if (si->sn_name != NULL)
1131-
{
1132-
# ifdef UNIX
1133-
// Compare dev/ino when possible, it catches symbolic links. Also
1134-
// compare file names, the inode may change when the file was
1135-
// edited or it may be re-used for another script (esp. in tests).
1136-
if ((stat_ok && si->sn_dev_valid)
1137-
&& (si->sn_dev != st.st_dev || si->sn_ino != st.st_ino))
1138-
continue;
1139-
# endif
1140-
if (fnamecmp(si->sn_name, fname_exp) == 0)
1128+
if (si->sn_name != NULL && fnamecmp(si->sn_name, fname_exp) == 0)
11411129
// Found it!
11421130
break;
1143-
}
11441131
}
11451132
if (sid > 0 && ret_sid != NULL)
11461133
{
@@ -1324,16 +1311,6 @@ do_source(
13241311
si = &SCRIPT_ITEM(current_sctx.sc_sid);
13251312
si->sn_name = fname_exp;
13261313
fname_exp = vim_strsave(si->sn_name); // used for autocmd
1327-
# ifdef UNIX
1328-
if (stat_ok)
1329-
{
1330-
si->sn_dev_valid = TRUE;
1331-
si->sn_dev = st.st_dev;
1332-
si->sn_ino = st.st_ino;
1333-
}
1334-
else
1335-
si->sn_dev_valid = FALSE;
1336-
# endif
13371314
if (ret_sid != NULL)
13381315
*ret_sid = current_sctx.sc_sid;
13391316
}

src/structs.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,11 +1665,6 @@ typedef struct
16651665
int sn_had_command; // TRUE if any command was executed
16661666
char_u *sn_save_cpo; // 'cpo' value when :vim9script found
16671667

1668-
# ifdef UNIX
1669-
int sn_dev_valid;
1670-
dev_t sn_dev;
1671-
ino_t sn_ino;
1672-
# endif
16731668
# ifdef FEAT_PROFILE
16741669
int sn_prof_on; // TRUE when script is/was profiled
16751670
int sn_pr_force; // forceit: profile functions in this script

src/testdir/test_vim9_script.vim

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ source check.vim
44

55
" Check that "lines" inside ":def" results in an "error" message.
66
func CheckDefFailure(lines, error)
7-
call writefile(['def! Func()'] + a:lines + ['enddef'], 'Xdef')
7+
call writefile(['def Func()'] + a:lines + ['enddef'], 'Xdef')
88
call assert_fails('so Xdef', a:error, a:lines)
99
call delete('Xdef')
1010
endfunc
@@ -126,10 +126,10 @@ enddef
126126

127127
def Test_return_type_wrong()
128128
" TODO: why is ! needed for Mac and FreeBSD?
129-
CheckScriptFailure(['def! Func(): number', 'return "a"', 'enddef'], 'expected number but got string')
130-
CheckScriptFailure(['def! Func(): string', 'return 1', 'enddef'], 'expected string but got number')
131-
CheckScriptFailure(['def! Func(): void', 'return "a"', 'enddef'], 'expected void but got string')
132-
CheckScriptFailure(['def! Func()', 'return "a"', 'enddef'], 'expected void but got string')
129+
CheckScriptFailure(['def Func(): number', 'return "a"', 'enddef'], 'expected number but got string')
130+
CheckScriptFailure(['def Func(): string', 'return 1', 'enddef'], 'expected string but got number')
131+
CheckScriptFailure(['def Func(): void', 'return "a"', 'enddef'], 'expected void but got string')
132+
CheckScriptFailure(['def Func()', 'return "a"', 'enddef'], 'expected void but got string')
133133
enddef
134134

135135
def Test_try_catch()

src/version.c

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

743743
static int included_patches[] =
744744
{ /* Add new patch number below this line */
745+
/**/
746+
151,
745747
/**/
746748
150,
747749
/**/

0 commit comments

Comments
 (0)