Skip to content

bpo-30061: Check if PyObject_Size()/PySequence_Size()/PyMapping_Size()#1096

Merged
serhiy-storchaka merged 7 commits into
python:masterfrom
serhiy-storchaka:PyObject_Size-check-error
Apr 19, 2017
Merged

bpo-30061: Check if PyObject_Size()/PySequence_Size()/PyMapping_Size()#1096
serhiy-storchaka merged 7 commits into
python:masterfrom
serhiy-storchaka:PyObject_Size-check-error

Conversation

@serhiy-storchaka
Copy link
Copy Markdown
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.
Comment thread Modules/_winapi.c Outdated
if (!keys || !values)
goto error;

envsize = PyList_GET_SIZE(keys);
Copy link
Copy Markdown
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
Copy Markdown
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.

return NULL;
}
/* PySequence_Length() can fail */
if (PyErr_Occurred())
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Modules/posixmodule.c Outdated
return NULL;
}
sf.hdr_cnt = (int)i;
i = 0;
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Objects/setobject.c Outdated
* 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
Copy Markdown
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
Copy Markdown
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.

Comment thread Modules/posixmodule.c Outdated
return NULL;
}
sf.trl_cnt = (int)i;
i = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also here.

Comment thread Modules/_io/iobase.c Outdated

if (length > hint)
if (line_length < 0) {
Py_DECREF(result);
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Modules/_io/iobase.c

while (1) {
PyObject *line = PyIter_Next(it);
Py_ssize_t line_length;
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Modules/posixmodule.c
return NULL;
}
if (i > 0) {
sf.hdr_cnt = (int)i;
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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