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

gh-133546: Makere.Match a well-roundedSequence type#133549

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

Open
vberlier wants to merge12 commits intopython:main
base:main
Choose a base branch
Loading
fromvberlier:gh-133546
Open
Changes from1 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
PrevPrevious commit
NextNext commit
Add missing braces
  • Loading branch information
@vberlier
vberlier committedMay 11, 2025
commit70b73e4be41a0db5fb0e7411a20cd8970b4a28f1
21 changes: 14 additions & 7 deletionsModules/_sre/sre.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2657,8 +2657,9 @@ _sre_SRE_Match_index_impl(MatchObject *self, PyObject *value,

if (start < 0) {
start += self->groups;
if (start < 0)
if (start < 0) {
start = 0;
}
}
if (stop < 0) {
stop += self->groups;
Expand All@@ -2668,14 +2669,17 @@ _sre_SRE_Match_index_impl(MatchObject *self, PyObject *value,
}
for (i = start; i < stop; i++) {
PyObject* group = match_getslice_by_index(self, i, Py_None);
if (group == NULL)
if (group == NULL) {
return NULL;
}
int cmp = PyObject_RichCompareBool(group, value, Py_EQ);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm pretty sure we need to incref the value in case the__bool__ releases a reference. cc@picnixz who fixed a bunch of issues related to that.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not sure if this is what you're talking about, but I think this is already handled byPyObject_RichCompareBool out of the box. It's a wrapper aroundPyObject_RichCompare that unwraps and decref the returned object.

Copy link
Member

@picnixzpicnixzMay 15, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It's not about the returned object, it's about the inputs.PyObject_RichCompareBool andPyObject_RichCompare can call arbitrary Python code in general, so if such arbitrary code actually releases or changes their inputs, we may end up with what we call a "use-after-free" (and usually it leads to a SIGSEV, but in other occurrences it can be a security issue).

In order to determine whether there is a UAF or not, you need to check whethergroup orvalue can actually be obtained from pure Python code and be changed by the call to__eq__. In this case,value can be modified in place by its call, but_sre_SRE_Match_index_impl should have already held a strong reference on it, so there shouldn't be a UAF.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I see, thanks for the explanation!

Py_DECREF(group);
if (cmp > 0)
if (cmp > 0) {
return PyLong_FromSsize_t(i);
else if (cmp < 0)
}
else if (cmp < 0) {
return NULL;
}
}
PyErr_SetString(PyExc_ValueError, "match.index(x): x not in match");
return NULL;
Expand All@@ -2699,14 +2703,17 @@ _sre_SRE_Match_count_impl(MatchObject *self, PyObject *value)

for (i = 0; i < self->groups; i++) {
PyObject* group = match_getslice_by_index(self, i, Py_None);
if (group == NULL)
if (group == NULL) {
return NULL;
}
int cmp = PyObject_RichCompareBool(group, value, Py_EQ);
Py_DECREF(group);
if (cmp > 0)
if (cmp > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Now you can use anelse if here

count++;
else if (cmp < 0)
}
else if (cmp < 0) {
return NULL;
}
}
return PyLong_FromSsize_t(count);
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp