This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: undefined behavior in Objects/obmalloc.c
Type: behavior Stage:
Components: Interpreter Core Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: vstinner Nosy List: dwight.guth, vstinner
Priority: normal Keywords:

Created on 2015-02-23 21:18 by dwight.guth, last changed 2022-04-11 14:58 by admin.

Messages (3)
msg236465 - (view) Author: Dwight Guth (dwight.guth) Date: 2015-02-23 21:18
According to the ISO C standard (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) section 6.5.6 paragraph 8, the behavior of the C addition operator is undefined if the result of the operand does not either fall inside the same array object that is being indexed, or one index past the end of the array object. In Objects/obmalloc.c line 841, the macros PT(0) through PT(7) expand to references to array indexes before the beginning of the usedpools array. As a result, this initializer causes this file to be undefined.
msg276955 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-09-19 08:36
It seems that line numbers are for Python 3.5:

#define PTA(x)  ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
#define PT(x)   PTA(x), PTA(x)

static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = {
    PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7) <==== HERE
msg276956 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-09-19 08:40
> As a result, this initializer causes this file to be undefined.

Hum, in practice I'm not aware of any crash on any platform. Python is tested on various compilers (GCC, Clang, ICC, Microsoft Visual Studio, etc.), various operating systems (Mac OS X, Linux, Windows, FreeBSD, OpenBSD, Solaris, etc.), various architectures (x86, x86-64, PPC, etc.).

The memory allocator is probably the first instruction when starting Python.

Anyway, do you see a way to avoid the undefined behaviour?

Python has a _PyMem_SetupAllocators() function called very early in main() (before the first Python memory allocation), but calling this function is optional. So we have to prepare everything during the compilation.
History
Date User Action Args
2022-04-11 14:58:13adminsetgithub: 67691
2016-09-19 08:40:15vstinnersetmessages: + msg276956
2016-09-19 08:36:10vstinnersetmessages: + msg276955
2016-09-19 08:20:24christian.heimessetassignee: vstinner

nosy: + vstinner
2015-02-23 21:18:56dwight.guthcreate