Skip to content

Commit

Permalink
clusterizer: Fix out of bounds errors in partition
Browse files Browse the repository at this point in the history
Change the code to keep r at one-past-the-end of the current range and
make the code robust wrt out of bounds access in edge cases.
  • Loading branch information
zeux committed Jan 13, 2021
1 parent 43d2e4f commit 689fe5a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/clusterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,28 @@ static size_t kdtreePartition(unsigned int* indices, size_t count, const float*
assert(count > 0);

size_t l = 0;
size_t r = count - 1;
size_t r = count;

while (l < r)
while (l != r)
{
while (points[indices[l] * stride + axis] < pivot)
{
l++;
if (l == r) return l;
}

while (points[indices[r] * stride + axis] > pivot)
do
{
r--;

if (l >= r)
break;
if (l == r) return l;
}
while (points[indices[r] * stride + axis] > pivot);

unsigned int t = indices[l];
indices[l] = indices[r];
indices[r] = t;

l++;
r--;
}

return l;
Expand Down

0 comments on commit 689fe5a

Please sign in to comment.