Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bpo-1621: Avoid signed integer overflow in set_table_resize() (GH-9059)…
… (GH-9198)

Address a C undefined behavior signed integer overflow issue in set object table resizing.  Our -fwrapv compiler flag and practical reasons why sets are unlikely to get this large should mean this was never an issue but it was incorrect code that generates code analysis warnings.

Co-authored-by: Sergey Fedoseev <fedoseev.sergey@gmail.com>
  • Loading branch information
2 people authored and vstinner committed Oct 19, 2018
1 parent 76d31a3 commit 6665802
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
@@ -0,0 +1,2 @@
Do not assume signed integer overflow behavior (C undefined behavior) when
performing set hash table resizing.
11 changes: 3 additions & 8 deletions Objects/setobject.c
Expand Up @@ -302,7 +302,6 @@ actually be smaller than the old one.
static int
set_table_resize(PySetObject *so, Py_ssize_t minused)
{
Py_ssize_t newsize;
setentry *oldtable, *newtable, *entry;
Py_ssize_t oldmask = so->mask;
size_t newmask;
Expand All @@ -313,13 +312,9 @@ set_table_resize(PySetObject *so, Py_ssize_t minused)

/* Find the smallest table size > minused. */
/* XXX speed-up with intrinsics */
for (newsize = PySet_MINSIZE;
newsize <= minused && newsize > 0;
newsize <<= 1)
;
if (newsize <= 0) {
PyErr_NoMemory();
return -1;
size_t newsize = PySet_MINSIZE;
while (newsize <= (size_t)minused) {
newsize <<= 1; // The largest possible value is PY_SSIZE_T_MAX + 1.
}

/* Get space for a new table. */
Expand Down

0 comments on commit 6665802

Please sign in to comment.