From: Sandro Santilli Date: Tue, 10 Jan 2012 13:17:21 +0000 (+0000) Subject: Use a standard lwcollection_allows_subtype function to guard against bad input. X-Git-Tag: 2.0.0alpha1~129 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=92026a713ba919bf23edd59aa41103e9db87d354;p=postgis Use a standard lwcollection_allows_subtype function to guard against bad input. Fixes #698 (and #1445 in a better way) git-svn-id: http://svn.osgeo.org/postgis/trunk@8747 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/liblwgeom/g_serialized.c b/liblwgeom/g_serialized.c index 8d10e2a0d..e706c977f 100644 --- a/liblwgeom/g_serialized.c +++ b/liblwgeom/g_serialized.c @@ -1038,42 +1038,6 @@ static LWCIRCSTRING* lwcircstring_from_gserialized_buffer(uint8_t *data_ptr, uin return circstring; } -static int lwcollection_from_gserialized_allowed_types(int collectiontype, int subtype) -{ - if ( collectiontype == COLLECTIONTYPE ) - return LW_TRUE; - if ( collectiontype == MULTIPOINTTYPE && - subtype == POINTTYPE ) - return LW_TRUE; - if ( collectiontype == MULTILINETYPE && - subtype == LINETYPE ) - return LW_TRUE; - if ( collectiontype == MULTIPOLYGONTYPE && - subtype == POLYGONTYPE ) - return LW_TRUE; - if ( collectiontype == COMPOUNDTYPE && - (subtype == LINETYPE || subtype == CIRCSTRINGTYPE) ) - return LW_TRUE; - if ( collectiontype == CURVEPOLYTYPE && - (subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE) ) - return LW_TRUE; - if ( collectiontype == MULTICURVETYPE && - (subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE) ) - return LW_TRUE; - if ( collectiontype == MULTISURFACETYPE && - (subtype == POLYGONTYPE || subtype == CURVEPOLYTYPE) ) - return LW_TRUE; - if ( collectiontype == POLYHEDRALSURFACETYPE && - subtype == POLYGONTYPE ) - return LW_TRUE; - if ( collectiontype == TINTYPE && - subtype == TRIANGLETYPE ) - return LW_TRUE; - - /* Must be a bad combination! */ - return LW_FALSE; -} - static LWCOLLECTION* lwcollection_from_gserialized_buffer(uint8_t *data_ptr, uint8_t g_flags, size_t *g_size) { uint32_t type; @@ -1110,7 +1074,7 @@ static LWCOLLECTION* lwcollection_from_gserialized_buffer(uint8_t *data_ptr, uin uint32_t subtype = lw_get_uint32_t(data_ptr); size_t subsize = 0; - if ( ! lwcollection_from_gserialized_allowed_types(type, subtype) ) + if ( ! lwcollection_allows_subtype(type, subtype) ) { lwerror("Invalid subtype (%s) for collection type (%s)", lwtype_name(subtype), lwtype_name(type)); lwfree(collection); diff --git a/liblwgeom/liblwgeom_internal.h b/liblwgeom/liblwgeom_internal.h index 20a6a5f3e..d050fbf1f 100644 --- a/liblwgeom/liblwgeom_internal.h +++ b/liblwgeom/liblwgeom_internal.h @@ -322,4 +322,7 @@ int lwline_split_by_point_to(const LWLINE* ln, const LWPOINT* pt, LWMLINE* to); /** Ensure the collection can hold at least up to ngeoms geometries */ void lwcollection_reserve(LWCOLLECTION *col, int ngeoms); +/** Check if subtype is allowed in collectiontype */ +extern int lwcollection_allows_subtype(int collectiontype, int subtype); + #endif /* _LIBLWGEOM_INTERNAL_H */ diff --git a/liblwgeom/lwcollection.c b/liblwgeom/lwcollection.c index f86d3906e..304f4ac0b 100644 --- a/liblwgeom/lwcollection.c +++ b/liblwgeom/lwcollection.c @@ -180,27 +180,10 @@ LWCOLLECTION* lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom) } /* Check type compatibility */ - if ( col->type < 7 ) { - if ( geom->type != col->type-3 ) { - lwerror("%s cannot contain %s element", lwtype_name(col->type), lwtype_name(geom->type)); - return NULL; - } - } - else if ( col->type == COMPOUNDTYPE ) { - /* Allow: linestring, circularstring */ - if ( geom->type != LINETYPE && geom->type != CIRCSTRINGTYPE ) { - lwerror("%s cannot contain %s element", lwtype_name(col->type), lwtype_name(geom->type)); - return NULL; - } - } - else if ( col->type == MULTICURVETYPE ) { - /* Allow: linestring, circularstring, compoundcurve */ - if ( geom->type != LINETYPE && geom->type != CIRCSTRINGTYPE && geom->type != COMPOUNDTYPE ) { - lwerror("%s cannot contain %s element", lwtype_name(col->type), lwtype_name(geom->type)); - return NULL; - } + if ( ! lwcollection_allows_subtype(col->type, geom->type) ) { + lwerror("%s cannot contain %s element", lwtype_name(col->type), lwtype_name(geom->type)); + return NULL; } - /* TODO: I'm probably missing something else here... would be nice to have the +3 invariant always */ /* In case this is a truly empty, make some initial space */ if ( col->geoms == NULL ) @@ -521,3 +504,40 @@ LWCOLLECTION* lwcollection_simplify(const LWCOLLECTION *igeom, double dist) return out; } + +int lwcollection_allows_subtype(int collectiontype, int subtype) +{ + if ( collectiontype == COLLECTIONTYPE ) + return LW_TRUE; + if ( collectiontype == MULTIPOINTTYPE && + subtype == POINTTYPE ) + return LW_TRUE; + if ( collectiontype == MULTILINETYPE && + subtype == LINETYPE ) + return LW_TRUE; + if ( collectiontype == MULTIPOLYGONTYPE && + subtype == POLYGONTYPE ) + return LW_TRUE; + if ( collectiontype == COMPOUNDTYPE && + (subtype == LINETYPE || subtype == CIRCSTRINGTYPE) ) + return LW_TRUE; + if ( collectiontype == CURVEPOLYTYPE && + (subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE) ) + return LW_TRUE; + if ( collectiontype == MULTICURVETYPE && + (subtype == CIRCSTRINGTYPE || subtype == LINETYPE || subtype == COMPOUNDTYPE) ) + return LW_TRUE; + if ( collectiontype == MULTISURFACETYPE && + (subtype == POLYGONTYPE || subtype == CURVEPOLYTYPE) ) + return LW_TRUE; + if ( collectiontype == POLYHEDRALSURFACETYPE && + subtype == POLYGONTYPE ) + return LW_TRUE; + if ( collectiontype == TINTYPE && + subtype == TRIANGLETYPE ) + return LW_TRUE; + + /* Must be a bad combination! */ + return LW_FALSE; +} + diff --git a/regress/tickets_expected b/regress/tickets_expected index f514c5450..1574f7437 100644 --- a/regress/tickets_expected +++ b/regress/tickets_expected @@ -166,4 +166,4 @@ ERROR: MultiLineString cannot contain MultiPoint element ERROR: MultiPoint cannot contain MultiPoint element ERROR: CompoundCurve cannot contain MultiPoint element ERROR: MultiCurve cannot contain MultiPoint element -ERROR: Invalid subtype (MultiPoint) for collection type (MultiSurface) +ERROR: MultiSurface cannot contain MultiPoint element