Skip to content

Commit ce5d965

Browse files
kirklandsignfacebook-github-bot
authored andcommitted
Fix accessing nullptr in some ops
Summary: When dim is 0, some data pointers in a tensor, like Tensor.strides() and Tensor.sizes() is nullptr. We should not access it, even though it's a no-op in a memcpy. This is enforced in fbcode but not xplat. Reviewed By: manuelcandales Differential Revision: D48477031 fbshipit-source-id: 582da830dc47869cad2a8675cf928e3c8f1d8e43
1 parent 19a5994 commit ce5d965

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

kernels/portable/cpu/op_constant_pad_nd.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ void apply_padding_to_dim(
101101
size_t copy_len = in_step_len * self_sizes[dim];
102102
size_t copy_nbytes = copy_len * sizeof(CTYPE);
103103

104-
memcpy(out_data, self_data, copy_nbytes);
105-
106-
out_data += copy_len;
107-
self_data += copy_len;
104+
if (copy_nbytes > 0) {
105+
memcpy(out_data, self_data, copy_nbytes);
106+
out_data += copy_len;
107+
self_data += copy_len;
108+
}
108109
}
109110
// Otherwise, call this function recursively
110111
else {

kernels/portable/cpu/op_slice_copy.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ Tensor& slice_copy_Tensor_out(
144144
size_t leading_dims = getLeadingDims(input, dim);
145145
size_t trailing_dims = getTrailingDims(input, dim);
146146

147+
if (trailing_dims == 0) {
148+
return out;
149+
}
150+
147151
size_t length_per_step = trailing_dims * input.element_size();
148152

149153
const char* input_data = input.const_data_ptr<char>();

kernels/portable/cpu/op_slice_scatter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ Tensor& slice_scatter_out(
145145
auto error = resize_like_input(input, out);
146146
ET_CHECK_MSG(error == Error::Ok, "Failed to resize output tensor.");
147147

148+
if (input.numel() == 0) {
149+
return out;
150+
}
151+
148152
// If user do not set value to end_val, set end to input.size(dim) (largest
149153
// value available)
150154
int64_t end = end_val.has_value() ? end_val.value() : input.size(dim);

kernels/portable/cpu/util/transpose_util.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,23 @@ void transpose_tensors(
8686
auto dim = a.dim();
8787
auto data_a = a.const_data_ptr<T>();
8888
auto data_out = out.mutable_data_ptr<T>();
89-
auto a_sizes = a.sizes();
90-
auto a_strides = a.strides();
9189

9290
size_t out_index[kTensorDimensionLimit];
9391
memset(out_index, 0, sizeof(out_index));
9492

9593
StridesType new_strides[kTensorDimensionLimit];
96-
memcpy(new_strides, a_strides.data(), dim * sizeof(StridesType));
97-
9894
SizesType new_sizes[kTensorDimensionLimit];
99-
memcpy(new_sizes, a_sizes.data(), dim * sizeof(SizesType));
10095

101-
std::swap(new_sizes[dim0], new_sizes[dim1]);
102-
std::swap(new_strides[dim1], new_strides[dim0]);
96+
if (dim != 0) {
97+
auto a_strides = a.strides();
98+
memcpy(new_strides, a_strides.data(), dim * sizeof(StridesType));
99+
100+
auto a_sizes = a.sizes();
101+
memcpy(new_sizes, a_sizes.data(), dim * sizeof(SizesType));
102+
103+
std::swap(new_sizes[dim0], new_sizes[dim1]);
104+
std::swap(new_strides[dim1], new_strides[dim0]);
105+
}
103106

104107
// non_1_dim_indices stores the indices of the dimensions that have a value
105108
// greater than 1. Dimensions can only have a value of 1 or larger.

0 commit comments

Comments
 (0)