Skip to content

Commit 9502237

Browse files
authored
Merge pull request #5502 from simonlynen/patch-1
Fix out of bounds for heap-array in statistical outlier filter
2 parents 90910f1 + c83bd11 commit 9502237

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

filters/include/pcl/filters/impl/statistical_outlier_removal.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ pcl::StatisticalOutlierRemoval<PointT>::applyFilterIndices (Indices &indices)
5959
searcher_->setInputCloud (input_);
6060

6161
// The arrays to be used
62-
Indices nn_indices (mean_k_);
63-
std::vector<float> nn_dists (mean_k_);
62+
const int searcher_k = mean_k_ + 1; // Find one more, since results include the query point.
63+
Indices nn_indices (searcher_k);
64+
std::vector<float> nn_dists (searcher_k);
6465
std::vector<float> distances (indices_->size ());
6566
indices.resize (indices_->size ());
6667
removed_indices_->resize (indices_->size ());
@@ -79,7 +80,7 @@ pcl::StatisticalOutlierRemoval<PointT>::applyFilterIndices (Indices &indices)
7980
}
8081

8182
// Perform the nearest k search
82-
if (searcher_->nearestKSearch ((*indices_)[iii], mean_k_ + 1, nn_indices, nn_dists) == 0)
83+
if (searcher_->nearestKSearch ((*indices_)[iii], searcher_k, nn_indices, nn_dists) == 0)
8384
{
8485
distances[iii] = 0.0;
8586
PCL_WARN ("[pcl::%s::applyFilter] Searching for the closest %d neighbors failed.\n", getClassName ().c_str (), mean_k_);
@@ -88,7 +89,7 @@ pcl::StatisticalOutlierRemoval<PointT>::applyFilterIndices (Indices &indices)
8889

8990
// Calculate the mean distance to its neighbors
9091
double dist_sum = 0.0;
91-
for (int k = 1; k < mean_k_ + 1; ++k) // k = 0 is the query point
92+
for (int k = 1; k < searcher_k; ++k) // k = 0 is the query point
9293
dist_sum += sqrt (nn_dists[k]);
9394
distances[iii] = static_cast<float> (dist_sum / mean_k_);
9495
valid_distances++;

0 commit comments

Comments
 (0)