Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4db194

Browse files
committed
When comparing objects, always check that tp_compare is not NULL
before calling it. This check was there when the objects were of the same type *before* coercion, but not if they initially differed but became the same *after* coercion.
1 parent 052969a commit b4db194

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

Objects/object.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,18 @@ PyObject_Compare(v, w)
312312
if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
313313
char *vname = vtp->tp_name;
314314
char *wname = wtp->tp_name;
315-
if (vtp->tp_as_number != NULL &&
316-
wtp->tp_as_number != NULL) {
315+
if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
317316
int err;
318317
err = PyNumber_CoerceEx(&v, &w);
319318
if (err < 0)
320319
return -1;
321320
else if (err == 0) {
322-
int cmp = (*v->ob_type->tp_compare)(v, w);
321+
int cmp;
322+
vtp = v->ob_type;
323+
if (vtp->tp_compare == NULL)
324+
cmp = (v < w) ? -1 : 1;
325+
else
326+
cmp = (*vtp->tp_compare)(v, w);
323327
Py_DECREF(v);
324328
Py_DECREF(w);
325329
return cmp;

0 commit comments

Comments
 (0)