]> granicus.if.org Git - vim/commitdiff
patch 8.2.1793: not consistently giving the "is a directory" warning v8.2.1793
authorBram Moolenaar <Bram@vim.org>
Sat, 3 Oct 2020 15:04:37 +0000 (17:04 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 3 Oct 2020 15:04:37 +0000 (17:04 +0200)
Problem:    Not consistently giving the "is a directory" warning.
Solution:   Adjust check for illegal file name and directory. (Yasuhiro
            Matsumoto, closes #7067)

src/fileio.c
src/testdir/test_edit.vim
src/version.c

index aa6596df64429744bb05a538efa0dddc75790de0..83924b352cf01f4202bd48a2ee06405b1eb51071 100644 (file)
@@ -211,6 +211,7 @@ readfile(
     char_u     *old_b_fname;
     int                using_b_ffname;
     int                using_b_fname;
+    static char *msg_is_a_directory = N_("is a directory");
 
     au_did_filetype = FALSE; // reset before triggering any autocommands
 
@@ -310,22 +311,29 @@ readfile(
     else
        msg_scroll = TRUE;      // don't overwrite previous file message
 
-    /*
-     * If the name ends in a path separator, we can't open it.  Check here,
-     * because reading the file may actually work, but then creating the swap
-     * file may destroy it!  Reported on MS-DOS and Win 95.
-     * If the name is too long we might crash further on, quit here.
-     */
     if (fname != NULL && *fname != NUL)
     {
-       p = fname + STRLEN(fname);
-       if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
+       size_t namelen = STRLEN(fname);
+
+       // If the name is too long we might crash further on, quit here.
+       if (namelen >= MAXPATHL)
        {
            filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
            msg_end();
            msg_scroll = msg_save;
            return FAIL;
        }
+
+       // If the name ends in a path separator, we can't open it.  Check here,
+       // because reading the file may actually work, but then creating the
+       // swap file may destroy it!  Reported on MS-DOS and Win 95.
+       if (after_pathsep(fname, fname + namelen))
+       {
+           filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
+           msg_end();
+           msg_scroll = msg_save;
+           return FAIL;
+       }
     }
 
     if (!read_stdin && !read_buffer && !read_fifo)
@@ -349,7 +357,7 @@ readfile(
 
            if (S_ISDIR(perm))
            {
-               filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
+               filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
                retval = NOTDONE;
            }
            else
@@ -475,7 +483,7 @@ readfile(
        perm = mch_getperm(fname);  // check if the file exists
        if (isdir_f)
        {
-           filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
+           filemess(curbuf, sfname, (char_u *)_(msg_is_a_directory), 0);
            curbuf->b_p_ro = TRUE;      // must use "w!" now
        }
        else
index 0dbca453381e0d9816c88b3f9246ae75ba9414eb..66073455752540578bfd8af8b90db6a064e366ba 100644 (file)
@@ -1587,6 +1587,31 @@ func Test_edit_illegal_filename()
   close!
 endfunc
 
+" Test for editing a directory
+func Test_edit_is_a_directory()
+  CheckEnglish
+  let dirname = getcwd() . "/Xdir"
+  call mkdir(dirname, 'p')
+
+  new
+  redir => msg
+  exe 'edit' dirname
+  redir END
+  call assert_match("is a directory$", split(msg, "\n")[0])
+  bwipe!
+
+  let dirname .= '/'
+
+  new
+  redir => msg
+  exe 'edit' dirname
+  redir END
+  call assert_match("is a directory$", split(msg, "\n")[0])
+  bwipe!
+
+  call delete(dirname, 'rf')
+endfunc
+
 " Test for editing a file using invalid file encoding
 func Test_edit_invalid_encoding()
   CheckEnglish
index 33d39bdd6fb81995816b4d51ce527f57be54ba96..beca185c764375349de02594c3e6d7566d9d815f 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1793,
 /**/
     1792,
 /**/