8000 GH-100942: Fix incorrect cast in property_copy(). by rhettinger · Pull Request #100965 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-100942: Fix incorrect cast in property_copy(). #100965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
17 changes: 17 additions & 0 deletions Lib/test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ def test_property_set_name_incorrect_args(self):
):
p.__set_name__(*([0] * i))

def test_property_setname_on_property_subclass(self):
# https://github.com/python/cpython/issues/100942
# Copy was setting the name field without first
# verifying that the copy was an actual property
# instance. As a result, the code below was
# causing a segfault.

class pro(property):
def __new__(typ, *args, **kwargs):
return "abcdef"

class A:
pass

p = property.__new__(pro)
p.__set_name__(A, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not really required. Segfault happens and without it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to leave it because it covers an additional code path that could fail and it is an accurate record of how the issue was discovered.

np = p.getter(lambda self: 1)

# Issue 5890: subclasses of property do not preserve method __doc__ strings
class PropertySub(property):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed segfault in property.getter/setter/deleter that occurred when a property
subclass overrode the ``__new__`` method to return a non-property instance.
4 changes: 3 additions & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,9 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
if (new == NULL)
return NULL;

Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
if (PyObject_TypeCheck((new), &PyProperty_Type)) {
Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
}
return new;
}

Expand Down
0