Skip to content

Commit 5044eb2

Browse files
committed
Suppress crash when bignum is set to SAVE_HISTORY
1 parent 82efd37 commit 5044eb2

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/irb/ext/save-history.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ def save_history
109109

110110
open(history_file, "w:#{IRB.conf[:LC_MESSAGES].encoding}", 0600) do |f|
111111
hist = history.map{ |l| l.split("\n").join("\\\n") }
112-
f.puts(hist[-num..-1] || hist)
112+
begin
113+
hist = hist.last(num) if hist.size > num
114+
rescue RangeError # bignum too big to convert into `long'
115+
# Do nothing because the bignum should be treated as inifinity
116+
end
117+
f.puts(hist)
113118
end
114119
end
115120
end

test/irb/test_history.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ def test_history_save_100
5252
HISTORY_FILE
5353
end
5454

55+
def test_history_save_bignum
56+
result_output, result_history_file = launch_irb_with_irbrc_and_irb_history(<<~IRBRC, <<~IRB_HISTORY) do |stdin|
57+
IRB.conf[:USE_READLINE] = true
58+
IRB.conf[:SAVE_HISTORY] = 10 ** 19
59+
IRBRC
60+
1
61+
2
62+
3
63+
4
64+
IRB_HISTORY
65+
stdin.write("5\nexit\n")
66+
end
67+
68+
assert_equal(<<~HISTORY_FILE, result_history_file)
69+
1
70+
2
71+
3
72+
4
73+
5
74+
exit
75+
HISTORY_FILE
76+
end
77+
5578
private
5679

5780
def launch_irb_with_irbrc_and_irb_history(irbrc, irb_history)

0 commit comments

Comments
 (0)