Skip to content

array append, insert and extend grow the array on OverflowError #158345

Description

@fedonman

Bug description:

Since gh-156865, adding a value that does not fit to an 'f', 'Zf' or 'e' array raises OverflowError, but append(), insert() and extend() still grow the array by one item. append() and extend() leave an item read from uninitialized memory, and insert() duplicates the neighbouring item.

import array
for method, args in [('append', (1e300,)), ('insert', (0, 1e300))]:
    a = array.array('f', [1.0])
    try:
        getattr(a, method)(*args)
    except OverflowError as e:
        print(f'{method}: {e}: {a}')

Output on a debug build of main:

append: float too large to pack with f format: array('f', [1.0, -431602080.0])
insert: float too large to pack with f format: array('f', [1.0, 1.0])

Expected: array('f', [1.0]) in both cases. 3.15 has the same problem for 'e' arrays with values such as 123456.0.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions