Subject: bug#5440: 23.1; buffer-file-format encoding temp buffers not reentrant

From: Andrew Hyatt <[email-masked]>
Date: Thu, 21 Jul 2016 00:33:17 -0400

This reproduces on Emacs 25.  But I wonder if this should be a wishlist
instead of a bug.  As the original report notes, decoding a buffer
while encoding another buffer doesn't seem like a normal use-case.

If no one objects in the next few weeks, I can wishlist this.  Making it
a minor-severity bug also seems reasonable to me.

Kevin Ryde <[email-masked]> writes:

> The way build_annotations() makes `format-annotate-function' use the
> same temp buffer " *Format Temp 0*" on each call is not reentrant.  If
> TO-FN from the format-alist does anything that writes another buffer
> with a buffer-file-format then the buffer which TO-FN is supposed to be
> working on is clobbered.
>
> foo.el below illustrates the problem.  Evaluating it writes a /tmp/x
> containing
>
>     THIS IS FORMAT ONE
>     this is buffer yyy text
>
> where I expected it to be
>
>     THIS IS FORMAT TWO
>     hello world
>
> The `message's emitted by my-one-encode and my-two-encode show they get
> the same " *Format Temp 0*" buffer.  The latter is what you get in
> /tmp/x if taking away the "/tmp/y" blob within `my-two-encode'.
>
> I expect this wouldn't arise often in practice, but it ought to be
> reasonably easy to make build_annotations() (or wherever) do better in
> the management of temporary buffers.  I expect it's a matter of finding
> the right place in the write crunching that temp buffers used can be
> killed.
>
>
> (add-to-list 'format-alist '(my-one
>                              "My format."
>                              nil ;; no automatic decode
>                              my-one-decode
>                              my-one-encode
>                              t     ;; encode modifies the region
>                              nil)) ;; write removes from buffer-file-formats
>
> (defun my-one-decode (beg end)
>   end)
>
> (defun my-one-encode (beg end buffer)
>   (message "my-one-encode in %S" (current-buffer))
>
>   (save-excursion
>     (save-restriction
>       (narrow-to-region beg end)
>
>       (insert "THIS IS FORMAT ONE\n")
>
>       (point-max))))
>
>
> (add-to-list 'format-alist '(my-two
>                              "My format."
>                              nil ;; no automatic decode
>                              my-two-decode
>                              my-two-encode
>                              t     ;; encode modifies the region
>                              nil)) ;; write removes from buffer-file-formats
>
> (defun my-two-decode (beg end)
>   (save-excursion
>     (save-restriction
>       (narrow-to-region beg end)
>
>       (goto-char beg)
>       (if (looking-at "THIS IS FORMAT TWO\n")
>           (delete-region (match-beginning 0) (match-end 0)))
>
>       (point-max))))
>
> (defun my-two-encode (beg end buffer)
>   (message "my-two-encode in %S" (current-buffer))
>
>   (save-excursion
>     (save-restriction
>       (narrow-to-region beg end)
>
>       (goto-char (point-min))
>       (insert "THIS IS FORMAT TWO\n")
>
>       (save-current-buffer
>         (find-file "/tmp/y")
>         (format-decode-buffer 'my-one)
>         (erase-buffer)
>         (insert "this is buffer yyy text\n")
>         (save-buffer)
>         (kill-buffer nil))
>
>       (point-max))))
>
>
> (progn
>   (find-file "/tmp/x")
>   (format-decode-buffer 'my-two)
>   (erase-buffer)
>   (insert "hello world\n")
>   (save-buffer)
>   (kill-buffer nil)
>   (find-file "/tmp/x"))
>
>
>
> In GNU Emacs 23.1.1 (i486-pc-linux-gnu, GTK+ Version 2.16.5)
>  of 2009-09-14 on raven, modified by Debian
> configured using `configure  '--build=i486-linux-gnu' '--host=i486-linux-gnu' '--prefix=/usr' '--sharedstatedir=/var/lib' '--libexecdir=/usr/lib' '--localstatedir=/var/lib' '--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-pop=yes' '--enable-locallisppath=/etc/emacs23:/etc/emacs:/usr/local/share/emacs/23.1/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/23.1/site-lisp:/usr/share/emacs/site-lisp:/usr/share/emacs/23.1/leim' '--with-x=yes' '--with-x-toolkit=gtk' '--with-toolkit-scroll-bars' 'build_alias=i486-linux-gnu' 'host_alias=i486-linux-gnu' 'CFLAGS=-DDEBIAN -g -O2' 'LDFLAGS=-g' 'CPPFLAGS=''
>
> Important settings:
>   value of $LC_ALL: nil
>   value of $LC_COLLATE: nil
>   value of $LC_CTYPE: nil
>   value of $LC_MESSAGES: nil
>   value of $LC_MONETARY: nil
>   value of $LC_NUMERIC: nil
>   value of $LC_TIME: nil
>   value of $LANG: en_AU
>   value of $XMODIFIERS: nil
>   locale-coding-system: iso-latin-1-unix
>   default-enable-multibyte-characters: t