Skip to content

Commit e32c5f1

Browse files
committed
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 1785ace), 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.
1 parent f38070d commit e32c5f1

File tree

1 file changed

+4
-2
lines changed
  • src/backend/storage/lmgr

1 file changed

+4
-2
lines changed

src/backend/storage/lmgr/lock.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
758758
locallock->numLockOwners = 0;
759759
locallock->maxLockOwners = 8;
760760
locallock->holdsStrongLockCount = FALSE;
761-
locallock->lockOwners = NULL;
761+
locallock->lockOwners = NULL; /* in case next line fails */
762762
locallock->lockOwners = (LOCALLOCKOWNER *)
763763
MemoryContextAlloc(TopMemoryContext,
764764
locallock->maxLockOwners * sizeof(LOCALLOCKOWNER));
@@ -1227,7 +1227,9 @@ RemoveLocalLock(LOCALLOCK *locallock)
12271227
if (locallock->lockOwners[i].owner != NULL)
12281228
ResourceOwnerForgetLock(locallock->lockOwners[i].owner, locallock);
12291229
}
1230-
pfree(locallock->lockOwners);
1230+
locallock->numLockOwners = 0;
1231+
if (locallock->lockOwners != NULL)
1232+
pfree(locallock->lockOwners);
12311233
locallock->lockOwners = NULL;
12321234

12331235
if (locallock->holdsStrongLockCount)

0 commit comments

Comments
 (0)