From 86076395ef81c7482a5f5a62dc05a8d8075470bd Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Tue, 3 Oct 2017 14:58:25 +0200 Subject: [PATCH] Fix coding rules violations in walreceiver.c 1. Since commit b1a9bad9e744 we had pstrdup() inside a spinlock-protected critical section; reported by Andreas Seltenreich. Turn those into strlcpy() to stack-allocated variables instead. Backpatch to 9.6. 2. Since commit 9ed551e0a4fd we had a pfree() uselessly inside a spinlock-protected critical section. Tom Lane noticed in code review. Move down. Backpatch to 9.6. 3. Since commit 64233902d22b we had GetCurrentTimestamp() (a kernel call) inside a spinlock-protected critical section. Tom Lane noticed in code review. Move it up. Backpatch to 9.2. 4. Since commit 1bb2558046cc we did elog(PANIC) while holding spinlock. Tom Lane noticed in code review. Release spinlock before dying. Backpatch to 9.2. Discussion: https://postgr.es/m/87h8vhtgj2.fsf@ansel.ydns.eu --- src/backend/replication/walreceiver.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index e0ab112d31..7213b98bf9 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -201,6 +201,7 @@ WalReceiverMain(void) bool first_stream; WalRcvData *walrcv = WalRcv; TimestampTz last_recv_timestamp; + TimestampTz now; bool ping_sent; /* @@ -209,6 +210,8 @@ WalReceiverMain(void) */ Assert(walrcv != NULL); + now = GetCurrentTimestamp(); + /* * Mark walreceiver as running in shared memory. * @@ -239,6 +242,7 @@ WalReceiverMain(void) case WALRCV_RESTARTING: default: /* Shouldn't happen */ + SpinLockRelease(&walrcv->mutex); elog(PANIC, "walreceiver still running according to shared memory state"); } /* Advertise our PID so that the startup process can kill us */ @@ -253,7 +257,8 @@ WalReceiverMain(void) startpointTLI = walrcv->receiveStartTLI; /* Initialise to a sanish value */ - walrcv->lastMsgSendTime = walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = GetCurrentTimestamp(); + walrcv->lastMsgSendTime = + walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = now; SpinLockRelease(&walrcv->mutex); @@ -317,13 +322,13 @@ WalReceiverMain(void) SpinLockAcquire(&walrcv->mutex); memset(walrcv->conninfo, 0, MAXCONNINFO); if (tmp_conninfo) - { strlcpy((char *) walrcv->conninfo, tmp_conninfo, MAXCONNINFO); - pfree(tmp_conninfo); - } walrcv->ready_to_display = true; SpinLockRelease(&walrcv->mutex); + if (tmp_conninfo) + pfree(tmp_conninfo); + first_stream = true; for (;;) { @@ -1349,8 +1354,8 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS) TimestampTz last_receipt_time; XLogRecPtr latest_end_lsn; TimestampTz latest_end_time; - char *slotname; - char *conninfo; + char slotname[NAMEDATALEN]; + char conninfo[MAXCONNINFO]; /* * No WAL receiver (or not ready yet), just return a tuple with NULL @@ -1377,8 +1382,8 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS) last_receipt_time = walrcv->lastMsgReceiptTime; latest_end_lsn = walrcv->latestWalEnd; latest_end_time = walrcv->latestWalEndTime; - slotname = pstrdup(walrcv->slotname); - conninfo = pstrdup(walrcv->conninfo); + strlcpy(slotname, (char *) walrcv->slotname, sizeof(slotname)); + strlcpy(conninfo, (char *) walrcv->conninfo, sizeof(conninfo)); SpinLockRelease(&walrcv->mutex); /* Fetch values */