Opened 10 years ago

Closed 10 years ago

#2956 closed defect (fixed)

ST_AsTWKB(NULL) crashes the backend

Reported by: strk Owned by: nicklas
Priority: blocker Milestone: PostGIS 2.2.0
Component: postgis Version: master
Keywords: Cc:

Description

select st_astwkb(null,0); — boom

Change History (6)

comment:1 by nicklas, 10 years ago

That's not good.

I think you are right that I haven't protected against that.

Will try to look at it this evening.

comment:2 by nicklas, 10 years ago

What is the right behavior? Error or return null?

comment:3 by strk, 10 years ago

Right behavior is return null. Here's a quick patch (but needs regress test added):

diff --git a/postgis/lwgeom_inout.c b/postgis/lwgeom_inout.c
index 6bf84f9..d924f3a 100644
--- a/postgis/lwgeom_inout.c
+++ b/postgis/lwgeom_inout.c
@@ -406,7 +406,7 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1(TWKBFromLWGEOM);
 Datum TWKBFromLWGEOM(PG_FUNCTION_ARGS)
 {
-       GSERIALIZED *geom = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       GSERIALIZED *geom;
        LWGEOM *lwgeom;
        uint8_t *twkb;
        size_t twkb_size;
@@ -414,6 +414,10 @@ Datum TWKBFromLWGEOM(PG_FUNCTION_ARGS)
        bytea *result;
        int64_t id;
        int prec;
+
+       if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
+
+       geom = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        
        /* If user specified precision, respect it */
        if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )

comment:4 by pramsey, 10 years ago

Or you could just declare the function strict in the SQL?

comment:5 by nicklas, 10 years ago

Actually I just removed strict a few days ago because I used null as default value for id:

CREATE OR REPLACE FUNCTION ST_AsTWKB(geometry,int4,int8 default null,boolean default false, boolean default false)

Maybe that is a bad solution. The reason is that it is not supposed to be any default value. If id is not declared the twkb-geometry is created without any space for id. Since id can be 0 that is not possible to use as default. Any other ideas to do it the cleanest way? Or is it the best way to go to catch the null value in the C-function like in strks patch?

comment:6 by nicklas, 10 years ago

Resolution: fixed
Status: newclosed

Should be fixed with r13054

Note: See TracTickets for help on using tickets.