Re: [PATCH] clarify palloc comment on quote_literal_cstr

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Steve Chavez <steve(at)supabase(dot)io>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [PATCH] clarify palloc comment on quote_literal_cstr
Date: 2025-04-06 21:49:02
Message-ID: Z_L2zipq-qAYClxk@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, Apr 06, 2025 at 12:37:24PM -0500, Steve Chavez wrote:
> 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 */
> + );

Agreed that this is a good idea to have a better documentation here if
someone decides to tweak this area in the future, rather than have
them guess the numbers.

Looking at what quote_literal_internal() does, it seems to me that you
are right based on ESCAPE_STRING_SYNTAX, SQL_STR_DOUBLE(), and the
extra backslashes added to the destination buffer.
--
Michael

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-04-06 21:52:34 Re: Possibly hard-to-read message
Previous Message Michael Paquier 2025-04-06 21:44:31 Re: Typo in comment for pgstat_database_flush_cb()