diff --git a/lib/review/book/chapter.rb b/lib/review/book/chapter.rb index ec52c596d..bf9d3731f 100644 --- a/lib/review/book/chapter.rb +++ b/lib/review/book/chapter.rb @@ -85,6 +85,8 @@ def find_first_header_option nil rescue ArgumentError => e raise ReVIEW::CompileError, "#{@name}: #{e}" + rescue SyntaxError => e + raise ReVIEW::SyntaxError, "#{@name}:#{f.lineno}: #{e}" end end diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index 8de07f37b..3ee87f119 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -333,6 +333,8 @@ def do_compile end end close_all_tagged_section + rescue SyntaxError => e + error e end def compile_minicolumn_begin(name, caption = nil) diff --git a/lib/review/lineinput.rb b/lib/review/lineinput.rb index 3408331b5..fdc5301c3 100644 --- a/lib/review/lineinput.rb +++ b/lib/review/lineinput.rb @@ -13,5 +13,21 @@ def skip_comment_lines end n end + + def gets + unless @buf.empty? + @lineno += 1 + return @buf.pop + end + return nil if @eof_p # to avoid ARGF blocking. + line = @input.gets + @eof_p = true unless line + @lineno += 1 + if line =~ /[\x00-\x08]/ || line =~ /[\x0b-\x0c]/ || line =~ /[\x0e-\x1f]/ + # accept 0x09: TAB, 0x0a: LF, 0x0d: CR + raise SyntaxError, "found invalid control-sequence character (#{sprintf('%#x', $&.codepoints[0])})." + end + line + end end end diff --git a/test/test_lineinput.rb b/test/test_lineinput.rb index 4dc46cc65..5a2c3f705 100644 --- a/test/test_lineinput.rb +++ b/test/test_lineinput.rb @@ -1,5 +1,6 @@ require 'test_helper' -require 'lineinput' +require 'review/lineinput' +require 'review/exception' require 'tempfile' require 'stringio' @@ -178,4 +179,18 @@ def test_until_terminator2 assert_equal ["abc\n", "def\n"], data assert_equal 3, li.lineno end + + def test_invalid_control_sequence + 0.upto(31) do |n| + content = n.chr + io = StringIO.new(content) + li = ReVIEW::LineInput.new(io) + if [9, 10, 13].include?(n) # TAB, LF, CR + assert_equal content, li.gets + else + e = assert_raise(ReVIEW::SyntaxError) { li.gets } + assert_match(/found invalid control/, e.message) + end + end + end end