diff --git a/lib/pigeon/lexer.rb b/lib/pigeon/lexer.rb index d8001b9..7753ff9 100644 --- a/lib/pigeon/lexer.rb +++ b/lib/pigeon/lexer.rb @@ -14,10 +14,12 @@ module Pigeon @tokens = [] @state = HEADER @last_good = :START + @loops = 0 end def tokenize until scanner.eos? + safety_check case @state when HEADER then do_header when BODY then do_body @@ -30,6 +32,7 @@ module Pigeon def tokenize_unsigned(signature) until scanner.eos? + safety_check case @state when HEADER then do_header when BODY then do_body @@ -42,6 +45,14 @@ module Pigeon private + def safety_check + if @loops > 1000 + raise "RUNAWAY LOOP DETECTED" + else + @loops += 1 + end + end + attr_reader :bundle_string, :scanner, :tokens # TODO: Change all the `{40,90}` values in ::Lexer to real values # TODO: Create regexes using string and Regexp.new() for cleaner regexes. @@ -167,6 +178,7 @@ module Pigeon @state = HEADER maybe_end_message! @last_good = :FOOTER_SEPERATOR + @loops = 0 return end diff --git a/spec/pigeon/message_spec.rb b/spec/pigeon/message_spec.rb index 392c135..bf03cfd 100644 --- a/spec/pigeon/message_spec.rb +++ b/spec/pigeon/message_spec.rb @@ -167,14 +167,13 @@ RSpec.describe Pigeon::Message do # This was originally a bug nooted during development # That caused a runaway loop in the tokenizer. it "handles this key: '\\nVUx0hC3'" do - pending("Known bug- will fix after writing docs.") db.delete_current_draft db.new_draft(kind: "unit_test") db.update_draft("\nVUx0hC3", "n") db.update_draft("n", "\nVUx0hC3") Timeout::timeout(0.5) do boom = -> { Pigeon::Lexer.tokenize(db.publish_draft.render) } - expect(boom).to raise_error(Pigeon::Lexer::LexError) + expect(boom).to raise_error("RUNAWAY LOOP DETECTED") end end