mirror of
https://github.com/postgres/postgres.git
synced 2025-08-19 23:22:23 +03:00
Use FLEXIBLE_ARRAY_MEMBER in a bunch more places.
Replace some bogus "x[1]" declarations with "x[FLEXIBLE_ARRAY_MEMBER]". Aside from being more self-documenting, this should help prevent bogus warnings from static code analyzers and perhaps compiler misoptimizations. This patch is just a down payment on eliminating the whole problem, but it gets rid of a lot of easy-to-fix cases. Note that the main problem with doing this is that one must no longer rely on computing sizeof(the containing struct), since the result would be compiler-dependent. Instead use offsetof(struct, lastfield). Autoconf also warns against spelling that offsetof(struct, lastfield[0]). Michael Paquier, review and additional fixes by me.
This commit is contained in:
@@ -1390,7 +1390,7 @@ path_in(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
base_size = sizeof(path->p[0]) * npts;
|
||||
size = offsetof(PATH, p[0]) +base_size;
|
||||
size = offsetof(PATH, p) +base_size;
|
||||
|
||||
/* Check for integer overflow */
|
||||
if (base_size / npts != sizeof(path->p[0]) || size <= base_size)
|
||||
@@ -1443,12 +1443,12 @@ path_recv(PG_FUNCTION_ARGS)
|
||||
|
||||
closed = pq_getmsgbyte(buf);
|
||||
npts = pq_getmsgint(buf, sizeof(int32));
|
||||
if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(PATH, p[0])) / sizeof(Point)))
|
||||
if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(PATH, p)) / sizeof(Point)))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
||||
errmsg("invalid number of points in external \"path\" value")));
|
||||
|
||||
size = offsetof(PATH, p[0]) +sizeof(path->p[0]) * npts;
|
||||
size = offsetof(PATH, p) +sizeof(path->p[0]) * npts;
|
||||
path = (PATH *) palloc(size);
|
||||
|
||||
SET_VARSIZE(path, size);
|
||||
@@ -3476,7 +3476,7 @@ poly_in(PG_FUNCTION_ARGS)
|
||||
errmsg("invalid input syntax for type polygon: \"%s\"", str)));
|
||||
|
||||
base_size = sizeof(poly->p[0]) * npts;
|
||||
size = offsetof(POLYGON, p[0]) +base_size;
|
||||
size = offsetof(POLYGON, p) +base_size;
|
||||
|
||||
/* Check for integer overflow */
|
||||
if (base_size / npts != sizeof(poly->p[0]) || size <= base_size)
|
||||
@@ -3530,12 +3530,12 @@ poly_recv(PG_FUNCTION_ARGS)
|
||||
int size;
|
||||
|
||||
npts = pq_getmsgint(buf, sizeof(int32));
|
||||
if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(POLYGON, p[0])) / sizeof(Point)))
|
||||
if (npts <= 0 || npts >= (int32) ((INT_MAX - offsetof(POLYGON, p)) / sizeof(Point)))
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
|
||||
errmsg("invalid number of points in external \"polygon\" value")));
|
||||
|
||||
size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * npts;
|
||||
size = offsetof(POLYGON, p) +sizeof(poly->p[0]) * npts;
|
||||
poly = (POLYGON *) palloc0(size); /* zero any holes */
|
||||
|
||||
SET_VARSIZE(poly, size);
|
||||
@@ -4251,7 +4251,7 @@ path_add(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_NULL();
|
||||
|
||||
base_size = sizeof(p1->p[0]) * (p1->npts + p2->npts);
|
||||
size = offsetof(PATH, p[0]) +base_size;
|
||||
size = offsetof(PATH, p) +base_size;
|
||||
|
||||
/* Check for integer overflow */
|
||||
if (base_size / sizeof(p1->p[0]) != (p1->npts + p2->npts) ||
|
||||
@@ -4393,7 +4393,7 @@ path_poly(PG_FUNCTION_ARGS)
|
||||
* Never overflows: the old size fit in MaxAllocSize, and the new size is
|
||||
* just a small constant larger.
|
||||
*/
|
||||
size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * path->npts;
|
||||
size = offsetof(POLYGON, p) +sizeof(poly->p[0]) * path->npts;
|
||||
poly = (POLYGON *) palloc(size);
|
||||
|
||||
SET_VARSIZE(poly, size);
|
||||
@@ -4468,7 +4468,7 @@ box_poly(PG_FUNCTION_ARGS)
|
||||
int size;
|
||||
|
||||
/* map four corners of the box to a polygon */
|
||||
size = offsetof(POLYGON, p[0]) +sizeof(poly->p[0]) * 4;
|
||||
size = offsetof(POLYGON, p) +sizeof(poly->p[0]) * 4;
|
||||
poly = (POLYGON *) palloc(size);
|
||||
|
||||
SET_VARSIZE(poly, size);
|
||||
@@ -4502,7 +4502,7 @@ poly_path(PG_FUNCTION_ARGS)
|
||||
* Never overflows: the old size fit in MaxAllocSize, and the new size is
|
||||
* smaller by a small constant.
|
||||
*/
|
||||
size = offsetof(PATH, p[0]) +sizeof(path->p[0]) * poly->npts;
|
||||
size = offsetof(PATH, p) +sizeof(path->p[0]) * poly->npts;
|
||||
path = (PATH *) palloc(size);
|
||||
|
||||
SET_VARSIZE(path, size);
|
||||
@@ -5181,7 +5181,7 @@ circle_poly(PG_FUNCTION_ARGS)
|
||||
errmsg("must request at least 2 points")));
|
||||
|
||||
base_size = sizeof(poly->p[0]) * npts;
|
||||
size = offsetof(POLYGON, p[0]) +base_size;
|
||||
size = offsetof(POLYGON, p) +base_size;
|
||||
|
||||
/* Check for integer overflow */
|
||||
if (base_size / npts != sizeof(poly->p[0]) || size <= base_size)
|
||||
|
Reference in New Issue
Block a user