Skip to content

Commit 1587839

Browse files
authored
Merge pull request #169 from mrBliss/fix-168
Fix #168: use while in rust-rewind-irrelevant
2 parents 5cfb919 + bec3d0c commit 1587839

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

rust-mode-tests.el

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,23 @@ All positions are position symbols found in `rust-test-positions-alist'."
11621162
'nonblank-line-indented-already-middle-target
11631163
#'indent-for-tab-command))
11641164

1165+
(ert-deftest no-stack-overflow-in-rust-rewind-irrelevant ()
1166+
(with-temp-buffer
1167+
(rust-mode)
1168+
(insert "fn main() {\n let x = 1;")
1169+
;; Insert 150 separate comments on the same line
1170+
(dotimes (i 150)
1171+
(insert "/* foo */ "))
1172+
;; Rewinding from the last commment to the end of the let needs at least
1173+
;; 150 iterations, but if we limit the stack depth to 100 (this appears to
1174+
;; be some minimum), a recursive function would overflow, throwing an
1175+
;; error.
1176+
(let ((max-lisp-eval-depth 100))
1177+
(rust-rewind-irrelevant)
1178+
;; Only a non-stack overflowing function would make it this far. Also
1179+
;; check that we rewound till after the ;
1180+
(should (= (char-before) ?\;)))))
1181+
11651182
(defun rust-test-fontify-string (str)
11661183
(with-temp-buffer
11671184
(rust-mode)

rust-mode.el

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,20 @@ function or trait. When nil, where will be aligned with fn or trait."
201201
(defun rust-paren-level () (nth 0 (syntax-ppss)))
202202
(defun rust-in-str-or-cmnt () (nth 8 (syntax-ppss)))
203203
(defun rust-rewind-past-str-cmnt () (goto-char (nth 8 (syntax-ppss))))
204+
204205
(defun rust-rewind-irrelevant ()
205-
(let ((starting (point)))
206-
(skip-chars-backward "[:space:]\n")
207-
(if (rust-looking-back-str "*/") (backward-char))
208-
(if (rust-in-str-or-cmnt)
209-
(rust-rewind-past-str-cmnt))
210-
(if (/= starting (point))
211-
(rust-rewind-irrelevant))))
206+
(let ((continue t))
207+
(while continue
208+
(let ((starting (point)))
209+
(skip-chars-backward "[:space:]\n")
210+
(when (rust-looking-back-str "*/")
211+
(backward-char))
212+
(when (rust-in-str-or-cmnt)
213+
(rust-rewind-past-str-cmnt))
214+
;; Rewind until the point no longer moves
215+
(setq continue (/= starting (point)))))))
216+
217+
212218
(defun rust-in-macro ()
213219
(save-excursion
214220
(when (> (rust-paren-level) 0)

0 commit comments

Comments
 (0)