8000 [3.12] gh-122431: Disallow negative values in `readline.append_histor… · python/cpython@a65475f · GitHub
[go: up one dir, main page]

Skip to content

Commit a65475f

Browse files
miss-islingtonZeroIntensityvstinner
authored
[3.12] gh-122431: Disallow negative values in readline.append_history_file (GH-122469) (#127642)
gh-122431: Disallow negative values in `readline.append_history_file` (GH-122469) (cherry picked from commit 208b0fb) Co-authored-by: Peter Bierma <zintensitydev@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 288d6d0 commit a65475f

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/test/test_readline.py

Lines changed: 8 additions & 0 deletions
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:
Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ readline_append_history_file_impl(PyObject *module, int nelements,
336336
PyObject *filename_obj)
337337
/*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
338338
{
339+
if (nelements < 0)
340+
{
341+
PyErr_SetString(PyExc_ValueError, "nelements must be positive");
342+
return NULL;
343+
}
344+
339345
PyObject *filename_bytes;
340346
const char *filename;
341347
int err;

0 commit comments

Comments
 (0)
0