Skip to content

Commit f1759a8

Browse files
tensorflower-gardenerFrank Chen
authored and
Frank Chen
committed
Fix a couple of null-ptr dereferences
PiperOrigin-RevId: 188929375
1 parent ffb7dad commit f1759a8

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

tensorflow/contrib/lite/toco/tensorflow_graph_matching/resolve_cluster.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ std::unique_ptr<GraphDef> MaybeReplaceCompositeSubgraph(
144144
MaybeResolveClusters(tf_graph, cluster_factories);
145145

146146
// Copy function definitions
147-
*(pruned_graph->mutable_library()) = tf_graph.library();
147+
if (pruned_graph) {
148+
*(pruned_graph->mutable_library()) = tf_graph.library();
149+
}
148150
return pruned_graph;
149151
}
150152

tensorflow/contrib/lite/toco/tflite/import.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ std::unique_ptr<Model> Import(const ModelFlags& model_flags,
170170
// Full list of all known operators.
171171
const auto ops_by_name = BuildOperatorByNameMap();
172172

173-
if (input_model->subgraphs()->size() != 1) {
174-
LOG(FATAL) << "# of subgraphs in tflite should be exactly 1 for now.";
173+
if (!input_model->subgraphs() || input_model->subgraphs()->size() != 1) {
174+
LOG(FATAL) << "Number of subgraphs in tflite should be exactly 1.";
175175
}
176176
std::unique_ptr<Model> model;
177177
model.reset(new Model);

tensorflow/contrib/lite/toco/tflite/import_test.cc

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,32 @@ namespace {
2727

2828
using ::testing::ElementsAre;
2929

30+
using flatbuffers::Offset;
31+
using flatbuffers::Vector;
3032
class ImportTest : public ::testing::Test {
3133
protected:
3234
template <typename T>
33-
flatbuffers::Offset<flatbuffers::Vector<unsigned char>> CreateDataVector(
34-
const std::vector<T>& data) {
35+
Offset<Vector<unsigned char>> CreateDataVector(const std::vector<T>& data) {
3536
return builder_.CreateVector(reinterpret_cast<const uint8_t*>(data.data()),
3637
sizeof(T) * data.size());
3738
}
38-
// This is a very simplistic model. We are not interested in testing all the
39-
// details here, since tf.mini's testing framework will be exercising all the
40-
// conversions multiple times, and the conversion of operators is tested by
41-
// separate unittests.
42-
void BuildTestModel() {
43-
// The tensors
39+
Offset<Vector<Offset<::tflite::Buffer>>> BuildBuffers() {
40+
auto buf0 = ::tflite::CreateBuffer(builder_, CreateDataVector<float>({}));
41+
auto buf1 =
42+
::tflite::CreateBuffer(builder_, CreateDataVector<float>({1.0f, 2.0f}));
43+
auto buf2 =
44+
::tflite::CreateBuffer(builder_, CreateDataVector<float>({3.0f}));
45+
return builder_.CreateVector(
46+
std::vector<Offset<::tflite::Buffer>>({buf0, buf1, buf2}));
47+
}
48+
49+
Offset<Vector<Offset<::tflite::Tensor>>> BuildTensors() {
4450
auto q = ::tflite::CreateQuantizationParameters(
4551
builder_,
4652
/*min=*/builder_.CreateVector<float>({0.1f}),
4753
/*max=*/builder_.CreateVector<float>({0.2f}),
4854
/*scale=*/builder_.CreateVector<float>({0.3f}),
4955
/*zero_point=*/builder_.CreateVector<int64_t>({100ll}));
50-
auto buf0 = ::tflite::CreateBuffer(builder_, CreateDataVector<float>({}));
51-
auto buf1 =
52-
::tflite::CreateBuffer(builder_, CreateDataVector<float>({1.0f, 2.0f}));
53-
auto buf2 =
54-
::tflite::CreateBuffer(builder_, CreateDataVector<float>({3.0f}));
55-
auto buffers = builder_.CreateVector(
56-
std::vector<flatbuffers::Offset<::tflite::Buffer>>({buf0, buf1, buf2}));
5756
auto t1 = ::tflite::CreateTensor(builder_,
5857
builder_.CreateVector<int>({1, 2, 3, 4}),
5958
::tflite::TensorType_FLOAT32, 1,
@@ -62,17 +61,28 @@ class ImportTest : public ::testing::Test {
6261
::tflite::CreateTensor(builder_, builder_.CreateVector<int>({2, 1}),
6362
::tflite::TensorType_FLOAT32, 2,
6463
builder_.CreateString("tensor_two"), q);
65-
auto tensors = builder_.CreateVector(
66-
std::vector<flatbuffers::Offset<::tflite::Tensor>>({t1, t2}));
64+
return builder_.CreateVector(
65+
std::vector<Offset<::tflite::Tensor>>({t1, t2}));
66+
}
6767

68-
// The operator codes.
68+
Offset<Vector<Offset<::tflite::OperatorCode>>> BuildOpCodes() {
6969
auto c1 =
7070
::tflite::CreateOperatorCode(builder_, ::tflite::BuiltinOperator_CUSTOM,
7171
builder_.CreateString("custom_op_one"));
7272
auto c2 = ::tflite::CreateOperatorCode(
7373
builder_, ::tflite::BuiltinOperator_CONV_2D, 0);
74-
auto opcodes = builder_.CreateVector(
75-
std::vector<flatbuffers::Offset<::tflite::OperatorCode>>({c1, c2}));
74+
return builder_.CreateVector(
75+
std::vector<Offset<::tflite::OperatorCode>>({c1, c2}));
76+
}
77+
78+
// This is a very simplistic model. We are not interested in testing all the
79+
// details here, since tf.mini's testing framework will be exercising all the
80+
// conversions multiple times, and the conversion of operators is tested by
81+
// separate unittests.
82+
void BuildTestModel() {
83+
auto buffers = BuildBuffers();
84+
auto tensors = BuildTensors();
85+
auto opcodes = BuildOpCodes();
7686

7787
auto subgraph = ::tflite::CreateSubGraph(builder_, tensors, 0, 0, 0);
7888
std::vector<flatbuffers::Offset<::tflite::SubGraph>> subgraph_vector(
@@ -133,6 +143,19 @@ TEST_F(ImportTest, Tensors) {
133143
EXPECT_EQ(100, q->zero_point);
134144
}
135145

146+
TEST_F(ImportTest, NoSubGraphs) {
147+
auto buffers = BuildBuffers();
148+
auto opcodes = BuildOpCodes();
149+
auto subgraphs = 0; // no subgraphs in this model
150+
auto comment = builder_.CreateString("");
151+
builder_.Finish(::tflite::CreateModel(builder_, TFLITE_SCHEMA_VERSION,
152+
opcodes, subgraphs, comment, buffers));
153+
input_model_ = ::tflite::GetModel(builder_.GetBufferPointer());
154+
155+
EXPECT_DEATH(Import(ModelFlags(), InputModelAsString()),
156+
"Number of subgraphs in tflite should be exactly 1.");
157+
}
158+
136159
// TODO(ahentz): still need tests for Operators and IOTensors.
137160

138161
} // namespace

0 commit comments

Comments
 (0)