pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sergey Shinderuk <s.shinderuk@postgrespro.ru>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Subject: gcc -Wclobbered in PostgresMain
Date: Fri, 7 Jul 2023 15:13:14 +0300
Message-ID: <2eda015b-7dff-47fd-d5e2-f1a9899b90a6@postgrespro.ru> (raw)

Hi, hackers!

While analyzing -Wclobbered warnings from gcc we found a true one in 
PostgresMain():

postgres.c: In function ‘PostgresMain’:
postgres.c:4118:25: warning: variable 
‘idle_in_transaction_timeout_enabled’ might be clobbered by ‘longjmp’ or 
‘vfork’ [-Wclobbered]
  4118 |         bool            idle_in_transaction_timeout_enabled = 
false;
       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
postgres.c:4119:25: warning: variable ‘idle_session_timeout_enabled’ 
might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered]
  4119 |         bool            idle_session_timeout_enabled = false;
       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

These variables must be declared volatile, because they are read after 
longjmp(). send_ready_for_query declared there is volatile.

Without volatile, these variables are kept in registers and restored by 
longjump(). I think, this is harmless because the error handling code 
calls disable_all_timeouts() anyway. But strictly speaking the code is 
invoking undefined behavior by reading those variables after longjmp(), 
so it's worth fixing. And for consistency with send_ready_for_query too. 
I believe, making them volatile doesn't affect performance in any way.

I also moved firstchar's declaration inside the loop where it's used, to 
make it clear that this variable needn't be volatile and is not 
preserved after longjmp().

Best regards,

-- 
Sergey Shinderuk		https://postgrespro.com/

Attachments:

missing-volatile-longjmp.diff (text/x-patch, 962B) download diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 01b6cc1f7d3..56d8b0814b5 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -4111,12 +4111,11 @@ PostgresSingleUserMain(int argc, char *argv[], void PostgresMain(const char *dbname, const char *username) { - int firstchar; StringInfoData input_message; sigjmp_buf local_sigjmp_buf; volatile bool send_ready_for_query = true; - bool idle_in_transaction_timeout_enabled = false; - bool idle_session_timeout_enabled = false; + volatile bool idle_in_transaction_timeout_enabled = false; + volatile bool idle_session_timeout_enabled = false; Assert(dbname != NULL); Assert(username != NULL); @@ -4418,6 +4417,8 @@ PostgresMain(const char *dbname, const char *username) for (;;) { + int firstchar; + /* * At top of loop, reset extended-query-message flag, so that any * errors encountered in "idle" state don't provoke skip.

view thread (4+ messages)  latest in thread

Message-ID: <2eda015b-7dff-47fd-d5e2-f1a9899b90a6@postgrespro.ru>
Permalink:  ../2eda015b-7dff-47fd-d5e2-f1a9899b90a6@postgrespro.ru/
Also on:    postgresql.org/message-id/2eda015b-7dff-47fd-d5e2-f1a9899b90a6@postgrespro.ru

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: s.shinderuk@postgrespro.ru
  Subject: Re: gcc -Wclobbered in PostgresMain
  In-Reply-To: <2eda015b-7dff-47fd-d5e2-f1a9899b90a6@postgrespro.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox