e630f719d5af868110150d1580807b685bf5c27b
chmalee
  Fri Jan 24 16:32:03 2025 -0800
Prevent off-by-one in loop which prevents writing off the end of an array, refs #35138

diff --git src/hg/hgTracks/vcfTrack.c src/hg/hgTracks/vcfTrack.c
index aedffc8aace..abd045d9ba7 100644
--- src/hg/hgTracks/vcfTrack.c
+++ src/hg/hgTracks/vcfTrack.c
@@ -2518,58 +2518,58 @@
                     c2 = c2->next;
                 // similar to above case but swap c1 and c2
                 maybeRollBackCell(c2, c1, c2Copy, c1Copy);
                 }
             }
         }
     fillOutHapOrder(hapOrder, hapCount, c1, c2, sampleDrawOrder);
     }
 else
     {
     hapOrder[0] = matrix->alleleIx;
     hapOrder[1] = matrix->next->alleleIx;
     }
 }
 
-static void assignHapArrayIx(int *ret, struct hapCluster **hapArray, struct vcfFile *vcff, char *sample, boolean doChild)
+static void assignHapArrayIx(int *ret, struct hapCluster **hapArray, struct vcfFile *vcff, char *sample, boolean doChild, int hapArrayLen)
 {
 int i;
 struct vcfRecord *rec = vcff->records;
 int ix = 0; // index into ret
-for (i = 0; i < slCount(hapArray); i++)
+for (i = 0; i < hapArrayLen; i++)
     {
     struct hapCluster *hc = hapArray[i];
     struct vcfGenotype *gt = &(rec->genotypes[hc->gtHapIx >> 1]);
     if (!doChild && !sameString(gt->id, sample))
         ret[ix++] = i;
     else if (doChild && sameString(gt->id, sample))
         ret[ix++] = i;
     }
 }
 
 static struct hapDistanceMatrix *fillOutDistanceMatrix(struct hapCluster **hapArray, struct vcfFile *vcff, char *sample, struct cwaExtraData *helper, int gtCount)
 /* Allocates and fill out a struct hapDistanceMatrix, one row per child allele, and a
  * hapDistanceMatrixCell per parent allele */
 {
 int parGtCount = (gtCount - 1) * 2;
 int i,j;
 struct vcfRecord *rec = vcff->records;
 struct hapDistanceMatrix *matrix = NULL;
 int childHapArrayIndices[2];
 int parentHapArrayIndices[parGtCount];
-assignHapArrayIx(childHapArrayIndices, hapArray, vcff, sample, TRUE);
-assignHapArrayIx(parentHapArrayIndices, hapArray, vcff, sample, FALSE);
+assignHapArrayIx(childHapArrayIndices, hapArray, vcff, sample, TRUE, gtCount * 2);
+assignHapArrayIx(parentHapArrayIndices, hapArray, vcff, sample, FALSE, gtCount * 2);
 for (i = 0; i < 2; i++)
     {
     struct hapDistanceMatrix *row = needMem(sizeof(struct hapDistanceMatrix));
     struct hapCluster *hcChild = hapArray[childHapArrayIndices[i]];
     struct vcfGenotype *gt = &(rec->genotypes[hcChild->gtHapIx >> 1]);
     row->row = NULL;
     row->sampleId = cloneString(gt->id);
     row->alleleIx = hcChild->gtHapIx;
     for (j = 0; j < parGtCount; j++)
         {
         struct hapDistanceMatrixCell *cell = needMem(sizeof(struct hapDistanceMatrixCell));
         struct hapCluster *hcParent = hapArray[parentHapArrayIndices[j]];
         struct vcfGenotype *parGt = &(rec->genotypes[hcParent->gtHapIx >> 1]);
         cell->sampleId = cloneString(parGt->id);
         cell->alleleIx = hcParent->gtHapIx;