aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/tsgistidx.c
diff options
context:
space:
mode:
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>2024-08-06 23:05:25 +0300
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>2024-08-06 23:05:25 +0300
commit3ab2668d48476e31d8809fbb36c9ee739cc444e1 (patch)
treeccf1d029d16b9333616aff847ca51e15c9f1a3c6 /src/backend/utils/adt/tsgistidx.c
parentd5f139cb6814f0af2d2e1106899361e45c305630 (diff)
downloadpostgresql-3ab2668d48476e31d8809fbb36c9ee739cc444e1.tar.gz
postgresql-3ab2668d48476e31d8809fbb36c9ee739cc444e1.zip
Use psprintf to simplify gtsvectorout()
The buffer allocation was correct, but looked archaic and scary: - It was weird to calculate the buffer size before determining which format string was used. With the same effort, we could've used the right-sized buffer for each branch. - Commit aa0d3504560 added one more possible return string ("all true bits"), but didn't adjust the code at the top of the function to calculate the returned string's max size. It was not a live bug, because the new string was smaller than the existing ones, but seemed wrong in principle. - Use of sprintf() is generally eyebrow-raising these days Switch to psprintf(). psprintf() allocates a larger buffer than what was allocated before, 128 bytes vs 80 bytes, which is acceptable as this code is not performance or space critical. Reviewed-by: Andres Freund Discussion: https://www.postgresql.org/message-id/54c29fb0-edf2-48ea-9814-44e918bbd6e8@iki.fi
Diffstat (limited to 'src/backend/utils/adt/tsgistidx.c')
-rw-r--r--src/backend/utils/adt/tsgistidx.c17
1 files changed, 4 insertions, 13 deletions
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index 5698ee5502b..1438ff35209 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -96,34 +96,25 @@ gtsvectorin(PG_FUNCTION_ARGS)
PG_RETURN_VOID(); /* keep compiler quiet */
}
-#define SINGOUTSTR "%d true bits, %d false bits"
-#define ARROUTSTR "%d unique words"
-#define EXTRALEN ( 2*13 )
-
-static int outbuf_maxlen = 0;
-
Datum
gtsvectorout(PG_FUNCTION_ARGS)
{
SignTSVector *key = (SignTSVector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
char *outbuf;
- if (outbuf_maxlen == 0)
- outbuf_maxlen = 2 * EXTRALEN + Max(strlen(SINGOUTSTR), strlen(ARROUTSTR)) + 1;
- outbuf = palloc(outbuf_maxlen);
-
if (ISARRKEY(key))
- sprintf(outbuf, ARROUTSTR, (int) ARRNELEM(key));
+ outbuf = psprintf("%d unique words", (int) ARRNELEM(key));
else
{
if (ISALLTRUE(key))
- sprintf(outbuf, "all true bits");
+ outbuf = pstrdup("all true bits");
else
{
int siglen = GETSIGLEN(key);
int cnttrue = sizebitvec(GETSIGN(key), siglen);
- sprintf(outbuf, SINGOUTSTR, cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
+ outbuf = psprintf("%d true bits, %d false bits",
+ cnttrue, (int) SIGLENBIT(siglen) - cnttrue);
}
}