Hello hackers,

I found the numbers in `quote_literal_cstr` palloc quite magical. So I've
added a comment clarifying what they mean. The change is small:

        /* We make a worst-case result area; wasting a little space is OK */
-       result = palloc(len * 2 + 3 + 1);
+       result = palloc(
+               (len * 2)    /* worst-case doubling for every character if
each one is a quote */
+               + 3          /* two outer quotes + possibly 'E' if needed */
+               + 1          /* null terminator */
+       );

Best regards,
Steve
From 1eb0a72ea819ee9217d84abb8112e72bf0df61d8 Mon Sep 17 00:00:00 2001
From: steve-chavez <stevechavez...@gmail.com>
Date: Sun, 6 Apr 2025 12:33:55 -0500
Subject: [PATCH] clarify palloc comment on quote_literal_cstr

---
 src/backend/utils/adt/quote.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/quote.c b/src/backend/utils/adt/quote.c
index 677efb93e8d..4b9c52e6d9e 100644
--- a/src/backend/utils/adt/quote.c
+++ b/src/backend/utils/adt/quote.c
@@ -108,7 +108,11 @@ 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)    /* worst-case 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';
-- 
2.40.1

Reply via email to