Skip to content

Commit 8511b30

Browse files
authored
Enforce src and dst do not overlap in memcpy
Differential Revision: D88320713 Pull Request resolved: pytorch#16083
1 parent 1fe59c8 commit 8511b30

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

kernels/portable/cpu/op_constant_pad_nd.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ void apply_padding_to_dim(
8383
size_t copy_nbytes = copy_len * sizeof(CTYPE);
8484

8585
if (copy_nbytes > 0) {
86+
// Check that out_data and self_data do not overlap.
87+
ET_KERNEL_CHECK_MSG(
88+
ctx,
89+
out_data != self_data &&
90+
((out_data + copy_len < self_data) ||
91+
(self_data + copy_len < out_data)),
92+
InvalidArgument,
93+
/* void */,
94+
"Out tensor overlaps with the input tensor. This is not supported.");
8695
memcpy(out_data, self_data, copy_nbytes);
8796
out_data += copy_len;
8897
self_data += copy_len;

0 commit comments

Comments
 (0)