Skip to content

Segfault when directly calling Polygon.__new__() #171

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

Closed
itzpr3d4t0r opened this issue Jan 12, 2023 · 0 comments
Closed

Segfault when directly calling Polygon.__new__() #171

itzpr3d4t0r opened this issue Jan 12, 2023 · 0 comments
Labels
priority:high type:bug Something isn't working

Comments

@itzpr3d4t0r
Copy link
Member

itzpr3d4t0r commented Jan 12, 2023

The following program segfaults:

from geometry import Polygon

poly = Polygon.__new__(Polygon)

poly[0] = (1, 2)

This is because the __new__ function in the Polygon class does not allocate the vertices attribute. Instead, it relies on the pgPolygon_FromObject function to handle this. Here is the pg_polygon_new current implementation:

static PyObject *
pg_polygon_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    pgPolygonObject *self = (pgPolygonObject *)type->tp_alloc(type, 0);

    if (self) {
        self->polygon.vertices = NULL;
        self->polygon.verts_num = 0;
        self->polygon.c_x = 0;
        self->polygon.c_y = 0;
        self->weakreflist = NULL;
    }

    return (PyObject *)self;
}

The issue can be fixed by merging in code change #157 and making the following changes:

  • Allocate memory for a double array in the "pg_polygon_new" function, with a minimum size of 6 (since the smallest polygon is a triangle)
  • Modify the "polygon init" function in Restructured Polygon internals #157 to resize memory as needed, in case the init function needs to allocate more than 3 vertices.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority:high type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant