https://github.com/python/cpython/commit/468e8dad7ee6002334e0f20a5f2db9315194658a
commit: 468e8dad7ee6002334e0f20a5f2db9315194658a
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: vstinner <[email protected]>
date: 2024-12-05T18:15:44+01:00
summary:

[3.13] gh-122431: Disallow negative values in `readline.append_history_file` 
(GH-122469) (#127641)

gh-122431: Disallow negative values in `readline.append_history_file` 
(GH-122469)
(cherry picked from commit 208b0fb645c0e14b0826c0014e74a0b70c58c9d6)

Co-authored-by: Peter Bierma <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-07-30-11-37-40.gh-issue-122431.lAzVtu.rst
M Lib/test/test_readline.py
M Modules/readline.c

diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
index 50e77cbbb6be13..8b8772c66ee654 100644
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -114,6 +114,14 @@ def test_write_read_append(self):
         # write_history_file can create the target
         readline.write_history_file(hfilename)
 
+        # Negative values should be disallowed
+        with self.assertRaises(ValueError):
+            readline.append_history_file(-42, hfilename)
+
+        # See gh-122431, using the minimum signed integer value caused a 
segfault
+        with self.assertRaises(ValueError):
+            readline.append_history_file(-2147483648, hfilename)
+
     def test_nonascii_history(self):
         readline.clear_history()
         try:
diff --git 
a/Misc/NEWS.d/next/Library/2024-07-30-11-37-40.gh-issue-122431.lAzVtu.rst 
b/Misc/NEWS.d/next/Library/2024-07-30-11-37-40.gh-issue-122431.lAzVtu.rst
new file mode 100644
index 00000000000000..16ad75792aefa2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-07-30-11-37-40.gh-issue-122431.lAzVtu.rst
@@ -0,0 +1 @@
+:func:`readline.append_history_file` now raises a :exc:`ValueError` when given 
a negative value.
diff --git a/Modules/readline.c b/Modules/readline.c
index 35655c70a4618f..7d1f703f7dbdde 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -351,6 +351,12 @@ readline_append_history_file_impl(PyObject *module, int 
nelements,
                                   PyObject *filename_obj)
 /*[clinic end generated code: output=5df06fc9da56e4e4 input=784b774db3a4b7c5]*/
 {
+    if (nelements < 0)
+    {
+        PyErr_SetString(PyExc_ValueError, "nelements must be positive");
+        return NULL;
+    }
+
     PyObject *filename_bytes;
     const char *filename;
     int err;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to