Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.13] gh-142829: Fix use-after-free inContext.__eq__ via re-entrantContextVar.set (GH-142905)#143871

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
30 changes: 30 additions & 0 deletionsLib/test/test_context.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -370,6 +370,36 @@ def sub(num):
tp.shutdown()
self.assertEqual(results, list(range(10)))

def test_context_eq_reentrant_contextvar_set(self):
var = contextvars.ContextVar("v")
ctx1 = contextvars.Context()
ctx2 = contextvars.Context()

class ReentrantEq:
def __eq__(self, other):
ctx1.run(lambda: var.set(object()))
return True

ctx1.run(var.set, ReentrantEq())
ctx2.run(var.set, object())
ctx1 == ctx2

def test_context_eq_reentrant_contextvar_set_in_hash(self):
var = contextvars.ContextVar("v")
ctx1 = contextvars.Context()
ctx2 = contextvars.Context()

class ReentrantHash:
def __hash__(self):
ctx1.run(lambda: var.set(object()))
return 0
def __eq__(self, other):
return isinstance(other, ReentrantHash)

ctx1.run(var.set, ReentrantHash())
ctx2.run(var.set, ReentrantHash())
ctx1 == ctx2


# HAMT Tests

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
Fix a use-after-free crash in :class:`contextvars.Context` comparison when a
custom ``__eq__`` method modifies the context via
:meth:`~contextvars.ContextVar.set`.
27 changes: 22 additions & 5 deletionsPython/hamt.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2368,6 +2368,10 @@ _PyHamt_Eq(PyHamtObject *v, PyHamtObject *w)
return 0;
}

Py_INCREF(v);
Py_INCREF(w);

int res = 1;
PyHamtIteratorState iter;
hamt_iter_t iter_res;
hamt_find_t find_res;
Expand All@@ -2383,25 +2387,38 @@ _PyHamt_Eq(PyHamtObject *v, PyHamtObject *w)
find_res = hamt_find(w, v_key, &w_val);
switch (find_res) {
case F_ERROR:
return -1;
res = -1;
goto done;

case F_NOT_FOUND:
return 0;
res = 0;
goto done;

case F_FOUND: {
Py_INCREF(v_key);
Py_INCREF(v_val);
Py_INCREF(w_val);
int cmp = PyObject_RichCompareBool(v_val, w_val, Py_EQ);
Py_DECREF(v_key);
Py_DECREF(v_val);
Py_DECREF(w_val);
if (cmp < 0) {
return -1;
res = -1;
goto done;
}
if (cmp == 0) {
return 0;
res = 0;
goto done;
}
}
}
}
} while (iter_res != I_END);

return 1;
done:
Py_DECREF(v);
Py_DECREF(w);
return res;
}

Py_ssize_t
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp