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 1 commit
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
29 changes: 13 additions & 16 deletions src/dwi/bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class Bootstrap : public Adapter::Base<Bootstrap<ImageType, Functor, NUM_VOX_PER
voxel_buffer.push_back(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;
}

protected:
Expand All @@ -78,14 +77,10 @@ class Bootstrap : public Adapter::Base<Bootstrap<ImageType, Functor, NUM_VOX_PER
vector<vector<value_type>> voxel_buffer;
value_type *next_voxel;
value_type *last_voxel;
size_t current_chunk;

value_type *allocate_voxel() {
if (next_voxel == last_voxel) {
++current_chunk;
if (current_chunk >= voxel_buffer.size())
voxel_buffer.push_back(vector<value_type>(NUM_VOX_PER_CHUNK * size(3)));
assert(current_chunk < voxel_buffer.size());
voxel_buffer.push_back(vector<value_type>(NUM_VOX_PER_CHUNK * size(3)));
next_voxel = &voxel_buffer.back()[0];
last_voxel = next_voxel + NUM_VOX_PER_CHUNK * size(3);
}
Expand All @@ -95,20 +90,22 @@ 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 (std::make_pair (voxel, data));
return data;
}
};


} // namespace DWI
} // namespace MR

Expand Down