@@ -666,13 +666,13 @@ readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
666666 }
667667 }
668668
669- #ifdef UNIX
670- /*
671- * On Unix it is possible to read a directory, so we have to
672- * check for it before the mch_open().
673- */
674669 if (!read_stdin && !read_buffer )
675670 {
671+ #ifdef UNIX
672+ /*
673+ * On Unix it is possible to read a directory, so we have to
674+ * check for it before the mch_open().
675+ */
676676 perm = mch_getperm (fname );
677677 if (perm >= 0 && !S_ISREG (perm ) /* not a regular file ... */
678678# ifdef S_ISFIFO
@@ -695,8 +695,8 @@ readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
695695 msg_scroll = msg_save ;
696696 return FAIL ;
697697 }
698-
699- # if defined(MSDOS ) || defined(MSWIN ) || defined(OS2 )
698+ #endif
699+ #if defined(MSDOS ) || defined(MSWIN ) || defined(OS2 )
700700 /*
701701 * MS-Windows allows opening a device, but we will probably get stuck
702702 * trying to read it.
@@ -708,9 +708,8 @@ readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
708708 msg_scroll = msg_save ;
709709 return FAIL ;
710710 }
711- # endif
712- }
713711#endif
712+ }
714713
715714 /* Set default or forced 'fileformat' and 'binary'. */
716715 set_file_options (set_options , eap );
@@ -3173,9 +3172,14 @@ check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask)
31733172 int * did_ask ; /* flag: whether already asked for key */
31743173{
31753174 int method = crypt_method_from_magic ((char * )ptr , * sizep );
3175+ int b_p_ro = curbuf -> b_p_ro ;
31763176
31773177 if (method >= 0 )
31783178 {
3179+ /* Mark the buffer as read-only until the decryption has taken place.
3180+ * Avoids accidentally overwriting the file with garbage. */
3181+ curbuf -> b_p_ro = TRUE;
3182+
31793183 set_crypt_method (curbuf , method );
31803184 if (method > 0 )
31813185 (void )blowfish_self_test ();
@@ -3224,6 +3228,8 @@ check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask)
32243228 * sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len ;
32253229 mch_memmove (ptr , ptr + CRYPT_MAGIC_LEN + salt_len + seed_len ,
32263230 (size_t )* sizep );
3231+ /* Restore the read-only flag. */
3232+ curbuf -> b_p_ro = b_p_ro ;
32273233 }
32283234 }
32293235 /* When starting to edit a new file which does not have encryption, clear
0 commit comments