Repository navigation
Fix segfault from an uninitialised MultiDict/CIMultiDict created via __new__ - #1378
Conversation
MultiDict.__new__(MultiDict) (or a subclass skipping super().__init__()) left md->state and md->keys as NULL, and the first method call dereferenced them. Give the container a tp_new that initialises the object to a valid empty state (state set, keys = the shared &empty_htkeys sentinel), with is_ci derived from the type so CIMultiDict stays case-insensitive. The empty sentinel is not allocated, so the md_init() done later by tp_init on normal construction does not leak. Adds a C-extension regression test over both container types and a subclass that skips super().__init__(). Note: the proxy types have the same latent issue (self->md left NULL) and will be addressed separately.
Vizonex
left a comment
There was a problem hiding this comment.
@devdanzin As long as the spelling mistakes are changed we can move forward with this PR.
|
@copilot resolve the merge conflicts in this pull request |
1 similar comment
|
@copilot resolve the merge conflicts in this pull request |
Confidence Score: 4/5Not ready to merge: directly constructed proxy objects can still terminate the Python process on first use. One actionable non-security failure remains in the C-extension proxy construction path. Files Needing Attention: multidict/_multidict.c
What T-Rex did
Comments Outside Diff (1)
Reviews (4): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile |
|
@aiolibsbot rr |
PR Review — Fix segfault from an uninitialised MultiDict/CIMultiDict created via
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1378 +/- ##
=======================================
Coverage 99.86% 99.86%
=======================================
Files 29 29
Lines 3763 3784 +21
Branches 280 281 +1
=======================================
+ Hits 3758 3779 +21
Misses 3 3
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Rebase with requested adjustmentsBranch Warning Force-push safety check — the rewrite of Commits whose patch changed in-flight and whose content could not be verified as preserved — check these survived:
Previous PR head: Changes applied
Not changed (and why)
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
|
It has no write access so commits land on its fork and have to be cherry picked or merged in by a human |
|
@aiolibsbot rv |
|
@devdanzin thanks! |
What do these changes do?
MultiDict.__new__(MultiDict)(or a subclass that skipssuper().__init__()) leftmd->stateandmd->keysas NULL, and the first method call dereferenced them and segfaulted. The container now has atp_newthat initialises the object to a valid empty state (stateset,keys= the shared&empty_htkeyssentinel), withis_ciderived from the type soCIMultiDictstays case-insensitive.The sentinel is not heap-allocated, so the
md_init()thattp_initruns on normal construction doesnot leak.
Scope: this fixes the container types. The proxy types (
MultiDictProxy/CIMultiDictProxy) have thesame latent issue (
self->mdleft NULL by__new__) but need a different fix (moving argumentvalidation into
tp_new); I'll send that as a follow-up rather than fold a construction-protocol changein here. The pure-Python backend raises
AttributeErrorfor the same__new__-without-__init__case(no crash); aligning it to also yield a valid empty mapping can be a small follow-up if you'd prefer
exact parity.
Are there changes in behavior for the user?
Yes (mild):
MultiDict.__new__(MultiDict)and subclasses that skipsuper().__init__()now behave as avalid empty mapping instead of crashing.
Is it a substantial burden for the maintainers to support this?
No. One
tp_newfunction shared by both container types.Related issue number
Generalises the point fixes #1159 (
setdefault) and #1161/#1163 (view types) to the containers.Surfaced by the analysis report #1306.
Checklist
test_new_without_init_is_valid_empty, both container types + a subclass)CONTRIBUTORS.txtin this repoCHANGES/folder