Skip to content

gh-158345: Don't grow an array when append, insert or extend overflows - #158348

Open
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-array-float-overflow-grow
Open

fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-array-float-overflow-grow

Conversation

@fedonman

@fedonman fedonman commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

append(), insert() and extend() on an 'e', 'f' or 'Zf' array grew the array by one item when they raised OverflowError, because the overflow was detected only after the array had been resized. The setters now pack the value into a local buffer before the bounds check, so it is rejected before the array grows.

…erflows

ins1() checks the item with setitem(self, -1, v) before growing the
array, but since pythongh-156865 the 'e', 'f' and 'Zf' setters detect an
overflow only when they pack into the array, which that call skips.
The array then grew by one item before the real store failed.

Pack into a local buffer before the bounds check, so the check call
fails too, and copy the result into the array only for a real index.
Comment thread Modules/arraymodule.c
Comment on lines +592 to +594
if (PyFloat_Pack2(x, buf, PY_LITTLE_ENDIAN) < 0) {
return -1;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move this to the if (i >= 0) branch below, ditto for other functions.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants