aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/quote.c
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2025-04-07 10:02:12 +0900
committerMichael Paquier <michael@paquier.xyz>2025-04-07 10:02:12 +0900
commitc36eda259130faf3559b751b9e81a5c7e2bee0d7 (patch)
treeebf8847b980eea1bd53cd4c58cb9f5a8a990d167 /src/backend/utils/adt/quote.c
parent3191a593d6dea56def460d06adc779f2aca44976 (diff)
downloadpostgresql-c36eda259130faf3559b751b9e81a5c7e2bee0d7.tar.gz
postgresql-c36eda259130faf3559b751b9e81a5c7e2bee0d7.zip
Clarify comment for worst-case allocation in quote_literal_cstr()
palloc() is invoked with a specific formula for its allocation size in quote_literal_cstr(). This wastes some memory, but the size is large enough to cover even the worst-case scenarios. No explanations were given about the reasons behind these numbers. This commit adds more documentation about all that. Author: Steve Chavez <steve@supabase.io> Discussion: https://postgr.es/m/CAGRrpzZ9bToRWS+fAnjxDJrxwZN1QcJ-y1Pn2yg=Hst6rydLtw@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt/quote.c')
-rw-r--r--src/backend/utils/adt/quote.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/backend/utils/adt/quote.c b/src/backend/utils/adt/quote.c
index 677efb93e8d..551de59a07f 100644
--- a/src/backend/utils/adt/quote.c
+++ b/src/backend/utils/adt/quote.c
@@ -108,7 +108,12 @@ quote_literal_cstr(const char *rawstr)
len = strlen(rawstr);
/* We make a worst-case result area; wasting a little space is OK */
- result = palloc(len * 2 + 3 + 1);
+ result = palloc(
+ (len * 2) /* doubling for every character if each one is
+ * a quote */
+ + 3 /* two outer quotes + possibly 'E' if needed */
+ + 1 /* null terminator */
+ );
newlen = quote_literal_internal(result, rawstr, len);
result[newlen] = '\0';