]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-126105: Fix crash in `ast` module, when `._fields` is deleted (GH-126115...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 29 Oct 2024 16:07:32 +0000 (17:07 +0100)
committerGitHub <noreply@github.com>
Tue, 29 Oct 2024 16:07:32 +0000 (16:07 +0000)
gh-126105: Fix crash in `ast` module, when `._fields` is deleted (GH-126115)

Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
(cherry picked from commit b2eaa75b176e07730215d76d8dce4d63fb493391)

Co-authored-by: sobolevn <mail@sobolevn.me>
Lib/test/test_ast/test_ast.py
Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst [new file with mode: 0644]
Parser/asdl_c.py
Python/Python-ast.c

index b789b721c5256da4d9580e0efa56610106a5fd1e..0bb74582f3ecdfe866099f1188185b94c9f196c8 100644 (file)
@@ -72,6 +72,23 @@ class AST_Tests(unittest.TestCase):
             # "ast.AST constructor takes 0 positional arguments"
             ast.AST(2)
 
+    def test_AST_fields_NULL_check(self):
+        # See: https://github.com/python/cpython/issues/126105
+        old_value = ast.AST._fields
+
+        def cleanup():
+            ast.AST._fields = old_value
+        self.addCleanup(cleanup)
+
+        del ast.AST._fields
+
+        msg = "type object 'ast.AST' has no attribute '_fields'"
+        # Both examples used to crash:
+        with self.assertRaisesRegex(AttributeError, msg):
+            ast.AST(arg1=123)
+        with self.assertRaisesRegex(AttributeError, msg):
+            ast.AST()
+
     def test_AST_garbage_collection(self):
         class X:
             pass
diff --git a/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst b/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst
new file mode 100644 (file)
index 0000000..547eb3a
--- /dev/null
@@ -0,0 +1 @@
+Fix a crash in :mod:`ast` when the :attr:`ast.AST._fields` attribute is deleted.
index ed2ea16284d0e7470a39d8822747245618a59af0..936317b7ae6e0c7106902e35224de5f59cd79ff0 100755 (executable)
@@ -881,19 +881,17 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
     Py_ssize_t i, numfields = 0;
     int res = -1;
     PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL;
-    if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
+
+    fields = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_fields);
+    if (fields == NULL) {
         goto cleanup;
     }
-    if (fields) {
-        numfields = PySequence_Size(fields);
-        if (numfields == -1) {
-            goto cleanup;
-        }
-        remaining_fields = PySet_New(fields);
-    }
-    else {
-        remaining_fields = PySet_New(NULL);
+
+    numfields = PySequence_Size(fields);
+    if (numfields == -1) {
+        goto cleanup;
     }
+    remaining_fields = PySet_New(fields);
     if (remaining_fields == NULL) {
         goto cleanup;
     }
index 0f04c43b34cbb25c56831751b392f696ed0928bb..08ac2507d984d2a7343c8d99e51fdbd3dade939e 100644 (file)
@@ -5078,19 +5078,17 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
     Py_ssize_t i, numfields = 0;
     int res = -1;
     PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL;
-    if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
+
+    fields = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_fields);
+    if (fields == NULL) {
         goto cleanup;
     }
-    if (fields) {
-        numfields = PySequence_Size(fields);
-        if (numfields == -1) {
-            goto cleanup;
-        }
-        remaining_fields = PySet_New(fields);
-    }
-    else {
-        remaining_fields = PySet_New(NULL);
+
+    numfields = PySequence_Size(fields);
+    if (numfields == -1) {
+        goto cleanup;
     }
+    remaining_fields = PySet_New(fields);
     if (remaining_fields == NULL) {
         goto cleanup;
     }