Skip to content

Commit

Permalink
Disallow invalid tab widths for -w
Browse files Browse the repository at this point in the history
Previously RDoc::Options used OptionParser::DecimalInteger instead of
Integer for restricting values for the tab width.  Unfortunately the
validator in RDoc uses #to_i to validate the input instead of
Kernel#Integer.  This means no exception is raised for invalid input
such as "-w=3".

Now Integer is used which has the strict validation but also allows
octal, hexadecimal and binary numbers.

Bug reported by Charles Hixson.
  • Loading branch information
drbrain committed Sep 4, 2013
1 parent e525b1b commit bb45ec8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions History.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
* Fixed lexing of escaped characters in strings which could cause
duplication of the final characters in source code view. Bug #252 by Mike
Stok.
* Disallow invalid tab widths for -w option. Bug reported by Charles
Hixson.

=== 4.0.1 / 2013-03-27

Expand Down
4 changes: 3 additions & 1 deletion lib/rdoc/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,10 @@ def parse argv

opt.separator nil

opt.on("--tab-width=WIDTH", "-w", OptionParser::DecimalInteger,
opt.on("--tab-width=WIDTH", "-w", Integer,
"Set the width of tab characters.") do |value|
raise OptionParser::InvalidArgument,
"#{value} is an invalid tab width" if value <= 0
@tab_width = value
end

Expand Down
20 changes: 20 additions & 0 deletions test/test_rdoc_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,26 @@ def test_parse_root
assert_includes @options.rdoc_include, @options.root.to_s
end

def test_parse_tab_width
@options.parse %w[--tab-width=1]
assert_equal 1, @options.tab_width

@options.parse %w[-w2]
assert_equal 2, @options.tab_width

_, err = capture_io do
@options.parse %w[-w=2]
end

assert_match 'invalid options', err

_, err = capture_io do
@options.parse %w[-w0]
end

assert_match 'invalid options', err
end

def test_parse_template
out, err = capture_io do
@options.parse %w[--template darkfish]
Expand Down

0 comments on commit bb45ec8

Please sign in to comment.