Browse Source

!14411 fix: memcpy_s will fail when size is larger than 2^31 - 1

From: @jonyguo
Reviewed-by: @liucunwei,@heleiwang
Signed-off-by: @liucunwei
pull/14411/MERGE
mindspore-ci-bot Gitee 5 years ago
parent
commit
3ac935344e
1 changed files with 18 additions and 3 deletions
  1. +18
    -3
      mindspore/ccsrc/minddata/dataset/core/tensor.cc

+ 18
- 3
mindspore/ccsrc/minddata/dataset/core/tensor.cc View File

@@ -210,9 +210,24 @@ Status Tensor::CreateFromNpArray(const py::array &arr, std::shared_ptr<Tensor> *
if (is_strided) {
RETURN_IF_NOT_OK(CopyStridedArray((*out)->data_, data, shape, strides, (*out)->type_.SizeInBytes()));
} else {
int ret_code = memcpy_s((*out)->data_, byte_size, data, byte_size);
if (ret_code != 0) {
RETURN_STATUS_UNEXPECTED("Failed to copy data into Tensor.");
// fix: memcpy_s will fail when byte_size > 2^31 - 1
uint32_t step = 1;
while (byte_size > (step * kDeMaxDim)) {
int ret_code =
memcpy_s((*out)->data_ + (step - 1) * kDeMaxDim, kDeMaxDim, data + (step - 1) * kDeMaxDim, kDeMaxDim);
if (ret_code != 0) {
RETURN_STATUS_UNEXPECTED("Failed to copy data into Tensor.");
}
step++;
}

// copy the last
if (byte_size > ((step - 1) * kDeMaxDim) && byte_size <= (step * kDeMaxDim)) {
int ret_code = memcpy_s((*out)->data_ + (step - 1) * kDeMaxDim, byte_size - ((step - 1) * kDeMaxDim),
data + (step - 1) * kDeMaxDim, byte_size - ((step - 1) * kDeMaxDim));
if (ret_code != 0) {
RETURN_STATUS_UNEXPECTED("Failed to copy data into Tensor.");
}
}
}



Loading…
Cancel
Save