diff options
author | Éric Araujo <merwok@netwok.org> | 2011-03-23 01:08:07 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-03-23 01:08:07 (GMT) |
commit | 48049911d6ff4f3caebc09a28c0a94e0c7ad8842 (patch) | |
tree | fc6fadcb04b046e45b552af2b76d5169200bb234 /Objects | |
parent | c706dbfa67236eccf389b7c3cea029baf7eab8b2 (diff) | |
download | cpython-48049911d6ff4f3caebc09a28c0a94e0c7ad8842.zip cpython-48049911d6ff4f3caebc09a28c0a94e0c7ad8842.tar.gz cpython-48049911d6ff4f3caebc09a28c0a94e0c7ad8842.tar.bz2 |
Fix obscure set crashers (#8420). Backport of d56b3cafb1e6, reviewed by Raymond.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/setobject.c | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index 30afc7c..7aa1a7f 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -364,12 +364,14 @@ static int set_add_entry(register PySetObject *so, setentry *entry) { register Py_ssize_t n_used; + PyObject *key = entry->key; + long hash = entry->hash; assert(so->fill <= so->mask); /* at least one empty slot */ n_used = so->used; - Py_INCREF(entry->key); - if (set_insert_key(so, entry->key, (long) entry->hash) == -1) { - Py_DECREF(entry->key); + Py_INCREF(key); + if (set_insert_key(so, key, hash) == -1) { + Py_DECREF(key); return -1; } if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2)) @@ -637,6 +639,8 @@ static int set_merge(PySetObject *so, PyObject *otherset) { PySetObject *other; + PyObject *key; + long hash; register Py_ssize_t i; register setentry *entry; @@ -657,11 +661,13 @@ set_merge(PySetObject *so, PyObject *otherset) } for (i = 0; i <= other->mask; i++) { entry = &other->table[i]; - if (entry->key != NULL && - entry->key != dummy) { - Py_INCREF(entry->key); - if (set_insert_key(so, entry->key, (long) entry->hash) == -1) { - Py_DECREF(entry->key); + key = entry->key; + hash = entry->hash; + if (key != NULL && + key != dummy) { + Py_INCREF(key); + if (set_insert_key(so, key, hash) == -1) { + Py_DECREF(key); return -1; } } @@ -1642,15 +1648,22 @@ set_symmetric_difference_update(PySetObject *so, PyObject *other) while (_PyDict_Next(other, &pos, &key, &value, &hash)) { setentry an_entry; + Py_INCREF(key); an_entry.hash = hash; an_entry.key = key; + rv = set_discard_entry(so, &an_entry); - if (rv == -1) + if (rv == -1) { + Py_DECREF(key); return NULL; + } if (rv == DISCARD_NOTFOUND) { - if (set_add_entry(so, &an_entry) == -1) + if (set_add_entry(so, &an_entry) == -1) { + Py_DECREF(key); return NULL; + } } + Py_DECREF(key); } Py_RETURN_NONE; } |