Skip to content

Commit fea968f

Browse files
authored
Fix segfault when a single arg is passed to setdefault (#1160)
1 parent 7770ff2 commit fea968f

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

CHANGES/1160.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a segmentation fault when calling :py:meth:`multidict.MultiDict.setdefault` with a single argument -- by :user:`bdraco`.

multidict/_multidict.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,10 @@ multidict_setdefault(MultiDictObject *self, PyObject *const *args,
573573
"key", &key, "default", &_default) < 0) {
574574
return NULL;
575575
}
576+
if (_default == NULL) {
577+
// fixme, _default is potentially dangerous borrowed ref here
578+
_default = Py_None;
579+
}
576580
return pair_list_set_default(&self->pairs, key, _default);
577581
}
578582

tests/test_mutable_multidict.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ def test_set_default(
156156
assert "otherkey" in d
157157
assert "three" == d["otherkey"]
158158

159+
def test_set_default_single_arg(
160+
self,
161+
case_sensitive_multidict_class: type[MultiDict[str]],
162+
) -> None:
163+
d = case_sensitive_multidict_class([("key", "one"), ("key", "two")], foo="bar")
164+
assert d.setdefault("key") == "one" # type: ignore[call-arg]
165+
assert d.setdefault("noexist") is None # type: ignore[call-arg]
166+
assert d["noexist"] is None
167+
159168
def test_popitem(
160169
self,
161170
case_sensitive_multidict_class: type[MultiDict[str]],

0 commit comments

Comments
 (0)