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 buffer overflow in DWI bootstrapping #2779

Merged
merged 3 commits into from
Apr 18, 2024
Merged
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
32 changes: 16 additions & 16 deletions src/dwi/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ class Bootstrap : public Adapter::Base<Bootstrap<ImageType, Functor, NUM_VOX_PER
};

Bootstrap(const ImageType &Image, const Functor &functor)
: base_type(Image), func(functor), next_voxel(nullptr), last_voxel(nullptr) {
: base_type(Image), func(functor), next_voxel(nullptr), last_voxel(nullptr), current_chunk(0) {
assert(ndim() == 4);
voxel_buffer.push_back(std::vector<value_type>(NUM_VOX_PER_CHUNK * size(3)));
clear();
}

value_type value() { return get_voxel()[index(3)]; }
Expand All @@ -65,8 +67,6 @@ class Bootstrap : public Adapter::Base<Bootstrap<ImageType, Functor, NUM_VOX_PER

void clear() {
voxels.clear();
if (voxel_buffer.empty())
voxel_buffer.push_back(std::vector<value_type>(NUM_VOX_PER_CHUNK * size(3)));
next_voxel = &voxel_buffer[0][0];
last_voxel = next_voxel + NUM_VOX_PER_CHUNK * size(3);
current_chunk = 0;
Expand All @@ -82,11 +82,10 @@ class Bootstrap : public Adapter::Base<Bootstrap<ImageType, Functor, NUM_VOX_PER

value_type *allocate_voxel() {
if (next_voxel == last_voxel) {
++current_chunk;
if (current_chunk >= voxel_buffer.size())
if (++current_chunk >= voxel_buffer.size())
voxel_buffer.push_back(std::vector<value_type>(NUM_VOX_PER_CHUNK * size(3)));
assert(current_chunk < voxel_buffer.size());
next_voxel = &voxel_buffer.back()[0];
next_voxel = &voxel_buffer[current_chunk][0];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdtournier Note bug here resolved in b2fa527.

last_voxel = next_voxel + NUM_VOX_PER_CHUNK * size(3);
}
value_type *retval = next_voxel;
Expand All @@ -95,16 +94,17 @@ class Bootstrap : public Adapter::Base<Bootstrap<ImageType, Functor, NUM_VOX_PER
}

value_type *get_voxel() {
value_type *&data(
voxels.insert(std::make_pair(Eigen::Vector3i(index(0), index(1), index(2)), nullptr)).first->second);
if (!data) {
data = allocate_voxel();
ssize_t pos = index(3);
for (auto l = Loop(3)(*this); l; ++l)
data[index(3)] = base_type::value();
index(3) = pos;
func(data);
}
const Eigen::Vector3i voxel(index(0), index(1), index(2));
const typename std::map<Eigen::Vector3i, value_type *, IndexCompare>::const_iterator existing = voxels.find(voxel);
if (existing != voxels.end())
return existing->second;
Comment on lines +99 to +100

This comment was marked as outdated.

value_type *const data = allocate_voxel();
ssize_t pos = index(3);
for (auto l = Loop(3)(*this); l; ++l)

This comment was marked as outdated.

This comment was marked as outdated.

data[index(3)] = base_type::value();
Comment on lines +103 to +104

This comment was marked as outdated.

index(3) = pos;
func(data);
voxels.insert({voxel, data});
return data;
}
};
Expand Down