From: Sandro Santilli Date: Thu, 12 Oct 2017 21:21:42 +0000 (+0000) Subject: Fix GCC warnings X-Git-Tag: 2.5.0alpha~406 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=23b0621202aa545eb6ec9eb9d0bf5abefb2ad3e5;p=postgis Fix GCC warnings Obey const pointer in autofix logic. Move ring closing logic to ptarray_to_GEOSCoordSeq. Fix memory access. Patch by Darafei Praliaskouski Fixes #3900 git-svn-id: http://svn.osgeo.org/postgis/trunk@15976 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/liblwgeom/lwgeom_geos.c b/liblwgeom/lwgeom_geos.c index 1d3ccf707..e61c28fe9 100644 --- a/liblwgeom/lwgeom_geos.c +++ b/liblwgeom/lwgeom_geos.c @@ -19,6 +19,7 @@ ********************************************************************** * * Copyright 2011-2014 Sandro Santilli + * Copyright 2017 Darafei Praliaskouski * **********************************************************************/ @@ -202,21 +203,18 @@ GEOS2LWGEOM(const GEOSGeometry *geom, char want3d) default: lwerror("GEOS2LWGEOM: unknown geometry type: %d", type); return NULL; - } - } - -GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *); - +GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *, int fix_ring); GEOSCoordSeq -ptarray_to_GEOSCoordSeq(const POINTARRAY *pa) +ptarray_to_GEOSCoordSeq(const POINTARRAY *pa, int fix_ring) { uint32_t dims = 2; uint32_t i; + int append_points = 0; const POINT3DZ *p3d; const POINT2D *p2d; GEOSCoordSeq sq; @@ -224,7 +222,27 @@ ptarray_to_GEOSCoordSeq(const POINTARRAY *pa) if ( FLAGS_GET_Z(pa->flags) ) dims = 3; - if ( ! (sq = GEOSCoordSeq_create(pa->npoints, dims)) ) + if ( fix_ring ) + { + if (pa->npoints < 1) + { + lwerror("ptarray_to_GEOSCoordSeq called with fix_ring and 0 vertices in ring, cannot fix"); + return NULL; + } + else + { + if ( pa->npoints < 4 ) + { + append_points = 4 - pa->npoints; + } + if ( ! ptarray_is_closed_2d(pa) && append_points == 0 ) + { + append_points = 1; + } + } + } + + if ( ! (sq = GEOSCoordSeq_create(pa->npoints + append_points, dims)) ) { lwerror("Error creating GEOS Coordinate Sequence"); return NULL; @@ -248,43 +266,43 @@ ptarray_to_GEOSCoordSeq(const POINTARRAY *pa) GEOSCoordSeq_setY(sq, i, p2d->y); if ( dims == 3 ) + { GEOSCoordSeq_setZ(sq, i, p3d->z); + } } - return sq; -} -static GEOSGeometry * -ptarray_to_GEOSLinearRing(const POINTARRAY *pa, int autofix) -{ - GEOSCoordSeq sq; - GEOSGeom g; - POINTARRAY *npa = 0; - - if ( autofix ) + if ( append_points ) { - if (pa->npoints < 1) + if ( dims == 3 ) { - lwerror("ptarray_to_GEOSLinearRing called with autofix and 0 vertices in ring, cannot fix"); + p3d = getPoint3dz_cp(pa, 0); + p2d = (const POINT2D *)p3d; } - - /* - * Check ring for being closed and fix if not. - * Also create a copy of geometry to operate on. - */ - if ( ! ptarray_is_closed_2d(pa) || pa->npoints < 4 ) + else { - pa = ptarray_addPoint(pa, getPoint_internal(pa, 0), FLAGS_NDIMS(pa->flags), pa->npoints); - npa = pa; + p2d = getPoint2d_cp(pa, 0); } - /* Check ring for having at least 4 vertices */ - while ( pa->npoints < 4 ) + for ( i = pa->npoints; i < pa->npoints + append_points; i++ ) { - ptarray_append_point(pa, getPoint_internal(pa, 0), LW_TRUE); + GEOSCoordSeq_setX(sq, i, p2d->x); + GEOSCoordSeq_setY(sq, i, p2d->y); + + if ( dims == 3 ) + { + GEOSCoordSeq_setZ(sq, i, p3d->z); + } } } - sq = ptarray_to_GEOSCoordSeq(pa); - if ( npa ) ptarray_free(npa); + return sq; +} + +static inline GEOSGeometry * +ptarray_to_GEOSLinearRing(const POINTARRAY *pa, int autofix) +{ + GEOSCoordSeq sq; + GEOSGeom g; + sq = ptarray_to_GEOSCoordSeq(pa, autofix); g = GEOSGeom_createLinearRing(sq); return g; } @@ -373,7 +391,7 @@ LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix) } else { - sq = ptarray_to_GEOSCoordSeq(lwp->point); + sq = ptarray_to_GEOSCoordSeq(lwp->point, 0); g = GEOSGeom_createPoint(sq); } if ( ! g ) @@ -392,7 +410,7 @@ LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix) FLAGS_NDIMS(lwl->points->flags), lwl->points->npoints); } - sq = ptarray_to_GEOSCoordSeq(lwl->points); + sq = ptarray_to_GEOSCoordSeq(lwl->points, 0); g = GEOSGeom_createLineString(sq); if ( ! g ) { @@ -2140,4 +2158,3 @@ LWGEOM* lwgeom_voronoi_diagram(const LWGEOM* g, const GBOX* env, double toleranc return lwgeom_result; #endif /* POSTGIS_GEOS_VERSION < 35 */ } - diff --git a/liblwgeom/ptarray.c b/liblwgeom/ptarray.c index c7ffa8ae9..639e7b5a3 100644 --- a/liblwgeom/ptarray.c +++ b/liblwgeom/ptarray.c @@ -1454,7 +1454,7 @@ ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, int mi int n_points = pa->npoints; int n_points_out = 0; int pt_size = ptarray_point_size(pa); - double dsq; + double dsq = FLT_MAX; /* No-op on short inputs */ if ( n_points <= 2 ) return; @@ -1470,7 +1470,7 @@ ptarray_remove_repeated_points_in_place(POINTARRAY *pa, double tolerance, int mi if (last) { /* Don't drop points if we are running short of points */ - if (n_points - i > min_points - n_points_out) + if (n_points - i > min_points - n_points_out) { if (tolerance > 0.0) {