Skip to content

Commit c805beb

Browse files
saipraveenb25djohansson
authored andcommitted
Prevent pointer validation for zero-size arrays (shader-slang#4021)
1 parent ab3ea69 commit c805beb

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

prelude/slang-torch-prelude.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,18 @@ TensorView make_tensor_view(torch::Tensor val, const char* name, torch::ScalarTy
136136
if (val.dim() > kSlangTorchTensorMaxDim)
137137
throw std::runtime_error(std::string(name).append(": number of dimensions exceeds limit (").append(std::to_string(kSlangTorchTensorMaxDim)).append(")").c_str());
138138

139+
bool isEmpty = true;
139140
for (int i = 0; i < val.dim(); ++i)
140141
{
141142
res.strides[i] = val.stride(i) * elementSize;
142143
res.sizes[i] = val.size(i);
144+
if (res.sizes[i] > 0)
145+
isEmpty = false;
143146
}
144-
if (!res.data)
147+
148+
if (!res.data && !isEmpty)
145149
throw std::runtime_error(std::string(name).append(": data pointer is invalid.").c_str());
150+
146151
return res;
147152
}
148153

0 commit comments

Comments
 (0)