Skip to content

Commit 2a9d5d3

Browse files
committed
patch 8.2.2133: Vim9: checking for a non-empty string is too strict
Problem: Vim9: checking for a non-empty string is too strict. Solution: Check for any string. (closes #7447)
1 parent 3ae50c7 commit 2a9d5d3

6 files changed

Lines changed: 59 additions & 24 deletions

File tree

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,5 @@ EXTERN char e_for_argument_must_be_sequence_of_lists[]
313313
INIT(= N_("E1140: For argument must be a sequence of lists"));
314314
EXTERN char e_indexable_type_required[]
315315
INIT(= N_("E1141: Indexable type required"));
316+
EXTERN char e_non_empty_string_required[]
317+
INIT(= N_("E1142: Non-empty string required"));

src/filepath.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ f_exepath(typval_T *argvars, typval_T *rettv)
876876
{
877877
char_u *p = NULL;
878878

879-
if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
879+
if (in_vim9script() && check_for_nonempty_string(&argvars[0]) == FAIL)
880880
return;
881881
(void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
882882
rettv->v_type = VAR_STRING;
@@ -942,7 +942,7 @@ findfilendir(
942942

943943
rettv->vval.v_string = NULL;
944944
rettv->v_type = VAR_STRING;
945-
if (in_vim9script() && check_for_string(&argvars[0]) == FAIL)
945+
if (in_vim9script() && check_for_nonempty_string(&argvars[0]) == FAIL)
946946
return;
947947

948948
#ifdef FEAT_SEARCHPATH
@@ -1028,9 +1028,9 @@ f_fnamemodify(typval_T *argvars, typval_T *rettv)
10281028
return;
10291029
fname = tv_get_string_chk(&argvars[0]);
10301030
mods = tv_get_string_buf_chk(&argvars[1], buf);
1031-
if (fname == NULL || mods == NULL)
1031+
if (fname == NULL)
10321032
fname = NULL;
1033-
else
1033+
else if (mods != NULL && *mods != NUL)
10341034
{
10351035
len = (int)STRLEN(fname);
10361036
(void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);

src/proto/typval.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ varnumber_T tv_get_bool(typval_T *varp);
1010
varnumber_T tv_get_bool_chk(typval_T *varp, int *denote);
1111
float_T tv_get_float(typval_T *varp);
1212
int check_for_string(typval_T *tv);
13+
int check_for_nonempty_string(typval_T *tv);
1314
char_u *tv_get_string(typval_T *varp);
1415
char_u *tv_get_string_buf(typval_T *varp, char_u *buf);
1516
char_u *tv_get_string_chk(typval_T *varp);

src/testdir/test_vim9_builtin.vim

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,17 @@ def Test_count()
186186
enddef
187187

188188
def Test_executable()
189+
assert_false(executable(""))
190+
assert_false(executable(test_null_string()))
191+
192+
CheckDefExecFailure(['echo executable(123)'], 'E928:')
189193
CheckDefExecFailure(['echo executable(true)'], 'E928:')
190-
CheckDefExecFailure(['echo executable(v:null)'], 'E928:')
191-
CheckDefExecFailure(['echo executable("")'], 'E928:')
192194
enddef
193195

194196
def Test_exepath()
195197
CheckDefExecFailure(['echo exepath(true)'], 'E928:')
196198
CheckDefExecFailure(['echo exepath(v:null)'], 'E928:')
197-
CheckDefExecFailure(['echo exepath("")'], 'E928:')
199+
CheckDefExecFailure(['echo exepath("")'], 'E1142:')
198200
enddef
199201

200202
def Test_expand()
@@ -254,36 +256,42 @@ def Test_map_function_arg()
254256
enddef
255257

256258
def Test_filereadable()
259+
assert_false(filereadable(""))
260+
assert_false(filereadable(test_null_string()))
261+
262+
CheckDefExecFailure(['echo filereadable(123)'], 'E928:')
257263
CheckDefExecFailure(['echo filereadable(true)'], 'E928:')
258-
CheckDefExecFailure(['echo filereadable(v:null)'], 'E928:')
259-
CheckDefExecFailure(['echo filereadable("")'], 'E928:')
260264
enddef
261265

262266
def Test_filewritable()
267+
assert_false(filewritable(""))
268+
assert_false(filewritable(test_null_string()))
269+
270+
CheckDefExecFailure(['echo filewritable(123)'], 'E928:')
263271
CheckDefExecFailure(['echo filewritable(true)'], 'E928:')
264-
CheckDefExecFailure(['echo filewritable(v:null)'], 'E928:')
265-
CheckDefExecFailure(['echo filewritable("")'], 'E928:')
266272
enddef
267273

268274
def Test_finddir()
269275
CheckDefExecFailure(['echo finddir(true)'], 'E928:')
270276
CheckDefExecFailure(['echo finddir(v:null)'], 'E928:')
271-
CheckDefExecFailure(['echo finddir("")'], 'E928:')
277+
CheckDefExecFailure(['echo finddir("")'], 'E1142:')
272278
enddef
273279

274280
def Test_findfile()
275281
CheckDefExecFailure(['echo findfile(true)'], 'E928:')
276282
CheckDefExecFailure(['echo findfile(v:null)'], 'E928:')
277-
CheckDefExecFailure(['echo findfile("")'], 'E928:')
283+
CheckDefExecFailure(['echo findfile("")'], 'E1142:')
278284
enddef
279285

280286
def Test_fnamemodify()
287+
CheckDefSuccess(['echo fnamemodify(test_null_string(), ":p")'])
288+
CheckDefSuccess(['echo fnamemodify("", ":p")'])
289+
CheckDefSuccess(['echo fnamemodify("file", test_null_string())'])
290+
CheckDefSuccess(['echo fnamemodify("file", "")'])
291+
281292
CheckDefExecFailure(['echo fnamemodify(true, ":p")'], 'E928:')
282293
CheckDefExecFailure(['echo fnamemodify(v:null, ":p")'], 'E928:')
283-
CheckDefExecFailure(['echo fnamemodify("", ":p")'], 'E928:')
284294
CheckDefExecFailure(['echo fnamemodify("file", true)'], 'E928:')
285-
CheckDefExecFailure(['echo fnamemodify("file", v:null)'], 'E928:')
286-
CheckDefExecFailure(['echo fnamemodify("file", "")'], 'E928:')
287295
enddef
288296

289297
def Test_filter_wrong_dict_key_type()
@@ -359,27 +367,35 @@ def Test_getloclist_return_type()
359367
enddef
360368

361369
def Test_getfperm()
370+
assert_equal('', getfperm(""))
371+
assert_equal('', getfperm(test_null_string()))
372+
362373
CheckDefExecFailure(['echo getfperm(true)'], 'E928:')
363374
CheckDefExecFailure(['echo getfperm(v:null)'], 'E928:')
364-
CheckDefExecFailure(['echo getfperm("")'], 'E928:')
365375
enddef
366376

367377
def Test_getfsize()
378+
assert_equal(-1, getfsize(""))
379+
assert_equal(-1, getfsize(test_null_string()))
380+
368381
CheckDefExecFailure(['echo getfsize(true)'], 'E928:')
369382
CheckDefExecFailure(['echo getfsize(v:null)'], 'E928:')
370-
CheckDefExecFailure(['echo getfsize("")'], 'E928:')
371383
enddef
372384

373385
def Test_getftime()
386+
assert_equal(-1, getftime(""))
387+
assert_equal(-1, getftime(test_null_string()))
388+
374389
CheckDefExecFailure(['echo getftime(true)'], 'E928:')
375390
CheckDefExecFailure(['echo getftime(v:null)'], 'E928:')
376-
CheckDefExecFailure(['echo getftime("")'], 'E928:')
377391
enddef
378392

379393
def Test_getftype()
394+
assert_equal('', getftype(""))
395+
assert_equal('', getftype(test_null_string()))
396+
380397
CheckDefExecFailure(['echo getftype(true)'], 'E928:')
381398
CheckDefExecFailure(['echo getftype(v:null)'], 'E928:')
382-
CheckDefExecFailure(['echo getftype("")'], 'E928:')
383399
enddef
384400

385401
def Test_getqflist_return_type()

src/typval.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,21 +341,35 @@ tv_get_float(typval_T *varp)
341341
#endif
342342

343343
/*
344-
* Give an error and return FAIL unless "tv" is a non-empty string.
344+
* Give an error and return FAIL unless "tv" is a string.
345345
*/
346346
int
347347
check_for_string(typval_T *tv)
348348
{
349-
if (tv->v_type != VAR_STRING
350-
|| tv->vval.v_string == NULL
351-
|| *tv->vval.v_string == NUL)
349+
if (tv->v_type != VAR_STRING)
352350
{
353351
emsg(_(e_stringreq));
354352
return FAIL;
355353
}
356354
return OK;
357355
}
358356

357+
/*
358+
* Give an error and return FAIL unless "tv" is a non-empty string.
359+
*/
360+
int
361+
check_for_nonempty_string(typval_T *tv)
362+
{
363+
if (check_for_string(tv) == FAIL)
364+
return FAIL;
365+
if (tv->vval.v_string == NULL || *tv->vval.v_string == NUL)
366+
{
367+
emsg(_(e_non_empty_string_required));
368+
return FAIL;
369+
}
370+
return OK;
371+
}
372+
359373
/*
360374
* Get the string value of a variable.
361375
* If it is a Number variable, the number is converted into a string.

src/version.c

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

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2133,
753755
/**/
754756
2132,
755757
/**/

0 commit comments

Comments
 (0)