]> granicus.if.org Git - python/commitdiff
Replace the size check for PyMem_MALLOC and PyMem_REALLOC with an almost
authorMark Dickinson <dickinsm@gmail.com>
Thu, 10 Dec 2009 10:36:32 +0000 (10:36 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Thu, 10 Dec 2009 10:36:32 +0000 (10:36 +0000)
equivalent[*] check that doesn't produce compiler warnings about a 'x < 0'
check on an unsigned type.

[*] it's equivalent for inputs of type size_t or Py_ssize_t, or any smaller
unsigned or signed integer type.

Include/pymem.h

index e2dfe0d3fc35dba0d1f4b10a409b5b5a59802b28..966c0f5a09f52c356a8910a495a5ec79c2c7922d 100644 (file)
@@ -71,9 +71,9 @@ PyAPI_FUNC(void) PyMem_Free(void *);
    pymalloc. To solve these problems, allocate an extra byte. */
 /* Returns NULL to indicate error if a negative size or size larger than
    Py_ssize_t can represent is supplied.  Helps prevents security holes. */
-#define PyMem_MALLOC(n)                (((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \
+#define PyMem_MALLOC(n)                ((size_t)(n) > (size_t)PY_SSIZE_T_MAX ? NULL \
                                : malloc((n) ? (n) : 1))
-#define PyMem_REALLOC(p, n)    (((n) < 0 || (n) > PY_SSIZE_T_MAX) ? NULL \
+#define PyMem_REALLOC(p, n)    ((size_t)(n) > (size_t)PY_SSIZE_T_MAX  ? NULL \
                                : realloc((p), (n) ? (n) : 1))
 #define PyMem_FREE             free