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.
This commit is contained in:
Victor Stinner 2014-08-17 22:20:00 +02:00
parent daca3d7e9b
commit 049e509a9f
8 changed files with 21 additions and 19 deletions

View File

@ -699,7 +699,7 @@ unicode_internal_encode(PyObject *self,
u = PyUnicode_AsUnicodeAndSize(obj, &len); u = PyUnicode_AsUnicodeAndSize(obj, &len);
if (u == NULL) if (u == NULL)
return NULL; return NULL;
if (len > PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
return PyErr_NoMemory(); return PyErr_NoMemory();
size = len * sizeof(Py_UNICODE); size = len * sizeof(Py_UNICODE);
return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size), return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size),

View File

@ -533,7 +533,7 @@ compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
} }
} }
if (data_size != PyBytes_GET_SIZE(result)) if (data_size != (size_t)PyBytes_GET_SIZE(result))
if (_PyBytes_Resize(&result, data_size) == -1) if (_PyBytes_Resize(&result, data_size) == -1)
goto error; goto error;
return result; return result;
@ -931,7 +931,7 @@ decompress(Decompressor *d, uint8_t *data, size_t len)
d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
} }
} }
if (data_size != PyBytes_GET_SIZE(result)) if (data_size != (size_t)PyBytes_GET_SIZE(result))
if (_PyBytes_Resize(&result, data_size) == -1) if (_PyBytes_Resize(&result, data_size) == -1)
goto error; goto error;
return result; return result;

View File

@ -2052,7 +2052,8 @@ raw_unicode_escape(PyObject *obj)
{ {
PyObject *repr; PyObject *repr;
char *p; char *p;
Py_ssize_t i, size, expandsize; Py_ssize_t i, size;
size_t expandsize;
void *data; void *data;
unsigned int kind; unsigned int kind;
@ -2067,7 +2068,7 @@ raw_unicode_escape(PyObject *obj)
else else
expandsize = 6; expandsize = 6;
if (size > PY_SSIZE_T_MAX / expandsize) if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
return PyErr_NoMemory(); return PyErr_NoMemory();
repr = PyBytes_FromStringAndSize(NULL, expandsize * size); repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
if (repr == NULL) if (repr == NULL)

View File

@ -42,7 +42,7 @@ fcntl_fcntl(PyObject *self, PyObject *args)
if (PyArg_ParseTuple(args, "O&is#:fcntl", if (PyArg_ParseTuple(args, "O&is#:fcntl",
conv_descriptor, &fd, &code, &str, &len)) { conv_descriptor, &fd, &code, &str, &len)) {
if (len > sizeof buf) { if ((size_t)len > sizeof buf) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"fcntl string arg too long"); "fcntl string arg too long");
return NULL; return NULL;

View File

@ -1010,7 +1010,7 @@ _fsum_realloc(double **p_ptr, Py_ssize_t n,
Py_ssize_t m = *m_ptr; Py_ssize_t m = *m_ptr;
m += m; /* double */ m += m; /* double */
if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) { if (n < m && (size_t)m < ((size_t)PY_SSIZE_T_MAX / sizeof(double))) {
double *p = *p_ptr; double *p = *p_ptr;
if (p == ps) { if (p == ps) {
v = PyMem_Malloc(sizeof(double) * m); v = PyMem_Malloc(sizeof(double) * m);

View File

@ -81,6 +81,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
{ {
PyBytesObject *op; PyBytesObject *op;
assert(size >= 0); assert(size >= 0);
if (size == 0 && (op = nullstring) != NULL) { if (size == 0 && (op = nullstring) != NULL) {
#ifdef COUNT_ALLOCS #ifdef COUNT_ALLOCS
null_strings++; null_strings++;
@ -89,7 +90,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
return (PyObject *)op; return (PyObject *)op;
} }
if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) {
PyErr_SetString(PyExc_OverflowError, PyErr_SetString(PyExc_OverflowError,
"byte string is too large"); "byte string is too large");
return NULL; return NULL;

View File

@ -97,7 +97,7 @@ PyTuple_New(Py_ssize_t size)
#endif #endif
{ {
/* Check for overflow */ /* Check for overflow */
if (size > (PY_SSIZE_T_MAX - sizeof(PyTupleObject) - if ((size_t)size > ((size_t)PY_SSIZE_T_MAX - sizeof(PyTupleObject) -
sizeof(PyObject *)) / sizeof(PyObject *)) { sizeof(PyObject *)) / sizeof(PyObject *)) {
return PyErr_NoMemory(); return PyErr_NoMemory();
} }

View File

@ -5,21 +5,21 @@ asdl_seq *
_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena) _Py_asdl_seq_new(Py_ssize_t size, PyArena *arena)
{ {
asdl_seq *seq = NULL; asdl_seq *seq = NULL;
size_t n = (size ? (sizeof(void *) * (size - 1)) : 0); size_t n;
/* check size is sane */ /* check size is sane */
if (size < 0 || size == INT_MIN || if (size < 0 ||
(size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { (size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
n = (size ? (sizeof(void *) * (size - 1)) : 0);
/* check if size can be added safely */ /* check if size can be added safely */
if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
n += sizeof(asdl_seq); n += sizeof(asdl_seq);
seq = (asdl_seq *)PyArena_Malloc(arena, n); seq = (asdl_seq *)PyArena_Malloc(arena, n);
@ -36,21 +36,21 @@ asdl_int_seq *
_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena) _Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena)
{ {
asdl_int_seq *seq = NULL; asdl_int_seq *seq = NULL;
size_t n = (size ? (sizeof(void *) * (size - 1)) : 0); size_t n;
/* check size is sane */ /* check size is sane */
if (size < 0 || size == INT_MIN || if (size < 0 ||
(size && ((size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { (size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
n = (size ? (sizeof(void *) * (size - 1)) : 0);
/* check if size can be added safely */ /* check if size can be added safely */
if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { if (n > PY_SIZE_MAX - sizeof(asdl_seq)) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
n += sizeof(asdl_seq); n += sizeof(asdl_seq);
seq = (asdl_int_seq *)PyArena_Malloc(arena, n); seq = (asdl_int_seq *)PyArena_Malloc(arena, n);