-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-30061: Check if PyObject_Size()/PySequence_Size()/PyMapping_Size() #1096
bpo-30061: Check if PyObject_Size()/PySequence_Size()/PyMapping_Size() #1096
Conversation
…) raised an error. Replace them with using concrete types API that never fails if appropriate.
Modules/_winapi.c
Outdated
keys = PyMapping_Keys(environment); | ||
values = PyMapping_Values(environment); | ||
if (!keys || !values) | ||
goto error; | ||
|
||
envsize = PyList_GET_SIZE(keys); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this function, PyList_GET_ITEM
is used. But PyMapping_Keys/Values
could also return a tuple. Is it possible here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a separate issue. See bpo-28280.
I have replaced PyList_GET_ITEM
by PySequence_Fast_GET_ITEM
.
@@ -1670,6 +1670,9 @@ _multibytecodec_MultibyteStreamWriter_writelines(MultibyteStreamWriterObject *se | |||
if (r == -1) | |||
return NULL; | |||
} | |||
/* PySequence_Length() can fail */ | |||
if (PyErr_Occurred()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe cache the result of PySequence_Length
. It's not free. And even lines
is changed the behaviour is not bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a behavior change. Since I'm going to backport most of these changes I prefer to keep the current behavior.
Modules/posixmodule.c
Outdated
return NULL; | ||
} | ||
sf.hdr_cnt = (int)i; | ||
i = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This i = 0
looks not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. if sf.hdr_cnt > 0
is false, i
can only be 0.
Objects/setobject.c
Outdated
@@ -1559,7 +1559,9 @@ set_difference(PySetObject *so, PyObject *other) | |||
|
|||
/* If len(so) much more than len(other), it's more efficient to simply copy | |||
* so and then iterate other looking for common elements. */ | |||
if ((PySet_GET_SIZE(so) >> 2) > PyObject_Size(other)) { | |||
other_size = PyDict_CheckExact(other) ? PyDict_GET_SIZE(other) | |||
: PySet_GET_SIZE(other); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer:
if (PyAnySet_Check(other) {
other_size = PySet_GET_SIZE(other);
}
else if (PyDict_CheckExact(other)) {
other_size = PyDict_GET_SIZE(other);
}
else {
return set_copy_and_difference(so, other);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this too. But let ask @rhettinger.
Modules/posixmodule.c
Outdated
return NULL; | ||
} | ||
sf.trl_cnt = (int)i; | ||
i = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here.
Modules/_io/iobase.c
Outdated
|
||
if (length > hint) | ||
if (line_length < 0) { | ||
Py_DECREF(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be goto error;
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Now we need to clear the iterator too.
Modules/_io/iobase.c
Outdated
@@ -671,6 +672,7 @@ _io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint) | |||
|
|||
while (1) { | |||
PyObject *line = PyIter_Next(it); | |||
Py_ssize_t line_length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better move this line before the above. IIRC C89 doesn't allow mixing variable declaration and assignment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no mixing variable declaration and assignment. PyObject *line = PyIter_Next(it);
is not an assignment, it is an initialization. But I'll swap the lines because this makes line
initialization closer to checking it's value.
return NULL; | ||
} | ||
if (i > 0) { | ||
sf.hdr_cnt = (int)i; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-scanning the patch I get a question here. Before PySequence_Size(headers)
returns 0 and sf.hdr_cnt
is set. Now when it returns 0, sf.hdr_cnt
is uninitialized. Codes below still use sf
but I don't know sf.hdr_cnt
matters or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't differ from the case when arguments headers
or trailers
are not specified. sf.headers
and sf.trailers
are set to NULL and according to the documentation this should be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean ret = sendfile(in, out, offset, &sbytes, &sf, flags)
. Is there any guarantee this function won't depend on sf.hdr_cnt
? Could it be just a for loop and then cause dereferencing a NULL pointer?
python#1096) raised an error. Replace them with using concrete types API that never fails if appropriate. (cherry picked from commit bf623ae)
python#1096) (python#1180) raised an error. (cherry picked from commit bf623ae) (cherry picked from commit 680fea4)
…_Size() (pythonGH-1096) (pythonGH-1180) raised an error. (cherry picked from commit bf623ae). (cherry picked from commit 680fea4)
…_Size() (pythonGH-1096) (pythonGH-1180) raised an error. (cherry picked from commit bf623ae) (cherry picked from commit 680fea4)
…_Size() (pythonGH-1096) (pythonGH-1180) raised an error. (cherry picked from commit bf623ae). (cherry picked from commit 680fea4)
raised an error.
Replace them with using concrete types API that never fails if appropriate.