]> granicus.if.org Git - postgis/commitdiff
Use a standard lwcollection_allows_subtype function to guard against bad input.
authorSandro Santilli <strk@keybit.net>
Tue, 10 Jan 2012 13:17:21 +0000 (13:17 +0000)
committerSandro Santilli <strk@keybit.net>
Tue, 10 Jan 2012 13:17:21 +0000 (13:17 +0000)
Fixes #698 (and #1445 in a better way)

git-svn-id: http://svn.osgeo.org/postgis/trunk@8747 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/g_serialized.c
liblwgeom/liblwgeom_internal.h
liblwgeom/lwcollection.c
regress/tickets_expected

index 8d10e2a0dcd31c96011450105bebdf437a61b08e..e706c977f2de922fa10a6da5dc489b9d70992876 100644 (file)
@@ -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);
index 20a6a5f3ea2a3577d750e777cfd325f7bb091f6d..d050fbf1ff5e6f5c77add0d2af059a90cf8a09ef 100644 (file)
@@ -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 */
index f86d3906e8a8ffbfcef03b98d08cad9bdc589563..304f4ac0b380322c711d229b69d0ca73c9305197 100644 (file)
@@ -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;
+}
+
index f514c5450bf9cdaca5cad7dc24134e8f05d59e5b..1574f7437a3344444369868da20fe8e6327e81bb 100644 (file)
@@ -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