Skip to content

Commit e6ea330

Browse files
committed
bpo-29854: Add failing test that crash readline
readline segfaults on input() if the number of items in the history file is equal or more to history size * 2. This issue affects only GNU readline. When using libedit emulation system history size option does not work.
1 parent 61cd9a1 commit e6ea330

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

Lib/test/test_readline.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import sys
1010
import tempfile
1111
import unittest
12-
from test.support import import_module, unlink, TESTFN
12+
from test.support import import_module, unlink, temp_dir, TESTFN
1313
from test.support.script_helper import assert_python_ok
1414

1515
# Skip tests if there is no readline module
@@ -210,6 +210,44 @@ def display(substitution, matches, longest_match_length):
210210
self.assertIn(b"result " + expected + b"\r\n", output)
211211
self.assertIn(b"history " + expected + b"\r\n", output)
212212

213+
@unittest.skipIf(is_editline,
214+
"editline history size configuration is broken")
215+
@unittest.expectedFailure
216+
def test_history_size(self):
217+
history_size = 10
218+
with temp_dir() as test_dir:
219+
inputrc = os.path.join(test_dir, "inputrc")
220+
with open(inputrc, "wb") as f:
221+
f.write(b"set history-size %d\n" % history_size)
222+
223+
history_file = os.path.join(test_dir, "history")
224+
with open(history_file, "wb") as f:
225+
# history_size * 2 items crashes readline
226+
data = b"".join(b"item %d\n" % i
227+
for i in range(history_size * 2))
228+
f.write(data)
229+
230+
script = """
231+
import os
232+
import readline
233+
234+
history_file = os.environ["HISTORY_FILE"]
235+
readline.read_history_file(history_file)
236+
input()
237+
readline.write_history_file(history_file)
238+
"""
239+
240+
env = dict(os.environ)
241+
env["INPUTRC"] = inputrc
242+
env["HISTORY_FILE"] = history_file
243+
244+
run_pty(script, input=b"last input\r", env=env)
245+
246+
with open(history_file, "rb") as f:
247+
lines = f.readlines()
248+
self.assertEqual(len(lines), history_size)
249+
self.assertEqual(lines[-1].strip(), b"last input")
250+
213251

214252
def run_pty(script, input=b"dummy input\r", env=None):
215253
pty = import_module('pty')

0 commit comments

Comments
 (0)