Fix rare core dump in BackendIdGetTransactionIds().

BackendIdGetTransactionIds() neglected the possibility that the PROC
pointer in a ProcState array entry is null.  In current usage, this could
only crash if the other backend had exited since pgstat_read_current_status
saw it as active, which is a pretty narrow window.  But it's reachable in
the field, per bug #12918 from Vladimir Borodin.

Back-patch to 9.4 where the faulty code was introduced.
This commit is contained in:
Tom Lane 2015-03-30 13:05:27 -04:00
parent f444de5e34
commit 2897e069c1

View File

@ -410,9 +410,7 @@ BackendIdGetProc(int backendID)
void void
BackendIdGetTransactionIds(int backendID, TransactionId *xid, TransactionId *xmin) BackendIdGetTransactionIds(int backendID, TransactionId *xid, TransactionId *xmin)
{ {
ProcState *stateP;
SISeg *segP = shmInvalBuffer; SISeg *segP = shmInvalBuffer;
PGXACT *xact;
*xid = InvalidTransactionId; *xid = InvalidTransactionId;
*xmin = InvalidTransactionId; *xmin = InvalidTransactionId;
@ -422,11 +420,16 @@ BackendIdGetTransactionIds(int backendID, TransactionId *xid, TransactionId *xmi
if (backendID > 0 && backendID <= segP->lastBackend) if (backendID > 0 && backendID <= segP->lastBackend)
{ {
stateP = &segP->procState[backendID - 1]; ProcState *stateP = &segP->procState[backendID - 1];
xact = &ProcGlobal->allPgXact[stateP->proc->pgprocno]; PGPROC *proc = stateP->proc;
*xid = xact->xid; if (proc != NULL)
*xmin = xact->xmin; {
PGXACT *xact = &ProcGlobal->allPgXact[proc->pgprocno];
*xid = xact->xid;
*xmin = xact->xmin;
}
} }
LWLockRelease(SInvalWriteLock); LWLockRelease(SInvalWriteLock);