﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30274	Segfault on iteration of GEOS Polygons/LinearRings.	Murray Christopherson	Sergey Fedoseev	"**Environment Details**
Ubuntu 14.04 (trusty)
Python 3.6.8 (via package manager, unofficial repo: ppa:deadsnakes/ppa)
Django 2.1.7 (via pip)
libgeos (both 3.4.2 via apt-get, and 3.7.1 via source distribution)

First and foremost, I am aware that the OS is a bit older (hence the unofficial installation routes of some newer dependencies), but I was unable to find any reason these components shouldn't play nice.

**Steps to reproduce**
I have a minimal script showcasing the issue:

{{{
from django.contrib.gis.geos import Polygon

def main():
    polygon = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) )

    for p in polygon[0]:
        print('Longitude:', p[0])
        print('Latitude:', p[1])

if __name__ == '__main__':
    main()
}}}

**Expected Behaviour**
Output to stdout, something like:

{{{
Latitude: 0.0
Longitude: 0.0
Latitude: 0.0
Longitude: 50.0
...
}}}

**Observed Behaviour**
Output to stderr:

{{{
Segmentation fault (core dumped)
}}}

**Notes**
It should be noted, when originally observed, there was a stacktrace (which I won't post in full, as it's mostly uWSGI functions), but the relevant section suggested the segfault is in `GEOSCoordSeq_getSize` in `libgeos.so`, which leads me to believe the issue may be in https://github.com/django/django/blob/master/django/contrib/gis/geos/prototypes/coordseq.py.

Furthermore, this issue was uncovered during a Python 2 to 3 migration attempt. The issue did not manifest in this environment:
Ubuntu 14.04 (trusty)
Python 2.7.12 (via package manager, unofficial repo: ppa:deadsnakes/ppa)
Django 1.11.18 (via pip)
libgeos 3.4.2 (via apt-get)

Lastly, I was able to find a workaround (in case anyone else encounters this). This un-Pythonic variation seems to work:

{{{
from django.contrib.gis.geos import Polygon

def main():
    polygon = Polygon( ((0.0, 0.0), (0.0, 50.0), (50.0, 50.0), (50.0, 0.0), (0.0, 0.0)) )

    for i in range(len(polygon[0])):
        p = polygon[0][i]
        print('Longitude:', p[0])
        print('Latitude:', p[1])

if __name__ == '__main__':
    main()
}}}
"	Bug	closed	GIS	dev	Normal	fixed	geos gis	Fabian Schindler	Accepted	1	0	0	0	0	0
