Skip to content

Commit eed5bbc

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 44297a0 commit eed5bbc

File tree

1 file changed

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

1 file changed

+5
-2
lines changed

src/backend/storage/lmgr/lock.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
644644
locallock->nLocks = 0;
645645
locallock->numLockOwners = 0;
646646
locallock->maxLockOwners = 8;
647-
locallock->lockOwners = NULL;
647+
locallock->lockOwners = NULL; /* in case next line fails */
648648
locallock->lockOwners = (LOCALLOCKOWNER *)
649649
MemoryContextAlloc(TopMemoryContext,
650650
locallock->maxLockOwners * sizeof(LOCALLOCKOWNER));
@@ -994,8 +994,11 @@ LockAcquireExtended(const LOCKTAG *locktag,
994994
static void
995995
RemoveLocalLock(LOCALLOCK *locallock)
996996
{
997-
pfree(locallock->lockOwners);
997+
locallock->numLockOwners = 0;
998+
if (locallock->lockOwners != NULL)
999+
pfree(locallock->lockOwners);
9981000
locallock->lockOwners = NULL;
1001+
9991002
if (!hash_search(LockMethodLocalHash,
10001003
(void *) &(locallock->tag),
10011004
HASH_REMOVE, NULL))

0 commit comments

Comments
 (0)