Commit ba51774d authored by Tom Lane's avatar Tom Lane

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 1785aceb), 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.
parent 85eda7e9
...@@ -753,7 +753,7 @@ LockAcquireExtended(const LOCKTAG *locktag, ...@@ -753,7 +753,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
locallock->numLockOwners = 0; locallock->numLockOwners = 0;
locallock->maxLockOwners = 8; locallock->maxLockOwners = 8;
locallock->holdsStrongLockCount = FALSE; locallock->holdsStrongLockCount = FALSE;
locallock->lockOwners = NULL; locallock->lockOwners = NULL; /* in case next line fails */
locallock->lockOwners = (LOCALLOCKOWNER *) locallock->lockOwners = (LOCALLOCKOWNER *)
MemoryContextAlloc(TopMemoryContext, MemoryContextAlloc(TopMemoryContext,
locallock->maxLockOwners * sizeof(LOCALLOCKOWNER)); locallock->maxLockOwners * sizeof(LOCALLOCKOWNER));
...@@ -1222,7 +1222,9 @@ RemoveLocalLock(LOCALLOCK *locallock) ...@@ -1222,7 +1222,9 @@ RemoveLocalLock(LOCALLOCK *locallock)
if (locallock->lockOwners[i].owner != NULL) if (locallock->lockOwners[i].owner != NULL)
ResourceOwnerForgetLock(locallock->lockOwners[i].owner, locallock); ResourceOwnerForgetLock(locallock->lockOwners[i].owner, locallock);
} }
pfree(locallock->lockOwners); locallock->numLockOwners = 0;
if (locallock->lockOwners != NULL)
pfree(locallock->lockOwners);
locallock->lockOwners = NULL; locallock->lockOwners = NULL;
if (locallock->holdsStrongLockCount) if (locallock->holdsStrongLockCount)
......
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