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.
| if (!keys || !values) | ||
| goto error; | ||
|
|
||
| envsize = PyList_GET_SIZE(keys); |
There was a problem hiding this comment.
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.
This is a separate issue. See bpo-28280.
I have replaced PyList_GET_ITEM by PySequence_Fast_GET_ITEM.
| return NULL; | ||
| } | ||
| /* PySequence_Length() can fail */ | ||
| if (PyErr_Occurred()) |
There was a problem hiding this comment.
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.
This would be a behavior change. Since I'm going to backport most of these changes I prefer to keep the current behavior.
| return NULL; | ||
| } | ||
| sf.hdr_cnt = (int)i; | ||
| i = 0; |
There was a problem hiding this comment.
This i = 0 looks not needed.
There was a problem hiding this comment.
Indeed. if sf.hdr_cnt > 0 is false, i can only be 0.
| * 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.
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);
}
| return NULL; | ||
| } | ||
| sf.trl_cnt = (int)i; | ||
| i = 0; |
|
|
||
| if (length > hint) | ||
| if (line_length < 0) { | ||
| Py_DECREF(result); |
There was a problem hiding this comment.
Should be goto error; here.
There was a problem hiding this comment.
Good catch. Now we need to clear the iterator too.
|
|
||
| while (1) { | ||
| PyObject *line = PyIter_Next(it); | ||
| Py_ssize_t line_length; |
There was a problem hiding this comment.
Better move this line before the above. IIRC C89 doesn't allow mixing variable declaration and assignment.
There was a problem hiding this comment.
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.
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.
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.
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.