IF kernel nulltpr fix (#2515)

In some cases if you IF op has 0 outputs, it can reference a nullptr in trying to access node->outputs->size(). This is in the case if your model was flatbuffer_aligned which most likely optimizes away the empty array to nullptr.

BUG=[328527454](https://buganizer.corp.google.com/issues/328527454)
This commit is contained in:
suleshahid 2024-03-21 21:54:02 +00:00 committed by GitHub
parent 79e3f35f3c
commit 15092dfdba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 5 additions and 2 deletions

View file

@ -67,7 +67,7 @@ TfLiteStatus IfPrepare(TfLiteContext* context, TfLiteNode* node) {
// passed to the branch subgraphs. Therefore, the number of subgraph inputs
// will be the number of node inputs - 1.
size_t num_inputs = node->inputs->size - 1;
size_t num_outputs = node->outputs->size;
size_t num_outputs = NumOutputs(node);
MicroGraph& graph_info = micro_context->graph();

View file

@ -252,6 +252,7 @@ TfLiteStatus CopySubgraphOutputsToOpOutputs(TfLiteContext* context,
TfLiteNode* node,
MicroGraph* graph_info,
int subgraph_idx) {
if (graph_info->NumSubgraphOutputs(subgraph_idx) == 0) return kTfLiteOk;
TF_LITE_ENSURE(context, static_cast<size_t>(node->outputs->size) ==
graph_info->NumSubgraphOutputs(subgraph_idx));
for (int i = 0; i < node->outputs->size; i++) {

View file

@ -259,7 +259,9 @@ TfLiteEvalTensor* MicroInterpreterGraph::GetSubgraphInput(int subgraph_idx,
}
size_t MicroInterpreterGraph::NumSubgraphOutputs(int subgraph_idx) {
return model_->subgraphs()->Get(subgraph_idx)->outputs()->size();
return model_->subgraphs()->Get(subgraph_idx)->outputs() == nullptr
? 0
: model_->subgraphs()->Get(subgraph_idx)->outputs()->size();
}
TfLiteEvalTensor* MicroInterpreterGraph::GetSubgraphOutput(int subgraph_idx,