Fix off-by-one bug in LWLockRegisterTranche().
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 Apr 2014 19:59:57 +0000 (15:59 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 Apr 2014 19:59:57 +0000 (15:59 -0400)
Original coding failed to enlarge the array as required if
the requested tranche_id was equal to LWLockTranchesAllocated.

In passing, fix poor style of not casting the result of (re)palloc.

src/backend/storage/lmgr/lwlock.c

index 36b4b8bbeaab5ad0501167a6ee4b8939e5e0f121..df8f9bfd893f34fa3a0a1dd8d90521db339c24cd 100644 (file)
@@ -350,8 +350,9 @@ CreateLWLocks(void)
    if (LWLockTrancheArray == NULL)
    {
        LWLockTranchesAllocated = 16;
-       LWLockTrancheArray = MemoryContextAlloc(TopMemoryContext,
-           LWLockTranchesAllocated * sizeof(LWLockTranche *));
+       LWLockTrancheArray = (LWLockTranche **)
+           MemoryContextAlloc(TopMemoryContext,
+                              LWLockTranchesAllocated * sizeof(LWLockTranche *));
    }
 
    MainLWLockTranche.name = "main";
@@ -423,11 +424,12 @@ LWLockRegisterTranche(int tranche_id, LWLockTranche *tranche)
    {
        int     i = LWLockTranchesAllocated;
 
-       while (i < tranche_id)
+       while (i <= tranche_id)
            i *= 2;
 
-       LWLockTrancheArray = repalloc(LWLockTrancheArray,
-                                     i * sizeof(LWLockTranche *));
+       LWLockTrancheArray = (LWLockTranche **)
+           repalloc(LWLockTrancheArray,
+                    i * sizeof(LWLockTranche *));
        LWLockTranchesAllocated = i;
    }