Skip to content

Commit fda2565

Browse files
committed
check nullptr in TargetMalloc and flatbuffers, test=develop
1 parent 7d36438 commit fda2565

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

lite/backends/host/target_wrapper.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ const int MALLOC_ALIGN = 64;
2424
void* TargetWrapper<TARGET(kHost)>::Malloc(size_t size) {
2525
size_t offset = sizeof(void*) + MALLOC_ALIGN - 1;
2626
char* p = static_cast<char*>(malloc(offset + size));
27-
if (!p) {
28-
return nullptr;
29-
}
27+
CHECK(p);
3028
void* r = reinterpret_cast<void*>(reinterpret_cast<size_t>(p + offset) &
3129
(~(MALLOC_ALIGN - 1)));
3230
static_cast<void**>(r)[-1] = p;
@@ -41,6 +39,8 @@ void TargetWrapper<TARGET(kHost)>::MemcpySync(void* dst,
4139
const void* src,
4240
size_t size,
4341
IoDirection dir) {
42+
CHECK(dst);
43+
CHECK(src);
4444
memcpy(dst, src, size);
4545
}
4646

lite/model_parser/flatbuffers/io.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ void SetParamWithTensor(const std::string& name,
6161
}
6262

6363
void SetTensorWithParam(lite::Tensor* tensor, const ParamDescReadAPI& param) {
64+
CHECK(tensor);
6465
tensor->Resize(param.Dim());
6566
tensor->set_precision(lite::ConvertPrecisionType(param.GetDataType()));
66-
std::memcpy(tensor->mutable_data(param.byte_size()),
67-
param.GetData(),
68-
param.byte_size());
67+
auto* dst = tensor->mutable_data(param.byte_size());
68+
CHECK(dst);
69+
CHECK(param.GetData());
70+
std::memcpy(dst, param.GetData(), param.byte_size());
6971
}
7072

7173
void SetCombinedParamsWithScope(const lite::Scope& scope,
@@ -82,9 +84,11 @@ void SetScopeWithCombinedParams(lite::Scope* scope,
8284
const CombinedParamsDescReadAPI& params) {
8385
CHECK(scope);
8486
for (size_t i = 0; i < params.GetParamsSize(); ++i) {
85-
const auto& param = *params.GetParamDesc(i);
86-
auto* tensor = scope->Var(param.Name())->GetMutable<lite::Tensor>();
87-
SetTensorWithParam(tensor, param);
87+
const auto* param = params.GetParamDesc(i);
88+
CHECK(param);
89+
auto* tensor = scope->Var(param->Name())->GetMutable<lite::Tensor>();
90+
CHECK(tensor);
91+
SetTensorWithParam(tensor, *param);
8892
tensor->set_persistable(true);
8993
}
9094
}

lite/model_parser/flatbuffers/param_desc.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ class ParamDescView : public ParamDescReadAPI {
3636
CHECK(desc_->variable_type() ==
3737
proto::ParamDesc_::VariableDesc_LoDTensorDesc);
3838
tensor_desc_ = desc_->variable_as<proto::ParamDesc_::LoDTensorDesc>();
39+
CHECK(tensor_desc_);
40+
CHECK(tensor_desc_->data());
41+
}
42+
std::string Name() const override {
43+
CHECK(desc_->name());
44+
return desc_->name()->c_str();
3945
}
40-
std::string Name() const override { return desc_->name()->c_str(); }
4146

4247
std::vector<int64_t> Dim() const override {
43-
const auto& dims = tensor_desc_->dim();
48+
const auto* dims = tensor_desc_->dim();
49+
CHECK(dims);
4450
std::vector<int64_t> dims_vec;
4551
dims_vec.resize(dims->size());
4652
for (size_t i = 0; i < dims->size(); ++i) {
@@ -86,6 +92,8 @@ class CombinedParamsDescView : public CombinedParamsDescReadAPI {
8692

8793
void InitParams() {
8894
desc_ = proto::GetCombinedParamsDesc(buf_.data());
95+
CHECK(desc_);
96+
CHECK(desc_->params());
8997
size_t params_size = desc_->params()->size();
9098
params_.resize(params_size);
9199
for (size_t idx = 0; idx < params_size; ++idx) {

0 commit comments

Comments
 (0)