Fix C6011: dereferencing NULL pointer with_data (and external_data) (#12982)

As title. For pattern like
```cpp
foo(*ptr)
```

we change them to
```cpp
if (ptr)
  foo(*ptr)
else
  throw
```
This commit is contained in:
Wei-Sheng Chin 2022-09-16 09:49:36 -07:00 committed by GitHub
parent 12aab3c01d
commit 1a684152cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1793,7 +1793,8 @@ TEST_F(GraphTest, InjectExternalInitializedTensors) {
tensor_proto.add_dims(tensor_data.size());
tensor_proto.set_data_type(ONNX_NAMESPACE::TensorProto_DataType_INT32);
tensor_proto.set_data_location(ONNX_NAMESPACE::TensorProto_DataLocation_EXTERNAL);
SetTensorProtoExternalData("location", ToUTF8String(tensor_data_dir_relative_path.ToPathString()), tensor_proto);
SetTensorProtoExternalData("location", ToUTF8String(tensor_data_dir_relative_path.ToPathString()),
tensor_proto);
SetTensorProtoExternalData("offset", "0", tensor_proto);
SetTensorProtoExternalData("length", std::to_string(tensor_data.size() * sizeof(int32_t)), tensor_proto);
return tensor_proto;
@ -1807,7 +1808,13 @@ TEST_F(GraphTest, InjectExternalInitializedTensors) {
const TensorProto* external_data = nullptr;
ASSERT_TRUE(graph.GetInitializedTensor(initializer_name, external_data));
ASSERT_TRUE(utils::HasExternalData(*external_data));
if (external_data) {
ASSERT_TRUE(utils::HasExternalData(*external_data));
} else {
// Fail the test because external_data shouldn't be nullptr.
// This if-else is added for suppressing warning C6011: dereferencing NULL pointer.
ASSERT_TRUE(false);
}
// Replace things.
ASSERT_STATUS_OK(graph.InjectExternalInitializedTensors(injection_initializers));
@ -1817,17 +1824,21 @@ TEST_F(GraphTest, InjectExternalInitializedTensors) {
const TensorProto* with_data = nullptr;
ASSERT_TRUE(graph.GetInitializedTensor(initializer_name, with_data));
// No longer has external data
ASSERT_FALSE(utils::HasExternalData(*with_data));
const auto& original_tensor = ort_value.Get<Tensor>();
Tensor replaced_tensor(original_tensor.DataType(), data_shape, std::make_shared<CPUAllocator>());
ASSERT_STATUS_OK(utils::TensorProtoToTensor(Env::Default(), tensor_data_dir_path.ToPathString().c_str(), *with_data, replaced_tensor));
ASSERT_EQ(original_tensor.GetElementType(), replaced_tensor.GetElementType());
const auto original_span = original_tensor.DataAsSpan<int32_t>();
const auto replaced_span = replaced_tensor.DataAsSpan<int32_t>();
ASSERT_EQ(original_span, replaced_span);
if (with_data) {
ASSERT_FALSE(utils::HasExternalData(*with_data));
const auto& original_tensor = ort_value.Get<Tensor>();
Tensor replaced_tensor(original_tensor.DataType(), data_shape, std::make_shared<CPUAllocator>());
ASSERT_STATUS_OK(utils::TensorProtoToTensor(Env::Default(), tensor_data_dir_path.ToPathString().c_str(), *with_data,
replaced_tensor));
ASSERT_EQ(original_tensor.GetElementType(), replaced_tensor.GetElementType());
const auto original_span = original_tensor.DataAsSpan<int32_t>();
const auto replaced_span = replaced_tensor.DataAsSpan<int32_t>();
ASSERT_EQ(original_span, replaced_span);
} else {
// Fail the test because with_data shouldn't be nullptr.
// This if-else is added for suppressing warning C6011: dereferencing NULL pointer.
ASSERT_TRUE(false);
}
}
#endif