Commit 89e434b5 authored by Alvaro Herrera's avatar Alvaro Herrera

Fix coding rules violations in walreceiver.c

1. Since commit b1a9bad9 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 9ed551e0 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 64233902 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 1bb25580 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
parent f41bd4cb
...@@ -196,6 +196,7 @@ WalReceiverMain(void) ...@@ -196,6 +196,7 @@ WalReceiverMain(void)
bool first_stream; bool first_stream;
WalRcvData *walrcv = WalRcv; WalRcvData *walrcv = WalRcv;
TimestampTz last_recv_timestamp; TimestampTz last_recv_timestamp;
TimestampTz now;
bool ping_sent; bool ping_sent;
char *err; char *err;
...@@ -205,6 +206,8 @@ WalReceiverMain(void) ...@@ -205,6 +206,8 @@ WalReceiverMain(void)
*/ */
Assert(walrcv != NULL); Assert(walrcv != NULL);
now = GetCurrentTimestamp();
/* /*
* Mark walreceiver as running in shared memory. * Mark walreceiver as running in shared memory.
* *
...@@ -235,6 +238,7 @@ WalReceiverMain(void) ...@@ -235,6 +238,7 @@ WalReceiverMain(void)
case WALRCV_RESTARTING: case WALRCV_RESTARTING:
default: default:
/* Shouldn't happen */ /* Shouldn't happen */
SpinLockRelease(&walrcv->mutex);
elog(PANIC, "walreceiver still running according to shared memory state"); elog(PANIC, "walreceiver still running according to shared memory state");
} }
/* Advertise our PID so that the startup process can kill us */ /* Advertise our PID so that the startup process can kill us */
...@@ -249,7 +253,8 @@ WalReceiverMain(void) ...@@ -249,7 +253,8 @@ WalReceiverMain(void)
startpointTLI = walrcv->receiveStartTLI; startpointTLI = walrcv->receiveStartTLI;
/* Initialise to a sanish value */ /* Initialise to a sanish value */
walrcv->lastMsgSendTime = walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = GetCurrentTimestamp(); walrcv->lastMsgSendTime =
walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = now;
SpinLockRelease(&walrcv->mutex); SpinLockRelease(&walrcv->mutex);
...@@ -308,13 +313,13 @@ WalReceiverMain(void) ...@@ -308,13 +313,13 @@ WalReceiverMain(void)
SpinLockAcquire(&walrcv->mutex); SpinLockAcquire(&walrcv->mutex);
memset(walrcv->conninfo, 0, MAXCONNINFO); memset(walrcv->conninfo, 0, MAXCONNINFO);
if (tmp_conninfo) if (tmp_conninfo)
{
strlcpy((char *) walrcv->conninfo, tmp_conninfo, MAXCONNINFO); strlcpy((char *) walrcv->conninfo, tmp_conninfo, MAXCONNINFO);
pfree(tmp_conninfo);
}
walrcv->ready_to_display = true; walrcv->ready_to_display = true;
SpinLockRelease(&walrcv->mutex); SpinLockRelease(&walrcv->mutex);
if (tmp_conninfo)
pfree(tmp_conninfo);
first_stream = true; first_stream = true;
for (;;) for (;;)
{ {
...@@ -1390,8 +1395,8 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS) ...@@ -1390,8 +1395,8 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
TimestampTz last_receipt_time; TimestampTz last_receipt_time;
XLogRecPtr latest_end_lsn; XLogRecPtr latest_end_lsn;
TimestampTz latest_end_time; TimestampTz latest_end_time;
char *slotname; char slotname[NAMEDATALEN];
char *conninfo; char conninfo[MAXCONNINFO];
/* Take a lock to ensure value consistency */ /* Take a lock to ensure value consistency */
SpinLockAcquire(&WalRcv->mutex); SpinLockAcquire(&WalRcv->mutex);
...@@ -1406,8 +1411,8 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS) ...@@ -1406,8 +1411,8 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
last_receipt_time = WalRcv->lastMsgReceiptTime; last_receipt_time = WalRcv->lastMsgReceiptTime;
latest_end_lsn = WalRcv->latestWalEnd; latest_end_lsn = WalRcv->latestWalEnd;
latest_end_time = WalRcv->latestWalEndTime; latest_end_time = WalRcv->latestWalEndTime;
slotname = pstrdup(WalRcv->slotname); strlcpy(slotname, (char *) WalRcv->slotname, sizeof(slotname));
conninfo = pstrdup(WalRcv->conninfo); strlcpy(conninfo, (char *) WalRcv->conninfo, sizeof(conninfo));
SpinLockRelease(&WalRcv->mutex); SpinLockRelease(&WalRcv->mutex);
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment