Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 049e509

Browse files
committed
Issue #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
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ unicode_internal_encode(PyObject *self,
699699
u = PyUnicode_AsUnicodeAndSize(obj, &len);
700700
if (u == NULL)
701701
return NULL;
702-
if (len > PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
702+
if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
703703
return PyErr_NoMemory();
704704
size = len * sizeof(Py_UNICODE);
705705
return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size),

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
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ asdl_seq *
55
_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena)
66
{
77
asdl_seq *seq = NULL;
8-
size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
8+
size_t n;
99

1010
/* check size is sane */
11-
if (size < 0 || size == INT_MIN ||
12-
(size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
11+
if (size < 0 ||
12+
(size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
1313
PyErr_NoMemory();
1414
return NULL;
1515
}
16+
n = (size ? (sizeof(void *) * (size - 1)) : 0);
1617

1718
/* check if size can be added safely */
1819
if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
1920
PyErr_NoMemory();
2021
return NULL;
2122
}
22-
2323
n += sizeof(asdl_seq);
2424

2525
seq = (asdl_seq *)PyArena_Malloc(arena, n);
@@ -36,21 +36,21 @@ asdl_int_seq *
3636
_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
3737
{
3838
asdl_int_seq *seq = NULL;
39-
size_t n = (size ? (sizeof(void *) * (size - 1)) : 0);
39+
size_t n;
4040

4141
/* check size is sane */
42-
if (size < 0 || size == INT_MIN ||
43-
(size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
42+
if (size < 0 ||
43+
(size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
4444
PyErr_NoMemory();
4545
return NULL;
4646
}
47+
n = (size ? (sizeof(void *) * (size - 1)) : 0);
4748

4849
/* check if size can be added safely */
4950
if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
5051
PyErr_NoMemory();
5152
return NULL;
5253
}
53-
5454
n += sizeof(asdl_seq);
5555

5656
seq = (asdl_int_seq *)PyArena_Malloc(arena, n);

0 commit comments

Comments
 (0)