From d9b0ee7a966f4cc905450dc25520400a18cbd835 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Fri, 22 Feb 2019 16:51:34 +0000 Subject: [PATCH] Replace VARSIZE(foo)-VARHDRSZ pattern with VARSIZE_ANY_EXHDR(foo) macro from standard Pg. git-svn-id: http://svn.osgeo.org/postgis/trunk@17271 b70326c6-7e19-0410-871a-916f4a2858ee --- .../address_standardizer/address_parser.c | 10 +------ .../address_standardizer.c | 26 ++++++---------- postgis/geography_inout.c | 30 +++++++++---------- postgis/gserialized_estimate.c | 2 +- postgis/gserialized_gist_nd.c | 2 +- postgis/lwgeom_export.c | 24 +++++++-------- postgis/lwgeom_in_geojson.c | 2 +- postgis/lwgeom_in_gml.c | 2 +- postgis/lwgeom_in_kml.c | 2 +- postgis/lwgeom_inout.c | 6 ++-- postgis/lwgeom_ogc.c | 2 +- postgis/mvt.c | 2 +- raster/rt_pg/rtpg_gdal.c | 4 +-- raster/rt_pg/rtpg_inout.c | 2 +- raster/rt_pg/rtpg_wkb.c | 4 +-- 15 files changed, 52 insertions(+), 68 deletions(-) diff --git a/extensions/address_standardizer/address_parser.c b/extensions/address_standardizer/address_parser.c index c6c597625..184cfc8cf 100644 --- a/extensions/address_standardizer/address_parser.c +++ b/extensions/address_standardizer/address_parser.c @@ -22,14 +22,6 @@ Datum parse_address(PG_FUNCTION_ARGS); -static char *text2char(text *in) -{ - char *out = palloc(VARSIZE(in)); - memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ); - out[VARSIZE(in) - VARHDRSZ] = '\0'; - return out; -} - PG_FUNCTION_INFO_V1(parse_address); Datum parse_address(PG_FUNCTION_ARGS) @@ -47,7 +39,7 @@ Datum parse_address(PG_FUNCTION_ARGS) DBG("Start standardize_address"); - str = text2char(PG_GETARG_TEXT_P(0)); + str = text_to_cstring(PG_GETARG_TEXT_P(0)); DBG("str='%s'", str); diff --git a/extensions/address_standardizer/address_standardizer.c b/extensions/address_standardizer/address_standardizer.c index b1703995c..dbfd21ed6 100644 --- a/extensions/address_standardizer/address_standardizer.c +++ b/extensions/address_standardizer/address_standardizer.c @@ -19,14 +19,6 @@ Datum standardize_address(PG_FUNCTION_ARGS); Datum standardize_address1(PG_FUNCTION_ARGS); -static char *text2char(text *in) -{ - char *out = palloc(VARSIZE(in)); - memcpy(out, VARDATA(in), VARSIZE(in) - VARHDRSZ); - out[VARSIZE(in) - VARHDRSZ] = '\0'; - return out; -} - /* * The signature for standardize_address follows. The lextab, gaztab and * rultab should not change once the reference has been standardized and @@ -80,11 +72,11 @@ Datum standardize_address(PG_FUNCTION_ARGS) DBG("Start standardize_address"); - lextab = text2char(PG_GETARG_TEXT_P(0)); - gaztab = text2char(PG_GETARG_TEXT_P(1)); - rultab = text2char(PG_GETARG_TEXT_P(2)); - micro = text2char(PG_GETARG_TEXT_P(3)); - macro = text2char(PG_GETARG_TEXT_P(4)); + lextab = text_to_cstring(PG_GETARG_TEXT_P(0)); + gaztab = text_to_cstring(PG_GETARG_TEXT_P(1)); + rultab = text_to_cstring(PG_GETARG_TEXT_P(2)); + micro = text_to_cstring(PG_GETARG_TEXT_P(3)); + macro = text_to_cstring(PG_GETARG_TEXT_P(4)); DBG("calling RelationNameGetTupleDesc"); if (get_call_result_type( fcinfo, NULL, &tuple_desc ) != TYPEFUNC_COMPOSITE ) { @@ -167,10 +159,10 @@ Datum standardize_address1(PG_FUNCTION_ARGS) DBG("Start standardize_address"); - lextab = text2char(PG_GETARG_TEXT_P(0)); - gaztab = text2char(PG_GETARG_TEXT_P(1)); - rultab = text2char(PG_GETARG_TEXT_P(2)); - addr = text2char(PG_GETARG_TEXT_P(3)); + lextab = text_to_cstring(PG_GETARG_TEXT_P(0)); + gaztab = text_to_cstring(PG_GETARG_TEXT_P(1)); + rultab = text_to_cstring(PG_GETARG_TEXT_P(2)); + addr = text_to_cstring(PG_GETARG_TEXT_P(3)); DBG("calling RelationNameGetTupleDesc"); if (get_call_result_type( fcinfo, NULL, &tuple_desc ) != TYPEFUNC_COMPOSITE ) { diff --git a/postgis/geography_inout.c b/postgis/geography_inout.c index 844e828d8..ad0deb838 100644 --- a/postgis/geography_inout.c +++ b/postgis/geography_inout.c @@ -260,19 +260,19 @@ Datum geography_as_gml(PG_FUNCTION_ARGS) if (PG_NARGS() >4 && !PG_ARGISNULL(4)) { prefix_text = PG_GETARG_TEXT_P(4); - if ( VARSIZE(prefix_text)-VARHDRSZ == 0 ) + if ( VARSIZE_ANY_EXHDR(prefix_text) == 0 ) { 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); + prefix_buf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2); + memcpy(prefix_buf, VARDATA_ANY(prefix_text), + VARSIZE_ANY_EXHDR(prefix_text)); /* add colon and null terminate */ - prefix_buf[VARSIZE(prefix_text)-VARHDRSZ] = ':'; - prefix_buf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0'; + prefix_buf[VARSIZE_ANY_EXHDR(prefix_text)] = ':'; + prefix_buf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0'; prefix = prefix_buf; } } @@ -281,15 +281,15 @@ Datum geography_as_gml(PG_FUNCTION_ARGS) if (PG_NARGS() >5 && !PG_ARGISNULL(5)) { id_text = PG_GETARG_TEXT_P(5); - if ( VARSIZE(id_text)-VARHDRSZ == 0 ) + if ( VARSIZE_ANY_EXHDR(id_text) == 0 ) { id = ""; } else { - id_buf = palloc(VARSIZE(id_text)-VARHDRSZ+1); - memcpy(id_buf, VARDATA(id_text), VARSIZE(id_text)-VARHDRSZ); - prefix_buf[VARSIZE(id_text)-VARHDRSZ+1] = '\0'; + id_buf = palloc(VARSIZE_ANY_EXHDR(id_text)+1); + memcpy(id_buf, VARDATA(id_text), VARSIZE_ANY_EXHDR(id_text)); + prefix_buf[VARSIZE_ANY_EXHDR(id_text)+1] = '\0'; id = id_buf; } } @@ -389,19 +389,19 @@ Datum geography_as_kml(PG_FUNCTION_ARGS) if (PG_NARGS() >3 && !PG_ARGISNULL(3)) { prefix_text = PG_GETARG_TEXT_P(3); - if ( VARSIZE(prefix_text)-VARHDRSZ == 0 ) + if ( VARSIZE_ANY_EXHDR(prefix_text) == 0 ) { prefix = ""; } else { /* +2 is one for the ':' and one for term null */ - prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2); + prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2); memcpy(prefixbuf, VARDATA(prefix_text), - VARSIZE(prefix_text)-VARHDRSZ); + VARSIZE_ANY_EXHDR(prefix_text)); /* add colon and null terminate */ - prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':'; - prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0'; + prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':'; + prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0'; prefix = prefixbuf; } } diff --git a/postgis/gserialized_estimate.c b/postgis/gserialized_estimate.c index 93ba45abf..b273cdcb4 100644 --- a/postgis/gserialized_estimate.c +++ b/postgis/gserialized_estimate.c @@ -304,7 +304,7 @@ text_p_get_mode(const text *txt) { int mode = 2; char *modestr; - if (VARSIZE(txt) - VARHDRSZ <= 0) + if (VARSIZE_ANY_EXHDR(txt) <= 0) return mode; modestr = (char*)VARDATA(txt); if ( modestr[0] == 'N' ) diff --git a/postgis/gserialized_gist_nd.c b/postgis/gserialized_gist_nd.c index e65060c9a..b9dc2649b 100644 --- a/postgis/gserialized_gist_nd.c +++ b/postgis/gserialized_gist_nd.c @@ -141,7 +141,7 @@ gidx_validate(GIDX *b) inline bool gidx_is_unknown(const GIDX *a) { - size_t size = VARSIZE(a) - VARHDRSZ; + size_t size = VARSIZE_ANY_EXHDR(a); /* "unknown" gidx objects have a too-small size of one float */ if (size <= 0.0) return true; diff --git a/postgis/lwgeom_export.c b/postgis/lwgeom_export.c index 248f383f8..a1bfb50da 100644 --- a/postgis/lwgeom_export.c +++ b/postgis/lwgeom_export.c @@ -233,7 +233,7 @@ Datum LWGEOM_asGML(PG_FUNCTION_ARGS) } else { - len = VARSIZE(prefix_text)-VARHDRSZ; + len = VARSIZE_ANY_EXHDR(prefix_text); 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 */ @@ -252,7 +252,7 @@ Datum LWGEOM_asGML(PG_FUNCTION_ARGS) } else { - len = VARSIZE(gml_id_text)-VARHDRSZ; + len = VARSIZE_ANY_EXHDR(gml_id_text); gml_id_buf = palloc(len+1); memcpy(gml_id_buf, VARDATA(gml_id_text), len); gml_id_buf[len] = '\0'; @@ -355,19 +355,19 @@ Datum LWGEOM_asKML(PG_FUNCTION_ARGS) if (PG_NARGS() >3 && !PG_ARGISNULL(3)) { prefix_text = PG_GETARG_TEXT_P(3); - if ( VARSIZE(prefix_text)-VARHDRSZ == 0 ) + if ( VARSIZE_ANY_EXHDR(prefix_text) == 0 ) { prefix = ""; } else { /* +2 is one for the ':' and one for term null */ - prefixbuf = palloc(VARSIZE(prefix_text)-VARHDRSZ+2); + prefixbuf = palloc(VARSIZE_ANY_EXHDR(prefix_text)+2); memcpy(prefixbuf, VARDATA(prefix_text), - VARSIZE(prefix_text)-VARHDRSZ); + VARSIZE_ANY_EXHDR(prefix_text)); /* add colon and null terminate */ - prefixbuf[VARSIZE(prefix_text)-VARHDRSZ] = ':'; - prefixbuf[VARSIZE(prefix_text)-VARHDRSZ+1] = '\0'; + prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)] = ':'; + prefixbuf[VARSIZE_ANY_EXHDR(prefix_text)+1] = '\0'; prefix = prefixbuf; } } @@ -581,19 +581,19 @@ Datum LWGEOM_asX3D(PG_FUNCTION_ARGS) if (PG_NARGS() >4 && !PG_ARGISNULL(4)) { defid_text = PG_GETARG_TEXT_P(4); - if ( VARSIZE(defid_text)-VARHDRSZ == 0 ) + if ( VARSIZE_ANY_EXHDR(defid_text) == 0 ) { defid = ""; } else { /* +2 is one for the ':' and one for term null */ - defidbuf = palloc(VARSIZE(defid_text)-VARHDRSZ+2); + defidbuf = palloc(VARSIZE_ANY_EXHDR(defid_text)+2); memcpy(defidbuf, VARDATA(defid_text), - VARSIZE(defid_text)-VARHDRSZ); + VARSIZE_ANY_EXHDR(defid_text)); /* add colon and null terminate */ - defidbuf[VARSIZE(defid_text)-VARHDRSZ] = ':'; - defidbuf[VARSIZE(defid_text)-VARHDRSZ+1] = '\0'; + defidbuf[VARSIZE_ANY_EXHDR(defid_text)] = ':'; + defidbuf[VARSIZE_ANY_EXHDR(defid_text)+1] = '\0'; defid = defidbuf; } } diff --git a/postgis/lwgeom_in_geojson.c b/postgis/lwgeom_in_geojson.c index c4d454d62..c39d9dd5e 100644 --- a/postgis/lwgeom_in_geojson.c +++ b/postgis/lwgeom_in_geojson.c @@ -51,7 +51,7 @@ cstring2text(const char *cstring) static char* text2cstring(const text *textptr) { - size_t size = VARSIZE(textptr) - VARHDRSZ; + size_t size = VARSIZE_ANY_EXHDR(textptr); char *str = lwalloc(size+1); memcpy(str, VARDATA(textptr), size); str[size]='\0'; diff --git a/postgis/lwgeom_in_gml.c b/postgis/lwgeom_in_gml.c index 6603a177c..cf9bd259b 100644 --- a/postgis/lwgeom_in_gml.c +++ b/postgis/lwgeom_in_gml.c @@ -108,7 +108,7 @@ Datum geom_from_gml(PG_FUNCTION_ARGS) if (PG_ARGISNULL(0)) PG_RETURN_NULL(); xml_input = PG_GETARG_TEXT_P(0); xml = text_to_cstring(xml_input); - xml_size = VARSIZE(xml_input) - VARHDRSZ; + xml_size = VARSIZE_ANY_EXHDR(xml_input); /* Zero for undefined */ root_srid = PG_GETARG_INT32(1); diff --git a/postgis/lwgeom_in_kml.c b/postgis/lwgeom_in_kml.c index 52a7820ef..068891819 100644 --- a/postgis/lwgeom_in_kml.c +++ b/postgis/lwgeom_in_kml.c @@ -86,7 +86,7 @@ Datum geom_from_kml(PG_FUNCTION_ARGS) if (PG_ARGISNULL(0)) PG_RETURN_NULL(); xml_input = PG_GETARG_TEXT_P(0); xml = text_to_cstring(xml_input); - xml_size = VARSIZE(xml_input) - VARHDRSZ; + xml_size = VARSIZE_ANY_EXHDR(xml_input); /* Begin to Parse XML doc */ xmlInitParser(); diff --git a/postgis/lwgeom_inout.c b/postgis/lwgeom_inout.c index d822fc802..9a86c1f21 100644 --- a/postgis/lwgeom_inout.c +++ b/postgis/lwgeom_inout.c @@ -374,7 +374,7 @@ Datum LWGEOMFromEWKB(PG_FUNCTION_ARGS) LWGEOM *lwgeom; uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb); - lwgeom = lwgeom_from_wkb(wkb, VARSIZE(bytea_wkb)-VARHDRSZ, LW_PARSER_CHECK_ALL); + lwgeom = lwgeom_from_wkb(wkb, VARSIZE_ANY_EXHDR(bytea_wkb), LW_PARSER_CHECK_ALL); if ( ( PG_NARGS()>1) && ( ! PG_ARGISNULL(1) )) { @@ -403,7 +403,7 @@ Datum LWGEOMFromTWKB(PG_FUNCTION_ARGS) LWGEOM *lwgeom; uint8_t *twkb = (uint8_t*)VARDATA(bytea_twkb); - lwgeom = lwgeom_from_twkb(twkb, VARSIZE(bytea_twkb)-VARHDRSZ, LW_PARSER_CHECK_ALL); + lwgeom = lwgeom_from_twkb(twkb, VARSIZE_ANY_EXHDR(bytea_twkb), LW_PARSER_CHECK_ALL); if ( lwgeom_needs_bbox(lwgeom) ) lwgeom_add_bbox(lwgeom); @@ -443,7 +443,7 @@ Datum WKBFromLWGEOM(PG_FUNCTION_ARGS) variant = variant | WKB_NDR; } } - wkb_size= VARSIZE(geom) - VARHDRSZ; + wkb_size= VARSIZE_ANY_EXHDR(geom); /* Create WKB hex string */ lwgeom = lwgeom_from_gserialized(geom); diff --git a/postgis/lwgeom_ogc.c b/postgis/lwgeom_ogc.c index 8cdc5fcc7..de294dae2 100644 --- a/postgis/lwgeom_ogc.c +++ b/postgis/lwgeom_ogc.c @@ -830,7 +830,7 @@ Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS) LWGEOM *lwgeom; uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb); - lwgeom = lwgeom_from_wkb(wkb, VARSIZE(bytea_wkb)-VARHDRSZ, LW_PARSER_CHECK_ALL); + lwgeom = lwgeom_from_wkb(wkb, VARSIZE_ANY_EXHDR(bytea_wkb), LW_PARSER_CHECK_ALL); if ( lwgeom_needs_bbox(lwgeom) ) lwgeom_add_bbox(lwgeom); diff --git a/postgis/mvt.c b/postgis/mvt.c index 5c4be1eb1..ce206c38a 100644 --- a/postgis/mvt.c +++ b/postgis/mvt.c @@ -1183,7 +1183,7 @@ mvt_agg_context * mvt_ctx_deserialize(const bytea *ba) NULL }; - size_t len = VARSIZE(ba) - VARHDRSZ; + size_t len = VARSIZE_ANY_EXHDR(ba); VectorTile__Tile *tile = vector_tile__tile__unpack(&allocator, len, (uint8_t*)VARDATA(ba)); mvt_agg_context *ctx = palloc(sizeof(mvt_agg_context)); memset(ctx, 0, sizeof(mvt_agg_context)); diff --git a/raster/rt_pg/rtpg_gdal.c b/raster/rt_pg/rtpg_gdal.c index 94f0bc151..31263a5e7 100644 --- a/raster/rt_pg/rtpg_gdal.c +++ b/raster/rt_pg/rtpg_gdal.c @@ -77,7 +77,7 @@ Datum RASTER_fromGDALRaster(PG_FUNCTION_ARGS) /* get data */ bytea_data = (bytea *) PG_GETARG_BYTEA_P(0); data = (uint8_t *) VARDATA(bytea_data); - data_len = VARSIZE(bytea_data) - VARHDRSZ; + data_len = VARSIZE_ANY_EXHDR(bytea_data); /* process srid */ /* NULL srid means try to determine SRID from bytea */ @@ -321,7 +321,7 @@ Datum RASTER_asGDALRaster(PG_FUNCTION_ARGS) PG_RETURN_NULL(); } SET_VARSIZE(result, result_size); - memcpy(VARDATA(result), gdal, VARSIZE(result) - VARHDRSZ); + memcpy(VARDATA(result), gdal, VARSIZE_ANY_EXHDR(result)); /* free gdal mem buffer */ CPLFree(gdal); diff --git a/raster/rt_pg/rtpg_inout.c b/raster/rt_pg/rtpg_inout.c index 5d0dce497..b959cf204 100644 --- a/raster/rt_pg/rtpg_inout.c +++ b/raster/rt_pg/rtpg_inout.c @@ -142,7 +142,7 @@ Datum RASTER_to_bytea(PG_FUNCTION_ARGS) result_size = wkb_size + VARHDRSZ; result = (bytea *)palloc(result_size); SET_VARSIZE(result, result_size); - memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ); + memcpy(VARDATA(result), wkb, VARSIZE_ANY_EXHDR(result)); /* Free raster objects used */ rt_raster_destroy(raster); diff --git a/raster/rt_pg/rtpg_wkb.c b/raster/rt_pg/rtpg_wkb.c index c43d446c1..6cf2b8a95 100644 --- a/raster/rt_pg/rtpg_wkb.c +++ b/raster/rt_pg/rtpg_wkb.c @@ -81,7 +81,7 @@ Datum RASTER_asWKB(PG_FUNCTION_ARGS) result_size = wkb_size + VARHDRSZ; result = (char *)palloc(result_size); SET_VARSIZE(result, result_size); - memcpy(VARDATA(result), wkb, VARSIZE(result) - VARHDRSZ); + memcpy(VARDATA(result), wkb, VARSIZE_ANY_EXHDR(result)); /* Free raster objects used */ rt_raster_destroy(raster); @@ -153,7 +153,7 @@ Datum RASTER_fromWKB(PG_FUNCTION_ARGS) bytea_data = (bytea *) PG_GETARG_BYTEA_P(0); data = (uint8_t *) VARDATA(bytea_data); - data_len = VARSIZE(bytea_data) - VARHDRSZ; + data_len = VARSIZE_ANY_EXHDR(bytea_data); raster = rt_raster_from_wkb(data, data_len); PG_FREE_IF_COPY(bytea_data, 0); -- 2.40.0