diff --git a/Objects/setobject.c b/Objects/setobject.c --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -138,17 +138,27 @@ setentry *entry; size_t perturb = hash; size_t mask = (size_t)so->mask; - size_t i = (size_t)hash; + size_t i = (size_t)hash & mask; size_t j; while (1) { - for (j = 0 ; j <= LINEAR_PROBES ; j++) { - entry = &table[(i + j) & mask]; - if (entry->key == NULL) - goto found_null; + if (i + LINEAR_PROBES <= mask) { + for (j = 0 ; j <= LINEAR_PROBES ; j++) { + entry = &table[i + j]; + if (entry->key == NULL) + goto found_null; + } + + } else { + for (j = 0 ; j <= LINEAR_PROBES ; j++) { + entry = &table[(i + j) & mask]; + if (entry->key == NULL) + goto found_null; + } } perturb >>= PERTURB_SHIFT; i = i * 5 + 1 + perturb; + i &= mask; } found_null: entry->key = key;