Issue #22207: Fix "comparison between signed and unsigned integers" w… · pythoncapi/cpython@049e509 · GitHub
Skip to content

Commit 049e509

Browse files
committed
Issue python#22207: Fix "comparison between signed and unsigned integers" warning in
test checking for integer overflow on Py_ssize_t type: cast explicitly to size_t.
1 parent daca3d7 commit 049e509

8 files changed

Lines changed: 21 additions & 19 deletions

File tree

Modules/_codecsmodule.c

Lines changed: 1 addition & 1 deletion

Modules/_lzmamodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
533533
c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
534534
}
535535
}
536-
if (data_size != PyBytes_GET_SIZE(result))
536+
if (data_size != (size_t)PyBytes_GET_SIZE(result))
537537
if (_PyBytes_Resize(&result, data_size) == -1)
538538
goto error;
539539
return result;
@@ -931,7 +931,7 @@ decompress(Decompressor *d, uint8_t *data, size_t len)
931931
d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
932932
}
933933
}
934-
if (data_size != PyBytes_GET_SIZE(result))
934+
if (data_size != (size_t)PyBytes_GET_SIZE(result))
935935
if (_PyBytes_Resize(&result, data_size) == -1)
936936
goto error;
937937
return result;

Modules/_pickle.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,8 @@ raw_unicode_escape(PyObject *obj)
20522052
{
20532053
PyObject *repr;
20542054
char *p;
2055-
Py_ssize_t i, size, expandsize;
2055+
Py_ssize_t i, size;
2056+
size_t expandsize;
20562057
void *data;
20572058
unsigned int kind;
20582059

@@ -2067,7 +2068,7 @@ raw_unicode_escape(PyObject *obj)
20672068
else
20682069
expandsize = 6;
20692070

2070-
if (size > PY_SSIZE_T_MAX / expandsize)
2071+
if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
20712072
return PyErr_NoMemory();
20722073
repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
20732074
if (repr == NULL)

Modules/fcntlmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fcntl_fcntl(PyObject *self, PyObject *args)
4242

4343
if (PyArg_ParseTuple(args, "O&is#:fcntl",
4444
conv_descriptor, &fd, &code, &str, &len)) {
45-
if (len > sizeof buf) {
45+
if ((size_t)len > sizeof buf) {
4646
PyErr_SetString(PyExc_ValueError,
4747
"fcntl string arg too long");
4848
return NULL;

Modules/mathmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ _fsum_realloc(double **p_ptr, Py_ssize_t n,
10101010
Py_ssize_t m = *m_ptr;
10111011

10121012
m += m; /* double */
1013-
if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) {
1013+
if (n < m && (size_t)m < ((size_t)PY_SSIZE_T_MAX / sizeof(double))) {
10141014
double *p = *p_ptr;
10151015
if (p == ps) {
10161016
v = PyMem_Malloc(sizeof(double) * m);

Objects/bytesobject.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
8181
{
8282
PyBytesObject *op;
8383
assert(size >= 0);
84+
8485
if (size == 0 && (op = nullstring) != NULL) {
8586
#ifdef COUNT_ALLOCS
8687
null_strings++;
@@ -89,7 +90,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
8990
return (PyObject *)op;
9091
}
9192

92-
if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
93+
if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
9394
PyErr_SetString(PyExc_OverflowError,
9495
"byte string is too large");
9596
return NULL;
@@ -1755,7 +1756,7 @@ bytes_count(PyBytesObject *self, PyObject *args)
17551756
bytes.translate
17561757
17571758
self: self(type="PyBytesObject *")
1758-
table: object
1759+
table: object
17591760
Translation table, which must be a bytes object of length 256.
17601761
[
17611762
deletechars: object
@@ -3328,7 +3329,7 @@ PyBytes_Concat(PyObject **pv, PyObject *w)
33283329
/* Only one reference, so we can resize in place */
33293330
Py_ssize_t oldsize;
33303331
Py_buffer wb;
3331-
3332+
33323333
wb.len = -1;
33333334
if (_getbuffer(w, &wb) < 0) {
33343335
PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",

Objects/tupleobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ PyTuple_New(Py_ssize_t size)
9797
#endif
9898
{
9999
/* Check for overflow */
100-
if (size > (PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
100+
if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
101101
sizeof(PyObject *)) / sizeof(PyObject *)) {
102102
return PyErr_NoMemory();
103103
}

Python/asdl.c

Lines changed: 8 additions & 8 deletions

0 commit comments

Comments
 (0)