Skip to content
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
2 changes: 2 additions & 0 deletions CHANGES/1310.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A segmentation fault that could be triggered when getting an item is now fixed
-- by :user:`Vizonex`.
18 changes: 16 additions & 2 deletions multidict/_multidict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,22 @@ def _parse_args(
f"multidict update sequence element #{pos} "
f"has length {len(item)}; 2 is required"
)
identity = identity_func(item[0])
yield _Entry(hash(identity), identity, item[0], item[1])
try:
key = item[0]
except Exception as exc:
raise ValueError(
f"multidict update sequence element #{pos}'s "
f"key could not be fetched"
) from exc
try:
value = item[1]
except Exception as exc:
raise ValueError(
f"multidict update sequence element #{pos}'s "
f"value could not be fetched"
) from exc
identity = identity_func(key)
yield _Entry(hash(identity), identity, key, value)
else:
yield len(kwargs)
for key, value in kwargs.items():
Expand Down
6 changes: 3 additions & 3 deletions multidict/_multilib/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -1470,8 +1470,8 @@ _err_cannot_fetch(Py_ssize_t i, const char *name)
PyErr_Format(PyExc_ValueError,
"multidict update sequence element #%zd's "
"%s could not be fetched",
name,
i);
i,
name);
}

static int
Expand Down Expand Up @@ -1507,11 +1507,11 @@ _md_parse_item(Py_ssize_t i, PyObject *item, PyObject **pkey,
goto fail;
}
*pkey = PySequence_ITEM(item, 0);
*pvalue = PySequence_ITEM(item, 1);
if (*pkey == NULL) {
_err_cannot_fetch(i, "key");
goto fail;
}
*pvalue = PySequence_ITEM(item, 1);
if (*pvalue == NULL) {
_err_cannot_fetch(i, "value");
goto fail;
Expand Down
36 changes: 36 additions & 0 deletions tests/test_multidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,42 @@ def test_cannot_create_from_unaccepted(
):
cls([(1, 2, 3)]) # type: ignore[call-arg]

def test_cannot_create_from_item_with_failing_getitem(
self,
cls: type[MutableMultiMapping[str]],
) -> None:
class BadItem:
def __len__(self) -> int:
return 2

def __getitem__(self, i: int) -> object:
Comment thread
bdraco marked this conversation as resolved.
Dismissed
raise RuntimeError("intentional getitem failure")

with pytest.raises(
ValueError,
match=r"^multidict update sequence element #0's key could not be fetched$",
):
cls([BadItem()]) # type: ignore[call-arg]

def test_cannot_create_from_item_with_failing_getitem_value(
self,
cls: type[MutableMultiMapping[str]],
) -> None:
class BadValueItem:
def __len__(self) -> int:
return 2

def __getitem__(self, i: int) -> object:
Comment thread
bdraco marked this conversation as resolved.
Dismissed
if i == 0:
return "key"
raise RuntimeError("intentional getitem failure")

with pytest.raises(
ValueError,
match=r"^multidict update sequence element #0's value could not be fetched$",
):
cls([BadValueItem()]) # type: ignore[call-arg]

def test_keys_is_set_less(self, cls: type[MultiDict[str]]) -> None:
d = cls([("key", "value1")])

Expand Down
Loading