Movatterモバイル変換


[0]ホーム

URL:


homepage

Issue43027

This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title:Calling _PyBytes_Resize() on 1-byte bytes may raise error
Type:behaviorStage:
Components:Interpreter CoreVersions:Python 3.10, Python 3.9, Python 3.8
process
Status:openResolution:
Dependencies:Superseder:
Assigned To:Nosy List: malin
Priority:normalKeywords:

Created on2021-01-26 03:52 bymalin, last changed2022-04-11 14:59 byadmin.

Messages (1)
msg385689 -(view)Author: Ma Lin (malin)*Date: 2021-01-26 03:52
PyBytes_FromStringAndSize() uses a global cache for 1-byte bytes:https://github.com/python/cpython/blob/v3.10.0a4/Objects/bytesobject.c#L147    if (size == 1 && str != NULL) {        struct _Py_bytes_state *state = get_bytes_state();        op = state->characters[*str & UCHAR_MAX];        if (op != NULL) {            Py_INCREF(op);            return (PyObject *)op;        }    }_PyBytes_Resize() will raise an error when (refcount != 1):https://github.com/python/cpython/blob/v3.10.0a4/Objects/bytesobject.c#L3029Then this code will raise an exception:    obj1 = PyBytes_FromStringAndSize("a", 1);    obj2 = PyBytes_FromStringAndSize("a", 1);    ret = _PyBytes_Resize(&obj2, 2);  // ret is -1BTW, 0-byte bytes comes from a global singleton, but _PyBytes_Resize() processes it before checking refcount:https://github.com/python/cpython/blob/v3.10.0a4/Objects/bytesobject.c#L3021    if (Py_SIZE(v) == 0) {        if (newsize == 0) {            return 0;        }        *pv = _PyBytes_FromSize(newsize, 0);        Py_DECREF(v);        return (*pv == NULL) ? -1 : 0;    }    if (Py_REFCNT(v) != 1) {        goto error;    }_PyBytes_Resize() doc:        Only use this to build up a brand new bytes object; don’t use this if the    bytes may already be known in other parts of the code. It is an error to    call this function if the refcount on the input bytes object is not one.
History
DateUserActionArgs
2022-04-11 14:59:40adminsetgithub: 87193
2021-01-26 03:52:05malincreate
Supported byThe Python Software Foundation,
Powered byRoundup
Copyright © 1990-2022,Python Software Foundation
Legal Statements

[8]ページ先頭

©2009-2026 Movatter.jp