Skip to content

Commit

Permalink
check hidden (unwanted) ascii control sequence character. Closes: #1596
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed Nov 15, 2020
1 parent fd6d4da commit f28f3c4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/review/book/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions lib/review/lineinput.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 16 additions & 1 deletion test/test_lineinput.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'test_helper'
require 'lineinput'
require 'review/lineinput'
require 'review/exception'
require 'tempfile'
require 'stringio'

Expand Down Expand Up @@ -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

0 comments on commit f28f3c4

Please sign in to comment.