Re: Remove unnecessary static type qualifiers

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>
Cc: Junwang Zhao <zhjwpku(at)gmail(dot)com>, Japin Li <japinli(at)hotmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Remove unnecessary static type qualifiers
Date: 2025-04-08 21:22:03
Message-ID: CAApHDvq2bO5+i4Y4N5Xd1kk6wJ_mRanRKSEiXb1tnLug3_9E1A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 9 Apr 2025 at 03:46, Peter Eisentraut <peter(at)eisentraut(dot)org> wrote:
> To avoid creating an array on the stack, you could maybe write it with a
> pointer instead, like:
>
> const char * const query = "...";
>
> I haven't tested whether that results in different or better compiled
> code. The original code is probably good enough.

I expect it's been done the way it has to make the overflow detection
code work. The problem with having a pointer to a string constant is
that sizeof() will return the size of the pointer and not the space
needed to store the string.

We can get rid of the variable and make the overflow work by checking
the return length of snprintf. I think that's all C99 spec now...

len = snprintf(qbuf, "set client_encoding to '%s'", encoding);
/* check query buffer overflow */
if (len >= sizeof(qbuf))
return -1;

David

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2025-04-08 21:27:41 Re: Enhancing Memory Context Statistics Reporting
Previous Message Tom Lane 2025-04-08 21:13:58 Re: Move tests of contrib/spi/ out of the core regression tests