From a00c3fd12d421e41b769debd7df717d17b0deed5 Mon Sep 17 00:00:00 2001 From: "T. Wouters" Date: Fri, 31 Mar 2017 09:14:41 -0700 Subject: bpo-29941: Assert fixes (#886) Make a non-Py_DEBUG, asserts-enabled build of CPython possible. This means making sure helper functions are defined when NDEBUG is not defined, not just when Py_DEBUG is defined. Also fix a division-by-zero in obmalloc.c that went unnoticed because in Py_DEBUG mode, elsize is never zero. --- Include/unicodeobject.h | 4 ++++ Objects/dictobject.c | 2 +- Objects/obmalloc.c | 6 +++--- Objects/typeobject.c | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h index c3d0516..0d10c71 100644 --- a/Include/unicodeobject.h +++ b/Include/unicodeobject.h @@ -2313,6 +2313,10 @@ PyAPI_FUNC(Py_UNICODE*) PyUnicode_AsUnicodeCopy( PyAPI_FUNC(int) _PyUnicode_CheckConsistency( PyObject *op, int check_content); +#elif !defined(NDEBUG) +/* For asserts that call _PyUnicode_CheckConsistency(), which would + * otherwise be a problem when building with asserts but without Py_DEBUG. */ +#define _PyUnicode_CheckConsistency(op, check_content) PyUnicode_Check(op) #endif #ifndef Py_LIMITED_API diff --git a/Objects/dictobject.c b/Objects/dictobject.c index aac7ac4..2543118 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -437,7 +437,7 @@ static PyObject *empty_values[1] = { NULL }; /* #define DEBUG_PYDICT */ -#ifdef Py_DEBUG +#ifndef NDEBUG static int _PyDict_CheckConsistency(PyDictObject *mp) { diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c index a1142f3..f284d9f 100644 --- a/Objects/obmalloc.c +++ b/Objects/obmalloc.c @@ -1227,6 +1227,9 @@ _PyObject_Alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize) _Py_AllocatedBlocks++; + if (nelem == 0 || elsize == 0) + goto redirect; + assert(nelem <= PY_SSIZE_T_MAX / elsize); nbytes = nelem * elsize; @@ -1237,9 +1240,6 @@ _PyObject_Alloc(int use_calloc, void *ctx, size_t nelem, size_t elsize) goto redirect; #endif - if (nelem == 0 || elsize == 0) - goto redirect; - if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) { LOCK(); /* diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 64a72d2..4a9949e 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -128,7 +128,7 @@ skip_signature(const char *doc) return NULL; } -#ifdef Py_DEBUG +#ifndef NDEBUG static int _PyType_CheckConsistency(PyTypeObject *type) { -- cgit v0.12