FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.14] gh-156101: Don't set the result in error in PyLong_AsInt32() (GH-156727) by miss-islington · Pull Request #156729 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Don't set the result in error in :c:func:`PyLong_AsInt32`,
:c:func:`PyLong_AsUInt32`, :c:func:`PyLong_AsInt64` and
:c:func:`PyLong_AsUInt64`. Leave the result unchanged in this case. Patch by
Victor Stinner.
12 changes: 8 additions & 4 deletions Modules/_testlimitedcapi/long.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,9 @@ static PyObject *
pylong_asint32(PyObject *module, PyObject *arg)
{
NULLABLE(arg);
int32_t value;
int32_t value = UNINITIALIZED_INT;
if (PyLong_AsInt32(arg, &value) < 0) {
assert(value == UNINITIALIZED_INT);
return NULL;
}
return PyLong_FromInt32(value);
Expand All @@ -773,8 +774,9 @@ static PyObject *
pylong_asuint32(PyObject *module, PyObject *arg)
{
NULLABLE(arg);
uint32_t value;
uint32_t value = UNINITIALIZED_INT;
if (PyLong_AsUInt32(arg, &value) < 0) {
assert(value == UNINITIALIZED_INT);
return NULL;
}
return PyLong_FromUInt32(value);
Expand All @@ -785,8 +787,9 @@ static PyObject *
pylong_asint64(PyObject *module, PyObject *arg)
{
NULLABLE(arg);
int64_t value;
int64_t value = UNINITIALIZED_INT;
if (PyLong_AsInt64(arg, &value) < 0) {
assert(value == UNINITIALIZED_INT);
return NULL;
}
return PyLong_FromInt64(value);
Expand All @@ -796,8 +799,9 @@ static PyObject *
pylong_asuint64(PyObject *module, PyObject *arg)
{
NULLABLE(arg);
uint64_t value;
uint64_t value = UNINITIALIZED_INT;
if (PyLong_AsUInt64(arg, &value) < 0) {
assert(value == UNINITIALIZED_INT);
return NULL;
}
return PyLong_FromUInt64(value);
Expand Down
36 changes: 20 additions & 16 deletions Objects/longobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6697,58 +6697,62 @@ PyObject* PyLong_FromUInt64(uint64_t value)
PYLONG_FROM_UINT(uint64_t, value);
}

#define LONG_TO_INT(obj, value, type_name) \
#define LONG_TO_INT(type, obj, result) \
do { \
type value; \
int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
| Py_ASNATIVEBYTES_ALLOW_INDEX); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, &value, sizeof(value), flags); \
if (bytes < 0) { \
return -1; \
} \
if ((size_t)bytes > sizeof(*value)) { \
if ((size_t)bytes > sizeof(value)) { \
PyErr_SetString(PyExc_OverflowError, \
"Python int too large to convert to " type_name); \
"Python int too large to convert to C " #type); \
return -1; \
} \
*result = value; \
return 0; \
} while (0)

int PyLong_AsInt32(PyObject *obj, int32_t *value)
int PyLong_AsInt32(PyObject *obj, int32_t *result)
{
LONG_TO_INT(obj, value, "C int32_t");
LONG_TO_INT(int32_t, obj, result);
}

int PyLong_AsInt64(PyObject *obj, int64_t *value)
int PyLong_AsInt64(PyObject *obj, int64_t *result)
{
LONG_TO_INT(obj, value, "C int64_t");
LONG_TO_INT(int64_t, obj, result);
}

#define LONG_TO_UINT(obj, value, type_name) \
#define LONG_TO_UINT(type, obj, result) \
do { \
type value; \
int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
| Py_ASNATIVEBYTES_UNSIGNED_BUFFER \
| Py_ASNATIVEBYTES_REJECT_NEGATIVE \
| Py_ASNATIVEBYTES_ALLOW_INDEX); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, &value, sizeof(value), flags); \
if (bytes < 0) { \
return -1; \
} \
if ((size_t)bytes > sizeof(*value)) { \
if ((size_t)bytes > sizeof(value)) { \
PyErr_SetString(PyExc_OverflowError, \
"Python int too large to convert to " type_name); \
"Python int too large to convert to C " #type); \
return -1; \
} \
*result = value; \
return 0; \
} while (0)

int PyLong_AsUInt32(PyObject *obj, uint32_t *value)
int PyLong_AsUInt32(PyObject *obj, uint32_t *result)
{
LONG_TO_UINT(obj, value, "C uint32_t");
LONG_TO_UINT(uint32_t, obj, result);
}

int PyLong_AsUInt64(PyObject *obj, uint64_t *value)
int PyLong_AsUInt64(PyObject *obj, uint64_t *result)
{
LONG_TO_UINT(obj, value, "C uint64_t");
LONG_TO_UINT(uint64_t, obj, result);
}


Expand Down
Loading

Back | FazBrowse Home | New Git URL