Skip to content

Commit 0b77518

Browse files
authored
Merge pull request #12982 from Sand3r-/mutable-data-check
Enforce requested size of tensor to be sufficiently large
2 parents 26cac36 + b6d261d commit 0b77518

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

paddle/fluid/framework/tensor.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ void* Tensor::mutable_data(platform::Place place, std::type_index type,
4040
"When calling this method, the Tensor's numel must be "
4141
"equal or larger than zero. "
4242
"Please check Tensor::Resize has been called first.");
43-
size_t size = requested_size ? requested_size : numel() * SizeOfType(type);
43+
size_t size = numel() * SizeOfType(type);
44+
if (requested_size) {
45+
PADDLE_ENFORCE_GE(requested_size, size);
46+
size = requested_size;
47+
}
4448
/* some versions of boost::variant don't have operator!= */
4549
if (holder_ == nullptr || !(holder_->place() == place) ||
4650
holder_->size() < size + offset_) {

0 commit comments

Comments
 (0)