Skip to content

Commit 208b0fb

Browse files
pythongh-122431: Disallow negative values in readline.append_history_file (python#122469)
Co-authored-by: Victor Stinner <[email protected]>
1 parent 67b9a53 commit 208b0fb

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/test/test_readline.py

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ def test_write_read_append(self):
114114
# write_history_file can create the target
115115
readline.write_history_file(hfilename)
116116

117+
# Negative values should be disallowed
118+
with self.assertRaises(ValueError):
119+
readline.append_history_file(-42, hfilename)
120+
121+
# See gh-122431, using the minimum signed integer value caused a segfault
122+
with self.assertRaises(ValueError):
123+
readline.append_history_file(-2147483648, hfilename)
124+
117125
def test_nonascii_history(self):
118126
readline.clear_history()
119127
try:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`readline.append_history_file` now raises a :exc:`ValueError` when given a negative value.

Modules/readline.c

+6
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ readline_append_history_file_impl(PyObject *module, int nelements,
351351
PyObject *filename_obj)
352352
/*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
353353
{
354+
if (nelements < 0)
355+
{
356+
PyErr_SetString(PyExc_ValueError, "nelements must be positive");
357+
return NULL;
358+
}
359+
354360
PyObject *filename_bytes;
355361
const char *filename;
356362
int err;

0 commit comments

Comments
 (0)