Skip to content

Commit 11431a2

Browse files
authored
Protect breakpoint persistence from print-* vars (#792)
* Protect breakpoint persistence from print-* vars The output of printing functions (including 'prin1' and, by extension, 'format' with a '%s' sequence) is affected by a set of output variables that both end-users and libraries can tweak. This is fine in the more common use of these functions to format messages, but the problem is that the same functions are also the most convenient way to serialize/persist data. Serialization should be robust to non-default values of 'print-length' and 'print-level' in particular, as setting these to small numbers can lead to accidental data loss (and in some cases unreadable output). Starting in Emacs 29, 'prin1' and co. gained a new optional argument precisely to protect against this problem. In the meantime, this patch ensures that 'print-length' and 'print-level' are set to their default values during serialization. * Minor printing simplifications 'dap--persist': 'with-temp-file' creates a fresh buffer that is already empty, so there is no need to erase it again. Print data-to-persist directly to output buffer, avoiding roundtrip via temporary string. 'dap--print-to-output-buffer': modes are enabled with a 'nil' or nonnegative numeric argument rather than with 't'; using 'turn-on-font-lock' avoids both this pitfall, as well as redundantly reenabling the mode. Use an 'inhibit-read-only' binding for temporary read-only overrides, as modifications to 'buffer-read-only' will not be undone in case of a non-local exit. 'dap-debug-edit-template': Avoid temporary string roundtrip when checking for an empty buffer. Use 'insert-char' for repetitions of the same character. Remove redundant 'prin1-to-string' in arguments to 'format'; it already uses 'prin1' for the '%s' sequence.
1 parent 45f6a6f commit 11431a2

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

dap-mode.el

+18-17
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,11 @@ This is in contrast to merely setting it to 0."
455455
"Failed to persist file: %S"
456456
(make-directory (file-name-directory file) t)
457457
(with-temp-file file
458-
(erase-buffer)
459-
(insert (prin1-to-string to-persist)))))
458+
;; Starting with Emacs 29, `prin1' takes an optional third argument
459+
;; which removes the need for these default bindings.
460+
(let ((print-length nil)
461+
(print-level nil))
462+
(prin1 to-persist (current-buffer))))))
460463

461464
(defun dap--set-sessions (debug-sessions)
462465
"Update list of debug sessions for WORKSPACE to DEBUG-SESSIONS."
@@ -917,14 +920,13 @@ an OUTPUT-BODY."
917920
(defun dap--print-to-output-buffer (debug-session str)
918921
"Insert content from STR into the output buffer associated with DEBUG-SESSION."
919922
(with-current-buffer (get-buffer-create (dap--debug-session-output-buffer debug-session))
920-
(font-lock-mode t)
921-
(setq-local buffer-read-only nil)
922-
(if (and (eq (current-buffer) (window-buffer (selected-window)))
923-
(not (= (point) (point-max))))
924-
(save-excursion
925-
(dap--insert-at-point-max str))
926-
(dap--insert-at-point-max str))
927-
(setq-local buffer-read-only t))
923+
(turn-on-font-lock)
924+
(let ((inhibit-read-only t))
925+
(if (and (eq (current-buffer) (window-buffer))
926+
(not (eobp)))
927+
(save-excursion
928+
(dap--insert-at-point-max str))
929+
(dap--insert-at-point-max str))))
928930
(when (and dap-auto-show-output
929931
(not (dap--debug-session-output-displayed debug-session)))
930932
(setf (dap--debug-session-output-displayed debug-session) t)
@@ -1913,7 +1915,7 @@ the new template can be used normally with `dap-debug'"
19131915
(emacs-lisp-mode)
19141916
(current-buffer)))
19151917
(goto-char (point-max))
1916-
(when (s-blank? (buffer-string))
1918+
(when (bobp) ;; Empty (accessible portion of) buffer.
19171919
(insert ";; Eval Buffer with `M-x eval-buffer' to register the newly created template."))
19181920
(insert
19191921
(format "\n\n(dap-register-debug-template\n \"%s%s\"\n"
@@ -1922,13 +1924,12 @@ the new template can be used normally with `dap-debug'"
19221924
(insert " (list ")
19231925
(-let ((column (current-column))
19241926
((fst snd . rst) debug-args))
1925-
(insert (format "%s %s" fst (prin1-to-string snd)))
1927+
(insert (format "%s %s" fst snd))
19261928
(cl-loop for (k v) on rst by #'cddr
1927-
do (if (not (equal k :program-to-start))
1928-
(progn
1929-
(insert "\n")
1930-
(--dotimes column (insert " "))
1931-
(insert (format "%s %s" k (prin1-to-string v)))))))
1929+
do (unless (eq k :program-to-start)
1930+
(insert "\n")
1931+
(insert-char ?\s column)
1932+
(insert (format "%s %s" k v)))))
19321933
(insert "))"))
19331934
(pop-to-buffer "*DAP Templates*")
19341935
(goto-char (point-max)))

0 commit comments

Comments
 (0)