Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-129573: Fix possible abort from non-string suggestions in calculate_suggestions/_Py_CalculateSuggestions #130997

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
7 changes: 7 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4740,6 +4740,13 @@ def test_suggestions_extension(self):
None
)

self.assertRaises(
TypeError,
_suggestions._generate_suggestions,
Copy link
Member

Choose a reason for hiding this comment

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

_suggestions._generate_suggestions() checks if all elements are strings before calling _Py_CalculateSuggestions(). So this test doesn't check changed code.

["hello", "world", 0, 1.1],
"hell",
)

# gh-131936: _generate_suggestions() doesn't accept list subclasses
class MyList(list):
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid possible abort when getting suggestions and there are non-string candidates.
4 changes: 4 additions & 0 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ _Py_CalculateSuggestions(PyObject *dir,
}
for (Py_ssize_t i = 0; i < dir_size; ++i) {
PyObject *item = PyList_GET_ITEM(dir, i);
if (!PyUnicode_Check(item)) {
PyMem_Free(buffer);
return NULL;
}
if (_PyUnicode_Equal(name, item)) {
continue;
}
Expand Down
Loading