pgLatLon
changeset 80:b9cdc74a90db
Clear memory of datums on allocation to avoid non-zero garbage in padding on all possible platforms
| author | jbe |
|---|---|
| date | Thu Oct 23 10:39:05 2025 +0200 (46 hours ago) |
| parents | 16787a19a325 |
| children | b0e17a5a0258 |
| files | latlon-v0010.c |
line diff
1.1 --- a/latlon-v0010.c Thu Oct 23 10:16:01 2025 +0200 1.2 +++ b/latlon-v0010.c Thu Oct 23 10:39:05 2025 +0200 1.3 @@ -215,7 +215,7 @@ 1.4 /* determine total number of points */ 1.5 for (i=0; i<nentries; i++) npoints += entries[i].npoints; 1.6 /* allocate memory for cluster (including entries and points) */ 1.7 - cluster = palloc(points_offset + npoints * sizeof(pgl_point)); 1.8 + cluster = palloc0(points_offset + npoints * sizeof(pgl_point)); 1.9 /* re-count total number of points to determine offset for each entry */ 1.10 npoints = 0; 1.11 /* copy entries and points */ 1.12 @@ -1109,7 +1109,7 @@ 1.13 } 1.14 /* otherwise create sample points for numerical integration and determine 1.15 area covered by sample points */ 1.16 - points = palloc(samples * sizeof(pgl_point)); 1.17 + points = palloc0(samples * sizeof(pgl_point)); 1.18 area = pgl_sample_points( 1.19 &cluster->bounding.center, 1.20 cluster->bounding.radius + distance, /* pad bounding circle by distance */ 1.21 @@ -1658,7 +1658,7 @@ 1.22 /* create point ("epoint" in SQL) from latitude and longitude */ 1.23 PG_FUNCTION_INFO_V1(pgl_create_epoint); 1.24 Datum pgl_create_epoint(PG_FUNCTION_ARGS) { 1.25 - pgl_point *point = (pgl_point *)palloc(sizeof(pgl_point)); 1.26 + pgl_point *point = (pgl_point *)palloc0(sizeof(pgl_point)); 1.27 pgl_epoint_set_latlon(point, PG_GETARG_FLOAT8(0), PG_GETARG_FLOAT8(1)); 1.28 PG_RETURN_POINTER(point); 1.29 } 1.30 @@ -1685,7 +1685,7 @@ 1.31 )); 1.32 } 1.33 /* allocate memory for result */ 1.34 - point = (pgl_point *)palloc(sizeof(pgl_point)); 1.35 + point = (pgl_point *)palloc0(sizeof(pgl_point)); 1.36 /* set latitude and longitude (and perform checks) */ 1.37 pgl_epoint_set_latlon(point, lat, lon); 1.38 /* return result */ 1.39 @@ -1718,7 +1718,7 @@ 1.40 ("epoint_with_sample_count" in SQL) from epoint and integer */ 1.41 PG_FUNCTION_INFO_V1(pgl_create_epoint_with_sample_count); 1.42 Datum pgl_create_epoint_with_sample_count(PG_FUNCTION_ARGS) { 1.43 - pgl_point_sc *search = (pgl_point_sc *)palloc(sizeof(pgl_point_sc)); 1.44 + pgl_point_sc *search = (pgl_point_sc *)palloc0(sizeof(pgl_point_sc)); 1.45 search->point = *(pgl_point *)PG_GETARG_POINTER(0); 1.46 pgl_epoint_set_sample_count(search, PG_GETARG_INT32(1)); 1.47 PG_RETURN_POINTER(search); 1.48 @@ -1754,7 +1754,7 @@ 1.49 )); 1.50 } 1.51 /* allocate memory for result */ 1.52 - search = (pgl_point_sc *)palloc(sizeof(pgl_point_sc)); 1.53 + search = (pgl_point_sc *)palloc0(sizeof(pgl_point_sc)); 1.54 /* set latitude, longitude, and sample count (while performing checks) */ 1.55 pgl_epoint_set_latlon(&search->point, lat, lon); 1.56 pgl_epoint_set_sample_count(search, samples); 1.57 @@ -1765,7 +1765,7 @@ 1.58 /* create box ("ebox" in SQL) that is empty */ 1.59 PG_FUNCTION_INFO_V1(pgl_create_empty_ebox); 1.60 Datum pgl_create_empty_ebox(PG_FUNCTION_ARGS) { 1.61 - pgl_box *box = (pgl_box *)palloc(sizeof(pgl_box)); 1.62 + pgl_box *box = (pgl_box *)palloc0(sizeof(pgl_box)); 1.63 pgl_box_set_empty(box); 1.64 PG_RETURN_POINTER(box); 1.65 } 1.66 @@ -1832,7 +1832,7 @@ 1.67 /* create box ("ebox" in SQL) from min/max latitude and min/max longitude */ 1.68 PG_FUNCTION_INFO_V1(pgl_create_ebox); 1.69 Datum pgl_create_ebox(PG_FUNCTION_ARGS) { 1.70 - pgl_box *box = (pgl_box *)palloc(sizeof(pgl_box)); 1.71 + pgl_box *box = (pgl_box *)palloc0(sizeof(pgl_box)); 1.72 pgl_ebox_set_boundaries( 1.73 box, 1.74 PG_GETARG_FLOAT8(0), PG_GETARG_FLOAT8(1), 1.75 @@ -1847,7 +1847,7 @@ 1.76 Datum pgl_create_ebox_from_epoints(PG_FUNCTION_ARGS) { 1.77 pgl_point *point1 = (pgl_point *)PG_GETARG_POINTER(0); 1.78 pgl_point *point2 = (pgl_point *)PG_GETARG_POINTER(1); 1.79 - pgl_box *box = (pgl_box *)palloc(sizeof(pgl_box)); 1.80 + pgl_box *box = (pgl_box *)palloc0(sizeof(pgl_box)); 1.81 double lat_min, lat_max, lon_min, lon_max; 1.82 double dlon; /* longitude range (delta longitude) */ 1.83 /* order latitude and longitude boundaries */ 1.84 @@ -1911,7 +1911,7 @@ 1.85 sscanf(strptr, " empty %n", &valid); 1.86 if (valid && strptr[valid] == 0) { 1.87 /* allocate and return empty box */ 1.88 - box = (pgl_box *)palloc(sizeof(pgl_box)); 1.89 + box = (pgl_box *)palloc0(sizeof(pgl_box)); 1.90 pgl_box_set_empty(box); 1.91 PG_RETURN_POINTER(box); 1.92 } 1.93 @@ -1946,7 +1946,7 @@ 1.94 if (lat_min > lat_max) { val = lat_min; lat_min = lat_max; lat_max = val; } 1.95 if (lon_min > lon_max) { val = lon_min; lon_min = lon_max; lon_max = val; } 1.96 /* allocate memory for result */ 1.97 - box = (pgl_box *)palloc(sizeof(pgl_box)); 1.98 + box = (pgl_box *)palloc0(sizeof(pgl_box)); 1.99 /* set boundaries (and perform checks) */ 1.100 pgl_ebox_set_boundaries(box, lat_min, lat_max, lon_min, lon_max); 1.101 /* return result */ 1.102 @@ -1980,7 +1980,7 @@ 1.103 /* create circle ("ecircle" in SQL) from latitude, longitude, and radius */ 1.104 PG_FUNCTION_INFO_V1(pgl_create_ecircle); 1.105 Datum pgl_create_ecircle(PG_FUNCTION_ARGS) { 1.106 - pgl_circle *circle = (pgl_circle *)palloc(sizeof(pgl_circle)); 1.107 + pgl_circle *circle = (pgl_circle *)palloc0(sizeof(pgl_circle)); 1.108 pgl_ecircle_set_latlon_radius( 1.109 circle, PG_GETARG_FLOAT8(0), PG_GETARG_FLOAT8(1), PG_GETARG_FLOAT8(2) 1.110 ); 1.111 @@ -1992,7 +1992,7 @@ 1.112 Datum pgl_create_ecircle_from_epoint(PG_FUNCTION_ARGS) { 1.113 pgl_point *point = (pgl_point *)PG_GETARG_POINTER(0); 1.114 double radius = PG_GETARG_FLOAT8(1); 1.115 - pgl_circle *circle = (pgl_circle *)palloc(sizeof(pgl_circle)); 1.116 + pgl_circle *circle = (pgl_circle *)palloc0(sizeof(pgl_circle)); 1.117 /* set latitude, longitude, radius (and perform checks) */ 1.118 pgl_ecircle_set_latlon_radius(circle, point->lat, point->lon, radius); 1.119 /* return result */ 1.120 @@ -2028,7 +2028,7 @@ 1.121 )); 1.122 } 1.123 /* allocate memory for result */ 1.124 - circle = (pgl_circle *)palloc(sizeof(pgl_circle)); 1.125 + circle = (pgl_circle *)palloc0(sizeof(pgl_circle)); 1.126 /* set latitude, longitude, radius (and perform checks) */ 1.127 pgl_ecircle_set_latlon_radius(circle, lat, lon, radius); 1.128 /* return result */ 1.129 @@ -2062,7 +2062,7 @@ 1.130 /* reset reading position to start of (lowercase) string */ 1.131 strptr = str_lower; 1.132 /* allocate initial buffer for entries */ 1.133 - entries = palloc(entries_buflen * sizeof(pgl_newentry)); 1.134 + entries = palloc0(entries_buflen * sizeof(pgl_newentry)); 1.135 /* parse until end of string */ 1.136 while (strptr[0]) { 1.137 /* require previous white-space or closing parenthesis before next token */ 1.138 @@ -2107,7 +2107,7 @@ 1.139 if (nentries == entries_buflen) { 1.140 pgl_newentry *newbuf; 1.141 entries_buflen *= 2; 1.142 - newbuf = palloc(entries_buflen * sizeof(pgl_newentry)); 1.143 + newbuf = palloc0(entries_buflen * sizeof(pgl_newentry)); 1.144 memcpy(newbuf, entries, nentries * sizeof(pgl_newentry)); 1.145 pfree(entries); 1.146 entries = newbuf; 1.147 @@ -2116,7 +2116,7 @@ 1.148 npoints = 0; 1.149 /* allocate array for points */ 1.150 points_buflen = 4; 1.151 - points = palloc(points_buflen * sizeof(pgl_point)); 1.152 + points = palloc0(points_buflen * sizeof(pgl_point)); 1.153 /* parse until closing parenthesis */ 1.154 while (strptr[0] != ')') { 1.155 /* error on unexpected end of string */ 1.156 @@ -2147,7 +2147,7 @@ 1.157 if (npoints == points_buflen) { 1.158 pgl_point *newbuf; 1.159 points_buflen *= 2; 1.160 - newbuf = palloc(points_buflen * sizeof(pgl_point)); 1.161 + newbuf = palloc0(points_buflen * sizeof(pgl_point)); 1.162 memcpy(newbuf, points, npoints * sizeof(pgl_point)); 1.163 pfree(points); 1.164 points = newbuf; 1.165 @@ -2358,7 +2358,7 @@ 1.166 PG_FUNCTION_INFO_V1(pgl_epoint_recv); 1.167 Datum pgl_epoint_recv(PG_FUNCTION_ARGS) { 1.168 StringInfo buf = (StringInfo)PG_GETARG_POINTER(0); 1.169 - pgl_point *point = (pgl_point *)palloc(sizeof(pgl_point)); 1.170 + pgl_point *point = (pgl_point *)palloc0(sizeof(pgl_point)); 1.171 point->lat = pq_getmsgfloat8(buf); 1.172 point->lon = pq_getmsgfloat8(buf); 1.173 PG_RETURN_POINTER(point); 1.174 @@ -2368,7 +2368,7 @@ 1.175 PG_FUNCTION_INFO_V1(pgl_ebox_recv); 1.176 Datum pgl_ebox_recv(PG_FUNCTION_ARGS) { 1.177 StringInfo buf = (StringInfo)PG_GETARG_POINTER(0); 1.178 - pgl_box *box = (pgl_box *)palloc(sizeof(pgl_box)); 1.179 + pgl_box *box = (pgl_box *)palloc0(sizeof(pgl_box)); 1.180 box->lat_min = pq_getmsgfloat8(buf); 1.181 box->lat_max = pq_getmsgfloat8(buf); 1.182 box->lon_min = pq_getmsgfloat8(buf); 1.183 @@ -2380,7 +2380,7 @@ 1.184 PG_FUNCTION_INFO_V1(pgl_ecircle_recv); 1.185 Datum pgl_ecircle_recv(PG_FUNCTION_ARGS) { 1.186 StringInfo buf = (StringInfo)PG_GETARG_POINTER(0); 1.187 - pgl_circle *circle = (pgl_circle *)palloc(sizeof(pgl_circle)); 1.188 + pgl_circle *circle = (pgl_circle *)palloc0(sizeof(pgl_circle)); 1.189 circle->center.lat = pq_getmsgfloat8(buf); 1.190 circle->center.lon = pq_getmsgfloat8(buf); 1.191 circle->radius = pq_getmsgfloat8(buf); 1.192 @@ -2431,7 +2431,7 @@ 1.193 PG_FUNCTION_INFO_V1(pgl_epoint_to_ebox); 1.194 Datum pgl_epoint_to_ebox(PG_FUNCTION_ARGS) { 1.195 pgl_point *point = (pgl_point *)PG_GETARG_POINTER(0); 1.196 - pgl_box *box = palloc(sizeof(pgl_box)); 1.197 + pgl_box *box = palloc0(sizeof(pgl_box)); 1.198 box->lat_min = point->lat; 1.199 box->lat_max = point->lat; 1.200 box->lon_min = point->lon; 1.201 @@ -2443,7 +2443,7 @@ 1.202 PG_FUNCTION_INFO_V1(pgl_epoint_to_ecircle); 1.203 Datum pgl_epoint_to_ecircle(PG_FUNCTION_ARGS) { 1.204 pgl_point *point = (pgl_point *)PG_GETARG_POINTER(0); 1.205 - pgl_circle *circle = palloc(sizeof(pgl_box)); 1.206 + pgl_circle *circle = palloc0(sizeof(pgl_box)); 1.207 circle->center = *point; 1.208 circle->radius = 0; 1.209 PG_RETURN_POINTER(circle);