Commit 74001fad authored by Benjamin Peterson's avatar Benjamin Peterson

list slotdefs in offset order rather than sorting them (closes #17610)

This means we can remove our usage of qsort() than relied on undefined behavior.

Backport by Zbigniew Halas.
parent aff9ceff
...@@ -377,6 +377,7 @@ Paul ten Hagen ...@@ -377,6 +377,7 @@ Paul ten Hagen
Rasmus Hahn Rasmus Hahn
Peter Haight Peter Haight
Václav Haisman Václav Haisman
Zbigniew Halas
Bob Halley Bob Halley
Jesse Hallio Jesse Hallio
Jun Hamano Jun Hamano
......
...@@ -9,6 +9,8 @@ What's New in Python 2.7.5? ...@@ -9,6 +9,8 @@ What's New in Python 2.7.5?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #17610: Don't rely on non-standard behavior of the C qsort() function.
Library Library
------- -------
......
...@@ -5795,15 +5795,16 @@ slot_tp_del(PyObject *self) ...@@ -5795,15 +5795,16 @@ slot_tp_del(PyObject *self)
} }
/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper /*
functions. The offsets here are relative to the 'PyHeapTypeObject' Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions.
structure, which incorporates the additional structures used for numbers,
sequences and mappings. The table is ordered by offsets relative to the 'PyHeapTypeObject' structure,
Note that multiple names may map to the same slot (e.g. __eq__, which incorporates the additional structures used for numbers, sequences and
__ne__ etc. all map to tp_richcompare) and one name may map to multiple mappings. Note that multiple names may map to the same slot (e.g. __eq__,
slots (e.g. __str__ affects tp_str as well as tp_repr). The table is __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots
terminated with an all-zero entry. (This table is further initialized and (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with
sorted in init_slotdefs() below.) */ an all-zero entry. (This table is further initialized in init_slotdefs().)
*/
typedef struct wrapperbase slotdef; typedef struct wrapperbase slotdef;
...@@ -5853,57 +5854,57 @@ typedef struct wrapperbase slotdef; ...@@ -5853,57 +5854,57 @@ typedef struct wrapperbase slotdef;
"x." NAME "(y) <==> " DOC) "x." NAME "(y) <==> " DOC)
static slotdef slotdefs[] = { static slotdef slotdefs[] = {
SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, TPSLOT("__str__", tp_print, NULL, NULL, ""),
"x.__len__() <==> len(x)"), TPSLOT("__repr__", tp_print, NULL, NULL, ""),
/* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
The logic in abstract.c always falls back to nb_add/nb_multiply in TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
this case. Defining both the nb_* and the sq_* slots to call the TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
user-defined methods has unexpected side-effects, as shown by TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
test_descr.notimplemented() */ TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, "x.__cmp__(y) <==> cmp(x,y)"),
"x.__add__(y) <==> x+y"), TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, "x.__repr__() <==> repr(x)"),
"x.__mul__(n) <==> x*n"), TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, "x.__hash__() <==> hash(x)"),
"x.__rmul__(n) <==> n*x"), FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
"x.__getitem__(y) <==> x[y]"), TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc, "x.__str__() <==> str(x)"),
"x.__getslice__(i, j) <==> x[i:j]\n\ TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
\n\ wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
Use of negative indices is not supported."), TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
"x.__setitem__(i, y) <==> x[i]=y"), "x.__setattr__('name', value) <==> x.name = value"),
SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
"x.__delitem__(y) <==> del x[y]"), "x.__delattr__('name') <==> del x.name"),
SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
wrap_ssizessizeobjargproc, "x.__lt__(y) <==> x<y"),
"x.__setslice__(i, j, y) <==> x[i:j]=y\n\ TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
\n\ "x.__le__(y) <==> x<=y"),
Use of negative indices is not supported."), TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, "x.__eq__(y) <==> x==y"),
"x.__delslice__(i, j) <==> del x[i:j]\n\ TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
\n\ "x.__ne__(y) <==> x!=y"),
Use of negative indices is not supported."), TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, "x.__gt__(y) <==> x>y"),
"x.__contains__(y) <==> y in x"), TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
SQSLOT("__iadd__", sq_inplace_concat, NULL, "x.__ge__(y) <==> x>=y"),
wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
SQSLOT("__imul__", sq_inplace_repeat, NULL, "x.__iter__() <==> iter(x)"),
wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
"x.next() -> the next value, or raise StopIteration"),
MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
"x.__len__() <==> len(x)"), "descr.__get__(obj[, type]) -> value"),
MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
wrap_binaryfunc, "descr.__set__(obj, value)"),
"x.__getitem__(y) <==> x[y]"), TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, wrap_descr_delete, "descr.__delete__(obj)"),
wrap_objobjargproc, FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
"x.__setitem__(i, y) <==> x[i]=y"), "x.__init__(...) initializes x; "
MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, "see help(type(x)) for signature",
wrap_delitem, PyWrapperFlag_KEYWORDS),
"x.__delitem__(y) <==> del x[y]"), TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
BINSLOT("__add__", nb_add, slot_nb_add, BINSLOT("__add__", nb_add, slot_nb_add,
"+"), "+"),
RBINSLOT("__radd__", nb_add, slot_nb_add, RBINSLOT("__radd__", nb_add, slot_nb_add,
...@@ -5961,8 +5962,6 @@ static slotdef slotdefs[] = { ...@@ -5961,8 +5962,6 @@ static slotdef slotdefs[] = {
"oct(x)"), "oct(x)"),
UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
"hex(x)"), "hex(x)"),
NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
"x[y:z] <==> x[y.__index__():z.__index__()]"),
IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
wrap_binaryfunc, "+="), wrap_binaryfunc, "+="),
IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
...@@ -5993,58 +5992,57 @@ static slotdef slotdefs[] = { ...@@ -5993,58 +5992,57 @@ static slotdef slotdefs[] = {
slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
IBSLOT("__itruediv__", nb_inplace_true_divide, IBSLOT("__itruediv__", nb_inplace_true_divide,
slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, "x[y:z] <==> x[y.__index__():z.__index__()]"),
"x.__str__() <==> str(x)"), MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
TPSLOT("__str__", tp_print, NULL, NULL, ""), "x.__len__() <==> len(x)"),
TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
"x.__repr__() <==> repr(x)"), wrap_binaryfunc,
TPSLOT("__repr__", tp_print, NULL, NULL, ""), "x.__getitem__(y) <==> x[y]"),
TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
"x.__cmp__(y) <==> cmp(x,y)"), wrap_objobjargproc,
TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, "x.__setitem__(i, y) <==> x[i]=y"),
"x.__hash__() <==> hash(x)"), MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, wrap_delitem,
"x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), "x.__delitem__(y) <==> del x[y]"),
TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), "x.__len__() <==> len(x)"),
TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), The logic in abstract.c always falls back to nb_add/nb_multiply in
TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), this case. Defining both the nb_* and the sq_* slots to call the
TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, user-defined methods has unexpected side-effects, as shown by
"x.__setattr__('name', value) <==> x.name = value"), test_descr.notimplemented() */
TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, "x.__add__(y) <==> x+y"),
"x.__delattr__('name') <==> del x.name"), SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), "x.__mul__(n) <==> x*n"),
TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
"x.__lt__(y) <==> x<y"), "x.__rmul__(n) <==> n*x"),
TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
"x.__le__(y) <==> x<=y"), "x.__getitem__(y) <==> x[y]"),
TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
"x.__eq__(y) <==> x==y"), "x.__getslice__(i, j) <==> x[i:j]\n\
TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, \n\
"x.__ne__(y) <==> x!=y"), Use of negative indices is not supported."),
TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
"x.__gt__(y) <==> x>y"), "x.__setitem__(i, y) <==> x[i]=y"),
TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
"x.__ge__(y) <==> x>=y"), "x.__delitem__(y) <==> del x[y]"),
TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
"x.__iter__() <==> iter(x)"), wrap_ssizessizeobjargproc,
TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
"x.next() -> the next value, or raise StopIteration"), \n\
TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, Use of negative indices is not supported."),
"descr.__get__(obj[, type]) -> value"), SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, "x.__delslice__(i, j) <==> del x[i:j]\n\
"descr.__set__(obj, value)"), \n\
TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, Use of negative indices is not supported."),
wrap_descr_delete, "descr.__delete__(obj)"), SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, "x.__contains__(y) <==> y in x"),
"x.__init__(...) initializes x; " SQSLOT("__iadd__", sq_inplace_concat, NULL,
"see help(type(x)) for signature", wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
PyWrapperFlag_KEYWORDS), SQSLOT("__imul__", sq_inplace_repeat, NULL,
TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
{NULL} {NULL}
}; };
...@@ -6225,21 +6223,6 @@ update_slots_callback(PyTypeObject *type, void *data) ...@@ -6225,21 +6223,6 @@ update_slots_callback(PyTypeObject *type, void *data)
return 0; return 0;
} }
/* Comparison function for qsort() to compare slotdefs by their offset, and
for equal offset by their address (to force a stable sort). */
static int
slotdef_cmp(const void *aa, const void *bb)
{
const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
int c = a->offset - b->offset;
if (c != 0)
return c;
else
/* Cannot use a-b, as this gives off_t,
which may lose precision when converted to int. */
return (a > b) ? 1 : (a < b) ? -1 : 0;
}
/* Initialize the slotdefs table by adding interned string objects for the /* Initialize the slotdefs table by adding interned string objects for the
names and sorting the entries. */ names and sorting the entries. */
static void static void
...@@ -6251,12 +6234,12 @@ init_slotdefs(void) ...@@ -6251,12 +6234,12 @@ init_slotdefs(void)
if (initialized) if (initialized)
return; return;
for (p = slotdefs; p->name; p++) { for (p = slotdefs; p->name; p++) {
/* Slots must be ordered by their offset in the PyHeapTypeObject. */
assert(!p[1].name || p->offset <= p[1].offset);
p->name_strobj = PyString_InternFromString(p->name); p->name_strobj = PyString_InternFromString(p->name);
if (!p->name_strobj) if (!p->name_strobj)
Py_FatalError("Out of memory interning slotdef names"); Py_FatalError("Out of memory interning slotdef names");
} }
qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
slotdef_cmp);
initialized = 1; initialized = 1;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment