malloc data nullptr check

This commit is contained in:
chenjianping 2020-09-18 20:55:56 +08:00
parent 0ae07ec43d
commit 966dc9fb16
4 changed files with 36 additions and 13 deletions

View File

@ -199,7 +199,7 @@ void String::clear() noexcept {
String &String::operator+=(const String &str) {
size_t new_size = size_ + str.size_;
char *tmp = reinterpret_cast<char *>(malloc(sizeof(char) * (new_size + 1)));
if (buffer_ == nullptr) {
if (tmp == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memcpy(tmp, this->buffer_, size_ + 1);
@ -218,7 +218,7 @@ String &String::operator+=(const char *str) {
size_t str_size = strlen(str);
size_t new_size = size_ + str_size;
char *tmp = reinterpret_cast<char *>(malloc(sizeof(char) * (new_size + 1)));
if (buffer_ == nullptr) {
if (tmp == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memcpy(tmp, this->buffer_, size_ + 1);
@ -232,7 +232,7 @@ String &String::operator+=(const char *str) {
String &String::operator+=(const char ch) {
char *tmp = reinterpret_cast<char *>(malloc(sizeof(char) * (size_ + 2)));
if (buffer_ == nullptr) {
if (tmp == nullptr) {
MS_C_EXCEPTION("malloc data failed");
}
memcpy(tmp, this->buffer_, size_ + 1);

View File

@ -155,6 +155,7 @@ int DoMatMul(const TensorPtrVector &in_tensors, const TensorPtrVector &out_tenso
kernel_data->a_c12_ptr_
= reinterpret_cast<float *>(allocator->Malloc(params->batch * params->row_12_ * params->deep_ * sizeof(float)));
if (kernel_data->a_c12_ptr_ == NULL) {
FreeMatMulKernelData(kernel_data, allocator);
return RET_MEMORY_FAILED;
}
memset(kernel_data->a_c12_ptr_, 0, params->row_12_ * params->deep_ * sizeof(float));

View File

@ -58,12 +58,14 @@ int MallocTmpBuffer(std::vector<float *> *data_buffers, const ShapeVector &shape
return RET_OK;
}
int FreeTmpBuffer(std::vector<float *> *data_buffers, mindspore::lite::Allocator *allocator) {
void FreeTmpBuffer(std::vector<float *> *data_buffers, mindspore::lite::Allocator *allocator) {
if (data_buffers == nullptr) {
return;
}
for (int i = 0; i < data_buffers->size(); ++i) {
allocator->Free(data_buffers->at(i));
}
data_buffers->clear();
return RET_OK;
}
int RunReduce(Reducer reducer, std::vector<float *> data_buffers, float *in_data, float *out_data, Int32Vector axes,
@ -220,13 +222,11 @@ int DoReduce(const TensorPtrVector &in_tensors, const TensorPtrVector &out_tenso
}
status = RunReduce(reducer, data_buffers, reinterpret_cast<float *>(in_tensors[0]->data_),
reinterpret_cast<float *>(out_tensors[0]->data_), axes, in_tensors[0]->shape_);
if (status != RET_OK) {
return status;
}
status = FreeTmpBuffer(&data_buffers, allocator);
FreeTmpBuffer(&data_buffers, allocator);
if (status != RET_OK) {
return status;
return RET_ERROR;
}
return RET_OK;
}

View File

@ -17,6 +17,7 @@
#include "internal/include/string.h"
#include "internal/include/vector.h"
#include "internal/include/ms_tensor.h"
#include "internal/src/lite_log.h"
MSTensor *CreateTensor(TypeId data_type, const ShapeVector &shape) {
MSTensor *tensor = new MSTensor();
@ -28,7 +29,12 @@ MSTensor *CreateTensor(TypeId data_type, const ShapeVector &shape) {
return tensor;
}
void DestroyTensor(MSTensor *ptr) { delete ptr; }
void DestroyTensor(MSTensor *ptr) {
if (ptr == nullptr) {
return;
}
delete ptr;
}
int MSTensor::ElementsNum() const {
int result = 1;
@ -206,14 +212,30 @@ int MSTensor::ElementsC4Num() const {
void *MSTensor::operator new(size_t sz) {
void *storage = malloc(sz);
if (storage == nullptr) {
MS_C_EXCEPTION("malloc tensor fail!");
}
return storage;
}
void *MSTensor::operator new[](size_t sz) {
void *storage = malloc(sz);
if (storage == nullptr) {
MS_C_EXCEPTION("malloc tensor array fail!");
}
return storage;
}
void MSTensor::operator delete(void *ptr, size_t sz) { free(ptr); }
void MSTensor::operator delete(void *ptr, size_t sz) {
if (ptr == nullptr) {
return;
}
free(ptr);
}
void MSTensor::operator delete[](void *ptr, size_t sz) { free(ptr); }
void MSTensor::operator delete[](void *ptr, size_t sz) {
if (ptr == nullptr) {
return;
}
free(ptr);
}