From: Tom Lane Date: Sun, 20 Sep 2015 20:48:44 +0000 (-0400) Subject: Be more wary about partially-valid LOCALLOCK data in RemoveLocalLock(). X-Git-Tag: REL9_2_14~32 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ac0c71228fe7e44d9eba130df9bbbb5c2bfa9faa;p=postgresql Be more wary about partially-valid LOCALLOCK data in RemoveLocalLock(). RemoveLocalLock() must consider the possibility that LockAcquireExtended() failed to palloc the initial space for a locallock's lockOwners array. I had evidently meant to cope with this hazard when the code was originally written (commit 1785acebf2ed14fd66955e2d9a55d77a025f418d), but missed that the pfree needed to be protected with an if-test. Just to make sure things are left in a clean state, reset numLockOwners as well. Per low-memory testing by Andreas Seltenreich. Back-patch to all supported branches. --- diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c index 65726196d0..4dd78f9113 100644 --- a/src/backend/storage/lmgr/lock.c +++ b/src/backend/storage/lmgr/lock.c @@ -744,7 +744,7 @@ LockAcquireExtended(const LOCKTAG *locktag, locallock->numLockOwners = 0; locallock->maxLockOwners = 8; locallock->holdsStrongLockCount = FALSE; - locallock->lockOwners = NULL; + locallock->lockOwners = NULL; /* in case next line fails */ locallock->lockOwners = (LOCALLOCKOWNER *) MemoryContextAlloc(TopMemoryContext, locallock->maxLockOwners * sizeof(LOCALLOCKOWNER)); @@ -1213,7 +1213,9 @@ RemoveLocalLock(LOCALLOCK *locallock) if (locallock->lockOwners[i].owner != NULL) ResourceOwnerForgetLock(locallock->lockOwners[i].owner, locallock); } - pfree(locallock->lockOwners); + locallock->numLockOwners = 0; + if (locallock->lockOwners != NULL) + pfree(locallock->lockOwners); locallock->lockOwners = NULL; if (locallock->holdsStrongLockCount)