classification
Title: checks whether PyArg_ParseTuple returned a negative int
Type: crash Stage: resolved
Components: Extension Modules, IO Versions: Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Oren Milman, benjamin.peterson, serhiy.storchaka, stutzbach
Priority: normal Keywords: easy (C)

Created on 2017-08-21 08:33 by Oren Milman, last changed 2017-08-29 16:18 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3171 merged Oren Milman, 2017-08-21 19:00
PR 3233 merged Oren Milman, 2017-08-29 10:54
PR 3235 merged Oren Milman, 2017-08-29 15:05
Messages (7)
msg300613 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-21 08:33
according to the docs (https://docs.python.org/3.7/c-api/arg.html?highlight=pyarg_parsetuple#c.PyArg_ParseTuple), PyArg_ParseTuple
returns true for success or false for failure. I also looked at the
implementation in Python/getargs.c, and it seems that indeed PyArg_ParseTuple
can return only 0 or 1.

however, in some places in the codebase, there are checks whether
PyArg_ParseTuple returned a negative int.
I found a bunch in Modules/_testcapimodule.c, and one in Modules/_io/textio.c
in textiowrapper_read_chunk. (hopefully i haven't missed other places.)

this code crashes because of the check in textiowrapper_read_chunk:
import codecs
import _io
class MyDec():
    def getstate(self):
        return 420
class MyDecWrapper():
    def __call__(self, blabla):
        return MyDec()
quopri = codecs.lookup("quopri")
quopri._is_text_encoding = True
quopri.incrementaldecoder = MyDecWrapper()
t = _io.TextIOWrapper(_io.BytesIO(b'aaaaaa'),
                      newline='\n', encoding="quopri")
t.read(42)


I guess all of these checks should be changed to check whether PyArg_ParseTuple
returned 0.

also, before this specific call to PyArg_ParseTuple in textiowrapper_read_chunk,
we should check whether 'state' is a tuple.
Moreover, this call shouldn't produce a wrong error message, as explained in
#28261.
msg300616 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-21 09:13
Do you want to provide a PR Oren?
msg300618 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-21 10:12
yes, soon.
(just wanted to hear your opinion before doing that.)
msg300977 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-29 08:58
New changeset ba7d7365215d791025d1efd25393c91404f2cfc8 by Serhiy Storchaka (Oren Milman) in branch 'master':
bpo-31243: Fixed PyArg_ParseTuple failure checks. (#3171)
https://github.com/python/cpython/commit/ba7d7365215d791025d1efd25393c91404f2cfc8
msg300982 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-29 12:43
New changeset c7750c2a3af014a5b742d0159d8957ea95b6c221 by Serhiy Storchaka (Oren Milman) in branch '3.6':
[3.6] bpo-31243: Fixed PyArg_ParseTuple failure checks. (GH-3171) (#3233)
https://github.com/python/cpython/commit/c7750c2a3af014a5b742d0159d8957ea95b6c221
msg300993 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-29 16:16
New changeset 20958e6d91d11a80d6c664ce6346791259b1d193 by Serhiy Storchaka (Oren Milman) in branch '2.7':
[2.7] bpo-31243: Fixed PyArg_ParseTuple failure checks. (GH-3171) (#3235)
https://github.com/python/cpython/commit/20958e6d91d11a80d6c664ce6346791259b1d193
msg300994 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-29 16:18
Thank you for your contribution Oren!
History
Date User Action Args
2017-08-29 16:18:10serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg300994

stage: backport needed -> resolved
2017-08-29 16:16:17serhiy.storchakasetmessages: + msg300993
2017-08-29 15:05:36Oren Milmansetpull_requests: + pull_request3276
2017-08-29 12:43:34serhiy.storchakasetmessages: + msg300982
2017-08-29 10:54:58Oren Milmansetpull_requests: + pull_request3275
2017-08-29 08:59:37serhiy.storchakasetstage: needs patch -> backport needed
2017-08-29 08:58:33serhiy.storchakasetmessages: + msg300977
2017-08-23 08:10:16serhiy.storchakasetnosy: + benjamin.peterson, stutzbach
components: + IO
2017-08-21 19:00:57Oren Milmansetpull_requests: + pull_request3208
2017-08-21 10:12:45Oren Milmansetmessages: + msg300618
2017-08-21 09:13:56serhiy.storchakasetkeywords: + easy (C)
assignee: serhiy.storchaka
2017-08-21 09:13:26serhiy.storchakasetversions: + Python 2.7, Python 3.6
nosy: + serhiy.storchaka

messages: + msg300616

components: + Extension Modules, - Interpreter Core
stage: needs patch
2017-08-21 08:33:08Oren Milmancreate