From c5bb6eaac84013dae2c454b65f416caa2c676201 Mon Sep 17 00:00:00 2001 From: lyvette Date: Tue, 1 Sep 2020 15:13:31 +0800 Subject: [PATCH] check if new is nullpter --- mindspore/lite/src/runtime/allocator.cc | 4 +++- mindspore/lite/src/runtime/kernel/arm/base/matrix.cc | 6 +++++- mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc | 4 ++-- .../lite/src/runtime/kernel/arm/fp16/deconvolution_fp16.h | 5 ++++- .../lite/src/runtime/kernel/arm/fp32/convolution_1x1.h | 5 ++++- .../src/runtime/kernel/arm/fp32_grad/arithmetic_grad.cc | 6 +++++- .../lite/src/runtime/kernel/arm/fp32_grad/bias_grad.cc | 5 ++++- mindspore/lite/src/runtime/kernel/arm/fp32_grad/bn_grad.cc | 6 +++++- .../runtime/kernel/arm/fp32_grad/convolution_grad_filter.cc | 6 +++++- .../lite/src/runtime/kernel/arm/fp32_grad/opt_momentum.cc | 5 ++++- .../lite/src/runtime/kernel/arm/fp32_grad/pooling_grad.cc | 6 +++++- .../lite/src/runtime/kernel/arm/fp32_grad/power_grad.cc | 5 +++++ .../fp32_grad/sparse_softmax_cross_entropy_with_logits.cc | 6 +++++- .../lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc | 5 +++++ 14 files changed, 61 insertions(+), 13 deletions(-) diff --git a/mindspore/lite/src/runtime/allocator.cc b/mindspore/lite/src/runtime/allocator.cc index 3b047e6690..a190e97072 100644 --- a/mindspore/lite/src/runtime/allocator.cc +++ b/mindspore/lite/src/runtime/allocator.cc @@ -19,7 +19,9 @@ #include "utils/log_adapter.h" namespace mindspore::lite { -std::shared_ptr Allocator::Create() { return std::shared_ptr(new DefaultAllocator()); } +std::shared_ptr Allocator::Create() { + return std::shared_ptr(new (std::nothrow) DefaultAllocator()); +} DefaultAllocator::DefaultAllocator() {} diff --git a/mindspore/lite/src/runtime/kernel/arm/base/matrix.cc b/mindspore/lite/src/runtime/kernel/arm/base/matrix.cc index f84cf92a68..aed7e0ebb8 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/matrix.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/matrix.cc @@ -19,7 +19,11 @@ namespace mindspore::kernel { Matrix *TransformMatrixGenerator(int m, int k) { - auto matrix = new Matrix; + auto matrix = new (std::nothrow) Matrix; + if (matrix == nullptr) { + MS_LOG(ERROR) << "matrix is nullptr."; + return nullptr; + } auto data = malloc(m * k * sizeof(float)); if (data == nullptr) { MS_LOG(ERROR) << "Malloc matrix data failed."; diff --git a/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc index 04419db7f3..87c00fe745 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc @@ -66,7 +66,7 @@ kernel::LiteKernel *CpuSoftmaxInt8KernelCreator(const std::vector &outputs, const lite::Context *ctx, const mindspore::lite::PrimitiveC *primitive) : ConvolutionBaseFP16CPUKernel(parameter, inputs, outputs, ctx, primitive) { - matmul_param_ = new MatMulParameter(); + matmul_param_ = new (std::nothrow) MatMulParameter(); + if (matmul_param_ == nullptr) { + MS_LOG(ERROR) << "new MatMulParameter fail!"; + } } ~DeConvolutionFp16CPUKernel() override; int Init() override; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_1x1.h b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_1x1.h index 7b7413f516..bd7b0e589a 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_1x1.h +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_1x1.h @@ -37,7 +37,10 @@ class Convolution1x1CPUKernel : public ConvolutionBaseCPUKernel { const std::vector &outputs, const lite::Context *ctx, const mindspore::lite::PrimitiveC *primitive) : ConvolutionBaseCPUKernel(parameter, inputs, outputs, ctx, primitive) { - matmul_param_ = new MatMulParameter(); + matmul_param_ = new (std::nothrow) MatMulParameter(); + if (matmul_param_ == nullptr) { + MS_LOG(ERROR) << "new MatMulParameter fail!"; + } } ~Convolution1x1CPUKernel(); int Init() override; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/arithmetic_grad.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/arithmetic_grad.cc index 7dd8f64357..24e5a253d6 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/arithmetic_grad.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/arithmetic_grad.cc @@ -282,7 +282,11 @@ kernel::LiteKernel *CpuArithmeticGradFp32KernelCreator(const std::vectorInit(); if (ret != RET_OK) { MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: " diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bias_grad.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bias_grad.cc index d3a9649814..e02ee48391 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bias_grad.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bias_grad.cc @@ -103,7 +103,10 @@ kernel::LiteKernel *CpuBiasGradFp32KernelCreator(const std::vector(opParameter), inputs, outputs, ctx, primitive); - MS_ASSERT(kernel != nullptr); + if (kernel == nullptr) { + MS_LOG(ERROR) << "new BiasGradCPUKernel fail!"; + return nullptr; + } auto ret = kernel->Init(); if (RET_OK != ret) { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bn_grad.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bn_grad.cc index dedaa8f0f5..06749be478 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bn_grad.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/bn_grad.cc @@ -33,7 +33,11 @@ namespace mindspore::kernel { int BNGradInputCPUKernel::Init() { auto bn_param = reinterpret_cast(opParameter); workspace_size = 5 * bn_param->channels; - workspace = new float[workspace_size]; + workspace = new (std::nothrow) float[workspace_size]; + if (workspace == nullptr) { + MS_LOG(ERROR) << "new workspace fail!"; + return RET_ERROR; + } if (2 != this->inputs_.size()) { MS_LOG(ERROR) << "Conv2d Grad should has 2 inputs"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/convolution_grad_filter.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/convolution_grad_filter.cc index ca4030e962..cd69162d52 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/convolution_grad_filter.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/convolution_grad_filter.cc @@ -61,7 +61,11 @@ int ConvolutionGradFilterCPUKernel::Init() { int ws_size = conv_param->output_h_ * conv_param->output_w_ * conv_param->kernel_h_ * conv_param->kernel_w_ * conv_param->input_channel_ / conv_param->group_; - workspace = new float[ws_size]; + workspace = new (std::nothrow) float[ws_size]; + if (workspace == nullptr) { + MS_LOG(ERROR) << "new workspace fail!"; + return RET_ERROR; + } int output_w = 0; int output_h = 0; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/opt_momentum.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/opt_momentum.cc index 5494dade61..4d332b8f84 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/opt_momentum.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/opt_momentum.cc @@ -68,7 +68,10 @@ kernel::LiteKernel *CpuOptMomentumFp32KernelCreator(const std::vectorInit(); if (0 != ret) { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/pooling_grad.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/pooling_grad.cc index 5348bdb3f3..6e387cb221 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/pooling_grad.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/pooling_grad.cc @@ -181,7 +181,11 @@ kernel::LiteKernel *CpuPoolingGradFp32KernelCreator(const std::vectorInit(); if (RET_OK != ret) { MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: " diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/power_grad.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/power_grad.cc index 8cd1c6ee6b..5127acb8cb 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/power_grad.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/power_grad.cc @@ -55,6 +55,11 @@ kernel::LiteKernel *CpuPowerGradFp32KernelCreator(const std::vectorInit(); if (ret != RET_OK) { MS_LOG(ERROR) << "Init kernel failed, name: " << opParameter->name_ << ", type: " diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/sparse_softmax_cross_entropy_with_logits.cc b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/sparse_softmax_cross_entropy_with_logits.cc index ee906eb686..315171efcc 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32_grad/sparse_softmax_cross_entropy_with_logits.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32_grad/sparse_softmax_cross_entropy_with_logits.cc @@ -79,7 +79,11 @@ int SparseSoftmaxCrossEntropyWithLogitsCPUKernel::Run() { } size_t data_size = inputs_.at(0)->ElementsNum(); float *losses = new (std::nothrow) float[data_size]; - MS_ASSERT(losses != nullptr); + if (losses == nullptr) { + MS_LOG(ERROR) << "losses is null"; + return nullptr; + } + std::fill(losses, losses + data_size, 0); MS_ASSERT(out != nullptr); diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc index dd6161df35..2cdf490445 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc @@ -29,6 +29,11 @@ namespace mindspore::kernel { DeConvInt8CPUKernel::~DeConvInt8CPUKernel() { FreeTmpBuffer(); ConvolutionBaseCPUKernel::FreeQuantParam(); + + if (matmul_param_ != nullptr) { + delete matmul_param_; + matmul_param_ = nullptr; + } } void DeConvInt8CPUKernel::FreeTmpBuffer() {