]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122431: Disallow negative values in `readline.append_history_file` (#122469)
authorPeter Bierma <zintensitydev@gmail.com>
Thu, 5 Dec 2024 16:07:38 +0000 (11:07 -0500)
committerGitHub <noreply@github.com>
Thu, 5 Dec 2024 16:07:38 +0000 (16:07 +0000)
Co-authored-by: Victor Stinner <vstinner@python.org>
Lib/test/test_readline.py
Misc/NEWS.d/next/Library/2024-07-30-11-37-40.gh-issue-122431.lAzVtu.rst [new file with mode: 0644]
Modules/readline.c

index 50e77cbbb6be13ad0a57b471d329b91ddc382199..8b8772c66ee65407e3c0656e5a314dbf4bd91825 100644 (file)
@@ -114,6 +114,14 @@ class TestHistoryManipulation (unittest.TestCase):
         # 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 (file)
index 0000000..16ad757
--- /dev/null
@@ -0,0 +1 @@
+:func:`readline.append_history_file` now raises a :exc:`ValueError` when given a negative value.
index 35655c70a4618fad8f1fdd2b12b7866388eee946..7d1f703f7dbddeaf152b18c55073307c83e34ab2 100644 (file)
@@ -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;