Skip to content

Commit

Permalink
[ruby/irb] Suppress crash when bignum is set to SAVE_HISTORY
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Aug 18, 2020
1 parent 1359da6 commit ef498a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/irb/ext/save-history.rb
Expand Up @@ -109,7 +109,12 @@ def save_history

open(history_file, "w:#{IRB.conf[:LC_MESSAGES].encoding}", 0600) do |f|
hist = history.map{ |l| l.split("\n").join("\\\n") }
f.puts(hist[-num..-1] || hist)
begin
hist = hist.last(num) if hist.size > num
rescue RangeError # bignum too big to convert into `long'
# Do nothing because the bignum should be treated as inifinity
end
f.puts(hist)
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions test/irb/test_history.rb
Expand Up @@ -52,6 +52,29 @@ def test_history_save_100
HISTORY_FILE
end

def test_history_save_bignum
result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin|
IRB.conf[:USE_READLINE] = true
IRB.conf[:SAVE_HISTORY] = 10 ** 19
IRBRC
1
2
3
4
IRB_HISTORY
stdin.write("5\nexit\n")
end

assert_equal(<<~HISTORY_FILE, result_history_file)
1
2
3
4
5
exit
HISTORY_FILE
end

private

def launch_irb_with_irbrc_and_irb_history(irbrc, irb_history)
Expand Down

0 comments on commit ef498a0

Please sign in to comment.