Skip to content

Commit ff11902

Browse files
committed
dwi2mask: Fix segfault with -fslgrad
If dwi2mask is run with the -fslgrad option, this leads to a segmentation fault when constructing the output mask image. check_DW_scheme() is called within a try{} block to determine whether or not any present gradient table should be cleared upon image creation. However the fourth axis is stripped from the DWI header in order to construct this output image header. Within the check_DW_scheme() function, the load_bvecs_bvals() function is called. This function however fails to test whether or not the template header is a 4D image prior to obtaining the number of volumes and comparing this to the number of columns in bvecs / bvals, which leads to a buffer over-run.
1 parent 1b818fc commit ff11902

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/dwi/gradient.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ namespace MR
117117
if (bvals.cols() != bvecs.cols())
118118
throw Exception ("bvecs and bvals files must have same number of diffusion directions (file \"" + bvecs_path + "\" has " + str(bvecs.cols()) + ", file \"" + bvals_path + "\" has " + str(bvals.cols()) + ")");
119119

120-
if (bvals.cols() != header.size (3))
121-
throw Exception ("bvecs and bvals files must have same number of diffusion directions as DW-image (gradients: " + str(bvecs.cols()) + ", image: " + str(header.size(3)) + ")");
120+
const size_t num_volumes = header.ndim() < 4 ? 1 : header.size(3);
121+
if (size_t(bvals.cols()) != num_volumes)
122+
throw Exception ("bvecs and bvals files must have same number of diffusion directions as DW-image (gradients: " + str(bvecs.cols()) + ", image: " + str(num_volumes) + ")");
122123

123124
// bvecs format actually assumes a LHS coordinate system even if image is
124125
// stored using RHS - x axis is flipped to make linear 3x3 part of

0 commit comments

Comments
 (0)