Skip to content

Commit

Permalink
Fix unbalanced parenthesis crash in Common Lisp lexer (#1129)
Browse files Browse the repository at this point in the history
The Common Lisp lexer attempted to guard against an unmatched closing
parenthesis being used by checking whether the stack of states was
empty.

This misunderstood the way the stack is used. The stack should always
have at least one state (the bottom-most :root state). Popping this
state will cause an empty stack error to be thrown (as noted in #1102).

The correct guard is to check whether the size of the stack is 1. If it
is, then we have an unmatched closing parenthesis and should generate an
error token. This fixes #1102.
  • Loading branch information
pyrmont authored May 27, 2019
1 parent 6835163 commit 641cd1d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rouge/lexers/common_lisp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class complex concatenated-stream cons echo-stream file-stream

rule /\(/, Punctuation, :root
rule /\)/, Punctuation do
if stack.empty?
if stack.size == 1
token Error
else
token Punctuation
Expand Down
8 changes: 8 additions & 0 deletions spec/lexers/common_lisp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@
assert_guess :mimetype => 'text/x-common-lisp'
end
end

describe 'lexing' do
include Support::Lexing

it 'does not crash on unbalanced parentheses' do
subject.lex(")\n").to_a
end
end
end
3 changes: 3 additions & 0 deletions spec/visual/samples/common_lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#nil (commented (form ftw))

; Unmatched closing parenthesis
)

;;;; TYPEP und Verwandtes
;;;; Michael Stoll, 21. 10. 1988
;;;; Bruno Haible, 10.6.1989
Expand Down

0 comments on commit 641cd1d

Please sign in to comment.