Skip to content

Commit 652cc5a

Browse files
authored
Fix write-heap-buffer-overflow in copy_out
Differential Revision: D80885980 Pull Request resolved: #15584
1 parent 319789c commit 652cc5a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

kernels/portable/cpu/op_copy.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Tensor& copy_out(
4949
// Use direct copy fast path if broadcast is not needed and tensors are
5050
// non-empty
5151
if (internal::sizes_match_ignoring_leading_1s(out.sizes(), src.sizes()) &&
52-
src.numel() > 0) {
52+
src.numel() > 0 && out.nbytes() >= src.nbytes() &&
53+
tensors_have_same_dtype(src, out)) {
5354
std::memcpy(out.mutable_data_ptr(), src.const_data_ptr(), src.nbytes());
5455
} else {
5556
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, op_name, CTYPE, [&]() {
@@ -91,8 +92,9 @@ Tensor& copy_(
9192
// Use direct copy fast path if broadcast is not needed and tensors are
9293
// non-empty
9394
if (internal::sizes_match_ignoring_leading_1s(in.sizes(), src.sizes()) &&
94-
src.numel() > 0) {
95-
std::memcpy(in.mutable_data_ptr(), src.const_data_ptr(), in.nbytes());
95+
src.numel() > 0 && in.nbytes() >= src.nbytes() &&
96+
tensors_have_same_dtype(src, in)) {
97+
std::memcpy(in.mutable_data_ptr(), src.const_data_ptr(), src.nbytes());
9698
} else {
9799
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, op_name, CTYPE, [&]() {
98100
utils::apply_bitensor_elementwise_fn<

0 commit comments

Comments
 (0)