Subject: Re: bug#16029: 24.3.50; epa-file.el: decrypted contents get inserted into the wrong buffer
From: Stefan Monnier <[email-masked]>
To: Attila Lendvai <[email-masked]>
Cc: [email-masked]
Date: Mon, 02 Dec 2013 13:56:40 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

> it's a race condition while opening a file using epa-file.el to decrypt
> its contents on the fly, and something else closing the buffer.

Indeed, epg-decrypt-file runs filters and timers, so "anything can
happen".  I installed the patch below to try and address this problem,

        Stefan

=== modified file 'lisp/epa-file.el'
--- lisp/epa-file.el	2013-10-28 08:04:48 +0000
+++ lisp/epa-file.el	2013-12-02 18:51:23 +0000
@@ -132,6 +132,7 @@
 	    (error)))
 	 (local-file (or local-copy file))
 	 (context (epg-make-context))
+         (buf (current-buffer))
 	 string length entry)
     (if visit
 	(setq buffer-file-name file))
@@ -157,9 +158,10 @@
 			 nil t))
 	     (signal 'file-error
 		     (cons "Opening input file" (cdr error)))))
-	  (make-local-variable 'epa-file-encrypt-to)
-	  (setq epa-file-encrypt-to
-		(mapcar #'car (epg-context-result-for context 'encrypted-to)))
+          (set-buffer buf) ;In case timer/filter changed/killed it (bug#16029)!
+	  (setq-local epa-file-encrypt-to
+                      (mapcar #'car (epg-context-result-for
+                                     context 'encrypted-to)))
 	  (if (or beg end)
 	      (setq string (substring string (or beg 0) end)))
 	  (save-excursion