Subject: 24.4; Broken function in `window-size-change-functions' cause `write-file' to write the wrong buffer
From: Anders Lindgren <[email-masked]>
To: [email-masked]
Date: Tue, 13 Jan 2015 00:37:53 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi!

I have noticed that if a broken window size change function is installed,
`write-file' will save the content of the wrong buffer. One such broken
windows-size-change-function is `follow-window-size-change' of Follow Mode
(a package I wrote many years ago).

To verify this, place the following piece of code in a buffer and evaluate
it (lets call this the CODE buffer). Call M-x test-write-file RET. The code
first creates a new file with the content "Alpha", this works OK. It then
tries to overwrite the file with the content "Beta", answer "y" when asked
to confirm this. Now, the file contains the content of the CODE buffer, and
the CODE buffer is associated with the "test.txt" file name, which clearly
is incorrect.

What happens is that the `y-or-n-p' triggers a window size change callback.
`follow-window-size-change' walks through all buffers to check if anything
is needed to be done. It then restores the original frame, the original
buffer, and the original window. Unfortunately, when restoring the original
window, it also sets the buffer associated with the window as current, this
causes `write-file' to write the wrong buffer.

This will, most likely, affect other functions also using `y-or-n-p', or
otherwise trigger a window change callback.

Interestingly, the problem only is visible in Emacs 24.3 and 24.4, even
though `follow-window-size-change' has been broken much longer than that.
This makes me wonder if there has been a change done to the code that calls
the functions in `window-size-change-functions'.

Of course, this should be corrected in Follow Mode (maybe a simple
reordering of the restore expression would suffice). However, it might be a
good idea for Emacs to ensure that the correct frame, window, and buffer is
set, in case of other broken size change functions.

;;; write-file-bug.el

;; Works in 24.2, broken in 24.3.

(require 'follow)

;; This simulates that there is at least one buffer with Follow Mode
;; enabled.
(add-hook 'window-size-change-functions 'follow-window-size-change t)

(defvar test-write-file--filename "test.txt")

(defun test-write-file--write-to-file (s)
  (with-temp-buffer
    (insert s)
    (write-file test-write-file--filename t)))

(defun test-write-file ()
  (interactive)
  (when (file-exists-p test-write-file--filename)
    (delete-file test-write-file--filename))
  (test-write-file--write-to-file "Alpha")
  (test-write-file--write-to-file "Beta"))

;;; white-file-bug.el ends here.

Sincerely,
    Anders Lindgren

In GNU Emacs 24.4.1 (x86_64-apple-darwin13.4.0, NS apple-appkit-1265.21)
 of 2014-10-21 on builder10-9.porkrind.org
Windowing system distributor `Apple', version 10.3.1265
Configured using:
 `configure --with-ns'

Important settings:
  value of $LC_CTYPE: UTF-8
  locale-coding-system: utf-8-unix

Major mode: Text

Minor modes in effect:
  tooltip-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
<escape> x e v a l - b u f <tab> <return> <escape>
x e <backspace> t e s t - e <backspace> w <tab> <return>
y <escape> x r e p o <tab> o <backspace> r <tab> <
return>

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Saving file /Users/anders/emacs/src/bugs/test.txt...
Wrote /Users/anders/emacs/src/bugs/test.txt
File `test.txt' exists; overwrite? (y or n) y
Saving file /Users/anders/emacs/src/bugs/test.txt...
Wrote /Users/anders/emacs/src/bugs/test.txt
Making completion list...

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr emacsbug message format-spec rfc822 mml
mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mm-util
help-fns mail-prsvr mail-utils help-mode follow easymenu vc-dispatcher
vc-svn time-date tooltip electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel ns-win tool-bar dnd fontset image regexp-opt
fringe tabulated-list newcomment lisp-mode prog-mode register page
menu-bar rfn-eshadow timer select scroll-bar mouse jit-lock font-lock
syntax facemenu font-core frame cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
case-table epa-hook jka-cmpr-hook help simple abbrev minibuffer nadvice
loaddefs button faces cus-face macroexp files text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote make-network-process cocoa ns
multi-tty emacs)

Memory information:
((conses 16 74101 5798)
 (symbols 48 17581 0)
 (miscs 40 40 159)
 (strings 32 10646 4637)
 (string-bytes 1 280871)
 (vectors 16 9360)
 (vector-slots 8 377207 14310)
 (floats 8 54 183)
 (intervals 56 224 0)
 (buffers 960 13))