10BC0 Be more rigorous about local variables in PostgresMain(). · postgres/postgres@671bf1c · GitHub
[go: up one dir, main page]

Skip to content

Commit 671bf1c

Browse files
committed
Be more rigorous about local variables in PostgresMain().
Since PostgresMain calls sigsetjmp, any local variables that are not marked "volatile" have a risk of unspecified behavior. In practice this means that when control return 10000 s via longjmp, such variables might get reset to their values as of the time of sigsetjmp, depending on whether the compiler chose to put them in registers or on the stack. We were careful about this for "send_ready_for_query", but not the other local variables. In the case of the timeout_enabled flags, resetting them to their initial "false" states is actually good, since we do "disable_all_timeouts()" in the longjmp cleanup code path. If that does not happen, we risk uselessly calling "disable_timeout()" later, which is harmless but a little bit expensive. Let's explicitly reset these flags so that the behavior is correct and platform-independent. (This change means that we really don't need the new "volatile" markings after all, but let's install them anyway since any change in this logic could re-introduce a problem.) There is no issue for "firstchar" and "input_message" because those are explicitly reinitialized each time through the query processing loop. To make that clearer, move them to be declared inside the loop. That leaves us with all the function-lifespan locals except the sigjmp_buf itself marked as volatile, which seems like a good policy to have going forward. Because of the possibility of extra disable_timeout() calls, this seems worth back-patching. Sergey Shinderuk and Tom Lane Discussion: https://postgr.es/m/2eda015b-7dff-47fd-d5e2-f1a9899b90a6@postgrespro.ru
1 parent 914e72e commit 671bf1c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/backend/tcop/postgres.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3689,11 +3689,11 @@ PostgresMain(int argc, char *argv[],
36893689
const char *dbname,
36903690
const char *username)
36913691
{
3692-
int firstchar;
3693-
StringInfoData input_message;
36943692
sigjmp_buf local_sigjmp_buf;
3693+
3694+
/* these must be volatile to ensure state is preserved across longjmp: */
36953695
volatile bool send_ready_for_query = true;
3696-
bool disable_idle_in_transaction_timeout = false;
3696+
volatile bool disable_idle_in_transaction_timeout = false;
36973697

36983698
/* Initialize startup process environment if necessary. */
36993699
if (!IsUnderPostmaster)
@@ -3978,9 +3978,10 @@ PostgresMain(int argc, char *argv[],
39783978
* query cancels from being misreported as timeouts in case we're
39793979
* forgetting a timeout cancel.
39803980
*/
3981-
disable_all_timeouts(false);
3982-
QueryCancelPending = false; /* second to avoid race condition */
3981+
disable_all_timeouts(false); /* do first to avoid race condition */
3982+
QueryCancelPending = false;
39833983
stmt_timeout_active = false;
3984+
disable_idle_in_transaction_timeout = false;
39843985

39853986
/* Not reading from the client anymore. */
39863987
DoingCommandRead = false;
@@ -4069,6 +4070,9 @@ PostgresMain(int argc, char *argv[],
40694070

40704071
for (;;)
40714072
{
4073+
int firstchar;
4074+
StringInfoData input_message;
4075+
40724076
/*
40734077
* At top of loop, reset extended-query-message flag, so that any
40744078
* errors encountered in "idle" state don't provoke skip.

0 commit comments

Comments
 (0)
0