Subject: Re: bug#78842: Ferror_message_string sometimes fails to clear prin1-to-string-buffer

From: Eli Zaretskii <[email-masked]>
Date: Sat, 28 Jun 2025 12:27:30 +0300

> Date: Thu, 19 Jun 2025 20:02:06 +0000
> From:  Pip Cet via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> This is a (hopefully) rare bug which happens to be triggered by a rather
> exotic test case; see recipe below.
> 
> During ordinary operation, Ferror_message_string calls
> print_error_message, then grabs the prin1-to-string buffer's contents
> and erases it.
> 
> Sometimes, however, print_error_message throws an error after writing
> some text ("peculiar error: error", in this case) to the buffer.
> 
> Currently, Ferror_message_string fails to erase the buffer in this case
> (it doesn't catch or unwind-protect anything), so the *next* call to
> prin1-to-string will return the "peculiar error: error" string before
> the actual output.
> 
> This test in print-tests.el:
> 
> (print-tests--deftest error-message-string-circular ()
>   (let ((err (list 'error)))
>     (setcdr err err)
>     (should-error (error-message-string err) :type 'circular-list)))
> 
> triggers this condition; the next prin1-to-string run after this test
> will include the "peculiar error: error" string (twice, in this case,
> because print-tests--deftests defines two tests, one for cl-prin1 and
> one for prin1; they produce one copy each).
> 
> Recipe:
> 
> 1. emacs -Q -l test/src/print-tests.el
> 2. M-x ert RET RET
> 
> Output:
> 
> F print-bignum
>     (ert-test-failed
>      ((should (equal (print-tests--prin1-to-string val) str)) :form
>       (equal
>        "peculiar error: errorpeculiar error: error999999999999999999999999999999999"
>        "999999999999999999999999999999999")
>       :value nil :explanation
>       (arrays-of-different-length 75 33
> 				  "peculiar error: errorpeculiar error: error999999999999999999999999999999999"
> 				  "999999999999999999999999999999999"
> 				  first-mismatch-at 0)))
> 
> Is this proposed patch okay for master?

Fine by me, but please add a comment explaining why we do it this way.

> Should we also modify the error-message-string-circular test to check
> that a subsequent prin1-to-string doesn't produce extra output?

Adding tests is always welcome, thanks.