changeset 5312:8b5d80861c5e v7.4.009

updated for version 7.4.009 Problem: When a file was not decrypted (yet), writing it may destroy the contents. Solution: Mark the file as readonly until decryption was done. (Christian Brabandt)
author Bram Moolenaar <bram@vim.org>
date Sun, 25 Aug 2013 17:46:08 +0200
parents e1d46e65db2f
children 477b4f701156
files src/fileio.c src/version.c
diffstat 2 files changed, 9 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -2926,9 +2926,14 @@ check_for_cryptkey(cryptkey, ptr, sizep,
     int		*did_ask;	/* flag: whether already asked for key */
 {
     int method = crypt_method_from_magic((char *)ptr, *sizep);
+    int b_p_ro = curbuf->b_p_ro;
 
     if (method >= 0)
     {
+	/* Mark the buffer as read-only until the decryption has taken place.
+	 * Avoids accidentally overwriting the file with garbage. */
+	curbuf->b_p_ro = TRUE;
+
 	set_crypt_method(curbuf, method);
 	if (method > 0)
 	    (void)blowfish_self_test();
@@ -2977,6 +2982,8 @@ check_for_cryptkey(cryptkey, ptr, sizep,
 	    *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len;
 	    mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len,
 							      (size_t)*sizep);
+	    /* Restore the read-only flag. */
+	    curbuf->b_p_ro = b_p_ro;
 	}
     }
     /* When starting to edit a new file which does not have encryption, clear
--- a/src/version.c
+++ b/src/version.c
@@ -728,6 +728,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    9,
+/**/
     8,
 /**/
     7,