8000 gh-106307: Fix PyMapping_GetOptionalItemString() by serhiy-storchaka · Pull Request #108797 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106307: Fix PyMapping_GetOptionalItemString() #108797

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
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-106307: Fix PyMapping_GetOptionalItemString()
The resulting pointer was not set to NULL if the creation of a temporary
string object was failed.

The tests were also missed due to oversight.
  • Loading branch information
serhiy-storchaka committed Sep 2, 2023
commit 0d54c803babe4512c1f5af6658ac15c116174e10
37 changes: 37 additions & 0 deletions Lib/test/test_capi/test_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,43 @@ def test_mapping_getitemstring(self):
self.assertRaises(TypeError, getitemstring, [], b'a')
self.assertRaises(SystemError, getitemstring, NULL, b'a')

def tes 8000 t_mapping_getoptionalitem(self):
getitem = _testcapi.mapping_getoptionalitem
dct = {'a': 1, '\U0001f40d': 2}
self.assertEqual(getitem(dct, 'a'), 1)
self.assertEqual(getitem(dct, 'b'), KeyError)
self.assertEqual(getitem(dct, '\U0001f40d'), 2)

dct2 = ProxyGetItem(dct)
self.assertEqual(getitem(dct2, 'a'), 1)
self.assertEqual(getitem(dct2, 'b'), KeyError)

self.assertEqual(getitem(['a', 'b', 'c'], 1), 'b')

self.assertRaises(TypeError, getitem, 42, 'a')
self.assertRaises(TypeError, getitem, {}, []) # unhashable
self.assertRaises(IndexError, getitem, [], 1)
self.assertRaises(TypeError, getitem, [], 'a')
# CRASHES getitem({}, NULL)
# CRASHES getitem(NULL, 'a')

def test_mapping_getoptionalitemstring(self):
getitemstring = _testcapi.mapping_getoptionalitemstring
dct = {'a': 1, '\U0001f40d': 2}
self.assertEqual(getitemstring(dct, b'a'), 1)
self.assertEqual(getitemstring(dct, b'b'), KeyError)
self.assertEqual(getitemstring(dct, '\U0001f40d'.encode()), 2)

dct2 = ProxyGetItem(dct)
self.assertEqual(getitemstring(dct2, b'a'), 1)
self.assertEqual(getitemstring(dct2, b'b'), KeyError)

self.assertRaises(TypeError, getitemstring, 42, b'a')
self.assertRaises(UnicodeDecodeError, getitemstring, {}, b'\xff')
self.assertRaises(SystemError, getitemstring, {}, NULL)
self.assertRaises(TypeError, getitemstring, [], b'a')
# CRASHES getitemstring(NULL, b'a')

def test_mapping_haskey(self):
haskey = _testcapi.mapping_haskey
dct = {'a': 1, '\U0001f40d': 2}
Expand Down
8 changes: 4 additions & 4 deletions Modules/_testcapi/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object_getattrstring(PyObject *self, PyObject *args)
static PyObject *
object_getoptionalattr(PyObject *self, PyObject *args)
{
PyObject *obj, *attr_name, *value;
PyObject *obj, *attr_name, *value = UNINITIALIZED_PTR;
if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
return NULL;
}
Expand All @@ -57,7 +57,7 @@ object_getoptionalattr(PyObject *self, PyObject *args)
static PyObject *
object_getoptionalattrstring(PyObject *self, PyObject *args)
{
PyObject *obj, *value;
PyObject *obj, *value = UNINITIALIZED_PTR;
const char *attr_name;
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
Expand Down Expand Up @@ -207,7 +207,7 @@ mapping_getitemstring(PyObject *self, PyObject *args)
static PyObject *
mapping_getoptionalitem(PyObject *self, PyObject *args)
{
PyObject *obj, *attr_name, *value;
PyObject *obj, *attr_name, *value = UNINITIALIZED_PTR;
if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
return NULL;
}
Expand All @@ -232,7 +232,7 @@ mapping_getoptionalitem(PyObject *self, PyObject *args)
static PyObject *
mapping_getoptionalitemstring(PyObject *self, PyObject *args)
{
PyObject *obj, *value;
PyObject *obj, *value = UNINITIALIZED_PTR;
const char *attr_name;
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
Expand Down
4 changes: 2 additions & 2 deletions Modules/_testcapi/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ dict_getitemwitherror(PyObject *self, PyObject *args)
static PyObject *
dict_getitemref(PyObject *self, PyObject *args)
{
PyObject *obj, *attr_name, *value;
PyObject *obj, *attr_name, *value = UNINITIALIZED_PTR;
if (!PyArg_ParseTuple(args, "OO", &obj, &attr_name)) {
return NULL;
}
Expand All @@ -164,7 +164,7 @@ dict_getitemref(PyObject *self, PyObject *args)
static PyObject *
dict_getitemstringref(PyObject *self, PyObject *args)
{
PyObject *obj, *value;
PyObject *obj, *value = UNINITIALIZED_PTR;
const char *attr_name;
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "Oz#", &obj, &attr_name, &size)) {
Expand Down
2 changes: 2 additions & 0 deletions Modules/_testcapi/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@
assert(!PyErr_Occurred()); \
return PyLong_FromSsize_t(_ret); \
} while (0)

#define UNINITIALIZED_PTR ((void *)"uninitialized")
2 changes: 2 additions & 0 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -2394,11 +2394,13 @@ int
PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
{
if (key == NULL) {
*result = NULL;
null_error();
return -1;
}
PyObject *okey = PyUnicode_FromString(key);
if (okey == NULL) {
*result = NULL;
return -1;
}
int rc = PyMapping_GetOptionalItem(obj, okey, result);
Expand Down
0