Skip to content

undefined behavior in Objects/obmalloc.c #67691

Closed

Description

@dwightguth
BPO 23503
Nosy @vstinner

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/vstinner'
closed_at = None
created_at = <Date 2015-02-23.21:18:56.610>
labels = ['interpreter-core', 'type-bug']
title = 'undefined behavior in Objects/obmalloc.c'
updated_at = <Date 2016-09-19.08:40:15.568>
user = 'https://bugs.python.org/dwightguth'

bugs.python.org fields:

activity = <Date 2016-09-19.08:40:15.568>
actor = 'vstinner'
assignee = 'vstinner'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2015-02-23.21:18:56.610>
creator = 'dwight.guth'
dependencies = []
files = []
hgrepos = []
issue_num = 23503
keywords = []
message_count = 3.0
messages = ['236465', '276955', '276956']
nosy_count = 2.0
nosy_names = ['vstinner', 'dwight.guth']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue23503'
versions = []

Activity

  1. dwightguth commented on Feb 23, 2015

    dwightguthmannequin
    MannequinAuthor

    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.

  2. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-bugAn unexpected behavior, bug, or error
    on Feb 23, 2015
  3. vstinner commented on Sep 19, 2016

    @vstinner
    Member

    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
  4. vstinner commented on Sep 19, 2016

    @vstinner
    Member

    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.

  5. transferred this issue fromon Apr 10, 2022
  6. iritkatriel commented on Feb 12, 2023

    @iritkatriel
    Member
  7. vstinner commented on Apr 5, 2023

    @vstinner
    Member

    Python is tested by ASAN, MSAN and UBSAN, and different memory debuggers. Last years, nobody reported an issue about this code. IMO it's just fine. Moreover, no fix was proposed. I just close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions