From e8400232697eebf461aba314627c12b4ae4bdbec Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 11 Jan 2005 15:09:32 +0000 Subject: [PATCH] Added SRID checks for all operators. git-svn-id: http://svn.osgeo.org/postgis/trunk@1271 b70326c6-7e19-0410-871a-916f4a2858ee --- lwgeom/lwgeom_gist.c | 115 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 91 insertions(+), 24 deletions(-) diff --git a/lwgeom/lwgeom_gist.c b/lwgeom/lwgeom_gist.c index 9914c5c5e..1dcf18541 100644 --- a/lwgeom/lwgeom_gist.c +++ b/lwgeom/lwgeom_gist.c @@ -86,12 +86,19 @@ int counter_intern = 0; PG_FUNCTION_INFO_V1(LWGEOM_overlap); Datum LWGEOM_overlap(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { // One or both are empty geoms @@ -124,8 +131,8 @@ Datum LWGEOM_overlap(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_overleft); Datum LWGEOM_overleft(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -134,6 +141,12 @@ Datum LWGEOM_overleft(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_overleft --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -152,8 +165,8 @@ Datum LWGEOM_overleft(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_left); Datum LWGEOM_left(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -162,6 +175,12 @@ Datum LWGEOM_left(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_left --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -180,8 +199,8 @@ Datum LWGEOM_left(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_right); Datum LWGEOM_right(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -190,6 +209,12 @@ Datum LWGEOM_right(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_right --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -208,8 +233,8 @@ Datum LWGEOM_right(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_overright); Datum LWGEOM_overright(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -218,6 +243,12 @@ Datum LWGEOM_overright(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_overright --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -236,8 +267,8 @@ Datum LWGEOM_overright(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_overbelow); Datum LWGEOM_overbelow(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -246,6 +277,12 @@ Datum LWGEOM_overbelow(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_overbelow --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -264,8 +301,8 @@ Datum LWGEOM_overbelow(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_below); Datum LWGEOM_below(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -274,6 +311,12 @@ Datum LWGEOM_below(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_below --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -292,8 +335,8 @@ Datum LWGEOM_below(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_above); Datum LWGEOM_above(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -302,6 +345,12 @@ Datum LWGEOM_above(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_above --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -320,8 +369,8 @@ Datum LWGEOM_above(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_overabove); Datum LWGEOM_overabove(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -330,6 +379,12 @@ Datum LWGEOM_overabove(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_overabove --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -348,8 +403,8 @@ Datum LWGEOM_overabove(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_contained); Datum LWGEOM_contained(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -358,6 +413,12 @@ Datum LWGEOM_contained(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_contained --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -376,8 +437,8 @@ Datum LWGEOM_contained(PG_FUNCTION_ARGS) PG_FUNCTION_INFO_V1(LWGEOM_contain); Datum LWGEOM_contain(PG_FUNCTION_ARGS) { - char *lwgeom1 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); - char *lwgeom2 = (char *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); + char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); bool result; BOX2DFLOAT4 box1; BOX2DFLOAT4 box2; @@ -386,6 +447,12 @@ Datum LWGEOM_contain(PG_FUNCTION_ARGS) elog(NOTICE,"GIST: LWGEOM_contain --entry"); #endif + if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) ) + { + elog(ERROR, "Operation on two geometries with different SRIDs"); + PG_RETURN_NULL(); + } + if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) ) { PG_RETURN_BOOL(FALSE); @@ -433,7 +500,7 @@ Datum LWGEOM_gist_compress(PG_FUNCTION_ARGS) in = (char*)PG_DETOAST_DATUM(entry->key); #ifdef DEBUG_GIST4 - elog(NOTICE,"GIST: LWGEOM_gist_compress detoasted entry->key: %s", unparse_WKT(SERIALIZED_FORM(in), malloc, free)); + elog(NOTICE,"GIST: LWGEOM_gist_compress detoasted entry->key: %s", unparse_WKT(in+4, malloc, free)); #endif if (in == NULL) @@ -539,7 +606,7 @@ Datum LWGEOM_gist_consistent(PG_FUNCTION_ARGS) PG_RETURN_BOOL(false); // null query - this is screwy! } - query = (char*) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); + query = (char*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); if ( ! (DatumGetPointer(entry->key) != NULL && query) ) -- 2.40.0