Skip to content
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

Merged

Conversation

serhiy-storchaka
Copy link
Member

raised an error.

Replace them with using concrete types API that never fails if appropriate.

…) raised

an error.

Replace them with using concrete types API that never fails if appropriate.
keys = PyMapping_Keys(environment);
values = PyMapping_Values(environment);
if (!keys || !values)
goto error;

envsize = PyList_GET_SIZE(keys);
Copy link
Member

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?

Copy link
Member Author

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())
Copy link
Member

@zhangyangyu zhangyangyu Apr 14, 2017

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.

Copy link
Member Author

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.

return NULL;
}
sf.hdr_cnt = (int)i;
i = 0;
Copy link
Member

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.

Copy link
Member Author

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.

@@ -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);
Copy link
Member

@zhangyangyu zhangyangyu Apr 14, 2017

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);
}

Copy link
Member Author

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.

return NULL;
}
sf.trl_cnt = (int)i;
i = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Also here.


if (length > hint)
if (line_length < 0) {
Py_DECREF(result);
Copy link
Member

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.

Copy link
Member Author

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.

@@ -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;
Copy link
Member

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.

Copy link
Member Author

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;
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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?

@serhiy-storchaka serhiy-storchaka merged commit bf623ae into python:master Apr 19, 2017
@serhiy-storchaka serhiy-storchaka deleted the PyObject_Size-check-error branch April 19, 2017 17:03
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Apr 19, 2017
python#1096)

raised an error.

Replace them with using concrete types API that never fails if appropriate.

(cherry picked from commit bf623ae)
serhiy-storchaka added a commit that referenced this pull request Apr 19, 2017
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Apr 19, 2017
python#1096) (python#1180)

raised an error.

(cherry picked from commit bf623ae)
(cherry picked from commit 680fea4)
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Apr 19, 2017
…_Size() (pythonGH-1096) (pythonGH-1180)

raised an error.

(cherry picked from commit bf623ae).
(cherry picked from commit 680fea4)
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Apr 19, 2017
…_Size() (pythonGH-1096) (pythonGH-1180)

raised an error.

(cherry picked from commit bf623ae)
(cherry picked from commit 680fea4)
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Apr 19, 2017
…_Size() (pythonGH-1096) (pythonGH-1180)

raised an error.

(cherry picked from commit bf623ae).
(cherry picked from commit 680fea4)
serhiy-storchaka added a commit that referenced this pull request Apr 19, 2017
…_Size() (GH-1096) (GH-1180) (#1182)

raised an error.

(cherry picked from commit bf623ae)
(cherry picked from commit 680fea4)
serhiy-storchaka added a commit that referenced this pull request Apr 19, 2017
…_Size() (GH-1096) (GH-1180) (#1183)

raised an error.

(cherry picked from commit bf623ae)
(cherry picked from commit 680fea4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants