Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault and unwanted behaviour in Line conversion #195

Closed
itzpr3d4t0r opened this issue Feb 13, 2023 · 2 comments
Closed

Segfault and unwanted behaviour in Line conversion #195

itzpr3d4t0r opened this issue Feb 13, 2023 · 2 comments

Comments

@itzpr3d4t0r
Copy link
Member

itzpr3d4t0r commented Feb 13, 2023

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

@itzpr3d4t0r itzpr3d4t0r pinned this issue Feb 13, 2023
@itzpr3d4t0r
Copy link
Member Author

itzpr3d4t0r commented Feb 13, 2023

Btw i partially fixed this (only for polygons) in #194 and yeah UPDATE: this is true for Circle as well: #189.

@Emc2356
Copy link
Collaborator

Emc2356 commented Mar 12, 2023

i dont not consider this an issue, this functions are designed to take a sequence and Polygon/Rect/Line are considered sequences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants