This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyType_FromSpecWithBases: spec->name is dereferenced before checking for NULL
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, berker.peksag, enedil, izbyshev, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-08-25 15:26 by izbyshev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 8930 merged izbyshev, 2018-08-25 18:40
PR 8931 merged miss-islington, 2018-08-25 18:54
PR 8932 merged miss-islington, 2018-08-25 18:54
Messages (7)
msg324073 - (view) Author: Alexey Izbyshev (izbyshev) * (Python triager) Date: 2018-08-25 15:26
In the following snippet from PyType_FromSpecWithBases() in Objects/typeobject.c, spec->name is dereferenced by strrchr() but then is checked for NULL:

    /* Set the type name and qualname */
    s = strrchr(spec->name, '.');
    if (s == NULL)
        s = (char*)spec->name;
    else
        s++;

    [snip]

    type->tp_name = spec->name;
    if (!type->tp_name)
        goto fail;

This was reported by Svace static analyzer.

If I were to check spec->name first, what error should I report to the caller? Is something like the following OK?

    if (spec->name == NULL) {
        PyErr_SetString(PyExc_SystemError,
                       "Type spec does not define the name field.");
        goto fail;
    }
msg324088 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2018-08-25 18:31
Your suggested fix seems fine to me.
msg324090 - (view) Author: Michał Radwański (enedil) * Date: 2018-08-25 18:37
But if the error message is set, then for consistency, it should be also the case that the later checks set some kind of error message.
msg324091 - (view) Author: Alexey Izbyshev (izbyshev) * (Python triager) Date: 2018-08-25 18:43
@enedil It seems that all later check either set a message or simply propagate the error set by a called function. Did you mean any specific later check?
msg324092 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2018-08-25 18:53
New changeset 5f79b50763d687aeeed8edcb4efcc7ac9f8fa186 by Benjamin Peterson (Alexey Izbyshev) in branch 'master':
closes bpo-34501: PyType_FromSpecWithBases: Check spec->name before dereferencing it. (GH-8930)
https://github.com/python/cpython/commit/5f79b50763d687aeeed8edcb4efcc7ac9f8fa186
msg324093 - (view) Author: miss-islington (miss-islington) Date: 2018-08-25 19:11
New changeset 15dadacabf6a1c62e9920b6a42ff8936ea1830f8 by Miss Islington (bot) in branch '3.7':
closes bpo-34501: PyType_FromSpecWithBases: Check spec->name before dereferencing it. (GH-8930)
https://github.com/python/cpython/commit/15dadacabf6a1c62e9920b6a42ff8936ea1830f8
msg324094 - (view) Author: miss-islington (miss-islington) Date: 2018-08-25 19:17
New changeset 323a91bb3a2b5639637efc517fe3f30d3bc288e2 by Miss Islington (bot) in branch '3.6':
closes bpo-34501: PyType_FromSpecWithBases: Check spec->name before dereferencing it. (GH-8930)
https://github.com/python/cpython/commit/323a91bb3a2b5639637efc517fe3f30d3bc288e2
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78682
2018-08-25 19:17:16miss-islingtonsetmessages: + msg324094
2018-08-25 19:11:32miss-islingtonsetnosy: + miss-islington
messages: + msg324093
2018-08-25 18:54:19miss-islingtonsetpull_requests: + pull_request8404
2018-08-25 18:54:08miss-islingtonsetpull_requests: + pull_request8403
2018-08-25 18:53:49benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg324092

stage: patch review -> resolved
2018-08-25 18:43:29izbyshevsetmessages: + msg324091
2018-08-25 18:40:00izbyshevsetkeywords: + patch
stage: patch review
pull_requests: + pull_request8402
2018-08-25 18:37:40enedilsetnosy: + enedil
messages: + msg324090
2018-08-25 18:31:51benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg324088
2018-08-25 15:26:40izbyshevcreate