]> granicus.if.org Git - postgis/commitdiff
#3104, st_asgml introduces random characters in ID field
authorPaul Ramsey <pramsey@cleverelephant.ca>
Wed, 29 Apr 2015 12:48:34 +0000 (12:48 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Wed, 29 Apr 2015 12:48:34 +0000 (12:48 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@13456 b70326c6-7e19-0410-871a-916f4a2858ee

postgis/lwgeom_export.c

index c5bfba541b758cc985eacaf1a7d3da858cd7d911..eba0d30dadf3d6700d82455df48f65087291eb12 100644 (file)
@@ -177,8 +177,6 @@ Datum LWGEOM_asGML(PG_FUNCTION_ARGS)
        static const char* default_prefix = "gml:"; /* default prefix */
        const char* prefix = default_prefix;
        const char* gml_id = NULL;
-       char *prefix_buf, *gml_id_buf;
-       text *prefix_text, *gml_id_text;
 
        /* Get the version */
        version = PG_GETARG_INT32(0);
@@ -209,36 +207,36 @@ Datum LWGEOM_asGML(PG_FUNCTION_ARGS)
        /* retrieve prefix */
        if (PG_NARGS() >4 && !PG_ARGISNULL(4))
        {
-               prefix_text = PG_GETARG_TEXT_P(4);
-               if ( VARSIZE(prefix_text)-VARHDRSZ == 0 )
+               text* prefix_text = PG_GETARG_TEXT_P(4);
+               if ( VARSIZE(prefix_text) == VARHDRSZ )
                {
                        prefix = "";
                }
                else
                {
-                       /* +2 is one for the ':' and one for term null */
-                       prefix_buf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2);
-                       memcpy(prefix_buf, VARDATA(prefix_text),
-                              VARSIZE(prefix_text)-VARHDRSZ);
+                       size_t len = VARSIZE(prefix_text)-VARHDRSZ;
+                       char* prefix_buf = palloc(len + 2); /* +2 is one for the ':' and one for term null */
+                       memcpy(prefix_buf, VARDATA(prefix_text), len);
                        /* add colon and null terminate */
-                       prefix_buf[VARSIZE(prefix_text)-VARHDRSZ] = ':';
-                       prefix_buf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0';
+                       prefix_buf[len] = ':';
+                       prefix_buf[len+1] = '\0';
                        prefix = prefix_buf;
                }
        }
 
        if (PG_NARGS() >5 && !PG_ARGISNULL(5))
        {
-               gml_id_text = PG_GETARG_TEXT_P(5);
-               if ( VARSIZE(gml_id_text)-VARHDRSZ == 0 )
+               text* gml_id_text = PG_GETARG_TEXT_P(5);
+               if ( VARSIZE(gml_id_text) == VARHDRSZ )
                {
                        gml_id = "";
                }
                else
                {
-                       gml_id_buf = palloc(VARSIZE(gml_id_text)-VARHDRSZ+1);
-                       memcpy(gml_id_buf, VARDATA(gml_id_text), VARSIZE(gml_id_text)-VARHDRSZ);
-                       gml_id_buf[VARSIZE(gml_id_text)-VARHDRSZ+1] = '\0';
+                       size_t len = VARSIZE(gml_id_text)-VARHDRSZ;
+                       char* gml_id_buf = palloc(len+1);
+                       memcpy(gml_id_buf, VARDATA(gml_id_text), len);
+                       gml_id_buf[len] = '\0';
                        gml_id = gml_id_buf;
                }
        }