comparison src/fileio.c @ 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 d96f16667cc4
children 9801d06e7b4c
comparison
equal deleted inserted replaced
5311:e1d46e65db2f 5312:8b5d80861c5e
2924 int newfile; /* editing a new buffer */ 2924 int newfile; /* editing a new buffer */
2925 char_u *fname; /* file name to display */ 2925 char_u *fname; /* file name to display */
2926 int *did_ask; /* flag: whether already asked for key */ 2926 int *did_ask; /* flag: whether already asked for key */
2927 { 2927 {
2928 int method = crypt_method_from_magic((char *)ptr, *sizep); 2928 int method = crypt_method_from_magic((char *)ptr, *sizep);
2929 int b_p_ro = curbuf->b_p_ro;
2929 2930
2930 if (method >= 0) 2931 if (method >= 0)
2931 { 2932 {
2933 /* Mark the buffer as read-only until the decryption has taken place.
2934 * Avoids accidentally overwriting the file with garbage. */
2935 curbuf->b_p_ro = TRUE;
2936
2932 set_crypt_method(curbuf, method); 2937 set_crypt_method(curbuf, method);
2933 if (method > 0) 2938 if (method > 0)
2934 (void)blowfish_self_test(); 2939 (void)blowfish_self_test();
2935 if (cryptkey == NULL && !*did_ask) 2940 if (cryptkey == NULL && !*did_ask)
2936 { 2941 {
2975 /* Remove magic number from the text */ 2980 /* Remove magic number from the text */
2976 *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len; 2981 *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len;
2977 *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len; 2982 *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len;
2978 mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len, 2983 mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len,
2979 (size_t)*sizep); 2984 (size_t)*sizep);
2985 /* Restore the read-only flag. */
2986 curbuf->b_p_ro = b_p_ro;
2980 } 2987 }
2981 } 2988 }
2982 /* When starting to edit a new file which does not have encryption, clear 2989 /* When starting to edit a new file which does not have encryption, clear
2983 * the 'key' option, except when starting up (called with -x argument) */ 2990 * the 'key' option, except when starting up (called with -x argument) */
2984 else if (newfile && *curbuf->b_p_key != NUL && !starting) 2991 else if (newfile && *curbuf->b_p_key != NUL && !starting)