Skip to content

Commit

Permalink
[rubygems/rubygems] Boundary check in Gem::StreamUI#choose_from_list
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu authored and matzbot committed Jul 21, 2023
1 parent 47c7c18 commit f602cb5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rubygems/user_interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def choose_from_list(question, list)
return nil, nil unless result

result = result.strip.to_i - 1
return nil, nil unless (0...list.size) === result
[list[result], result]
end

Expand Down
30 changes: 30 additions & 0 deletions test/rubygems/test_gem_stream_ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@ def test_choose_from_list_EOF
assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
end

def test_choose_from_list_0
@in.puts "0"
@in.rewind

result = @sui.choose_from_list "which one?", %w[foo bar]

assert_equal [nil, nil], result
assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
end

def test_choose_from_list_over
@in.puts "3"
@in.rewind

result = @sui.choose_from_list "which one?", %w[foo bar]

assert_equal [nil, nil], result
assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
end

def test_choose_from_list_negative
@in.puts "-1"
@in.rewind

result = @sui.choose_from_list "which one?", %w[foo bar]

assert_equal [nil, nil], result
assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
end

def test_progress_reporter_silent_nil
@cfg.verbose = nil
reporter = @sui.progress_reporter 10, "hi"
Expand Down

0 comments on commit f602cb5

Please sign in to comment.