Skip to content

Segfault and unwanted behaviour in Line conversion #195

@itzpr3d4t0r

Description

@itzpr3d4t0r

While working on #194 i discovered two major issues.

The first one is that you can create a Line object from a Rect

r = Rect(0, 0, 100, 100)
l = Line(r) # returns a Line(0, 0, 100, 100)

This is not fine as any function that takes in a Line or Line-like object also accepts a Rect. I don't think we should support this behaviour.

The second thing is that doing the same with a Polygon with 4 vertices results in a segfault:

p = Polygon((0, 0), (100, 0), (100, 100), (0, 100))
l = Line(p)

I've investigated a bit and founf that it's caused by the pgLine_FromObject function, responsible for extracting a Line struct from a single object. Issue is that both rect and Polygon count as Sequences, that are then treated in that function with this piece of code:

if (PySequence_Check(obj)) {
        length = PySequence_Length(obj);
        if (length == 4) {
            PyObject *tmp;
            tmp = PySequence_GetItem(obj, 0);
            if (!pg_DoubleFromObj(tmp, &(out->x1))) {
                Py_DECREF(tmp);
                return 0;
            }
            Py_DECREF(tmp);
            tmp = PySequence_GetItem(obj, 1);
            if (!pg_DoubleFromObj(tmp, &(out->y1))) {
                Py_DECREF(tmp);
                return 0;
            }
            Py_DECREF(tmp);
            tmp = PySequence_GetItem(obj, 2);
            if (!pg_DoubleFromObj(tmp, &(out->x2))) {
                Py_DECREF(tmp);
                return 0;
            }
            Py_DECREF(tmp);
            tmp = PySequence_GetItem(obj, 3);
            if (!pg_DoubleFromObj(tmp, &(out->y2))) {
                Py_DECREF(tmp);
                return 0;
            }
            Py_DECREF(tmp);
            return IS_LINE_VALID(out);
        }
        ...

So when the sequence has length 4 (that are exactly the rect and Polygon with 4 vertices cases) issues arise

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions