Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid potential index out of bounds #173

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nanoflann.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@ class KDTreeSingleIndexDynamicAdaptor
const size_t maximumPointCount = 1000000000U)
: dataset(inputData), index_params(params), distance(inputData)
{
treeCount = static_cast<size_t>(std::log2(maximumPointCount));
treeCount = static_cast<size_t>(std::log2(maximumPointCount)) + 1;
pointCount = 0U;
dim = dimensionality;
treeIndex.clear();
Expand Down
8 changes: 5 additions & 3 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,10 @@ TEST(kdtree,robust_nonempty_tree)
{
// Try to build a dynamic tree with some initial points
PointCloud<double> cloud;
generateRandomPointCloud(cloud, 1000);
const size_t max_point_count = 1000;
generateRandomPointCloud(cloud, max_point_count);

double query_pt[3] = { 0.5, 0.5, 0.5};
const double query_pt[3] = {0.5, 0.5, 0.5};

// construct a kd-tree index:
typedef KDTreeSingleIndexDynamicAdaptor<
Expand All @@ -490,7 +491,8 @@ TEST(kdtree,robust_nonempty_tree)
3
> my_kd_tree_simple_t;

my_kd_tree_simple_t index1(3 /*dim*/, cloud, KDTreeSingleIndexAdaptorParams(10 /* max leaf */) );
my_kd_tree_simple_t index1(3 /*dim*/, cloud, KDTreeSingleIndexAdaptorParams(10 /* max leaf */),
max_point_count);

// Try a search and expect a neighbor to exist because the dynamic tree was passed a non-empty cloud
const size_t num_results = 1;
Expand Down