message: Fix model fuzz test failures caused by malformed sparsity parameters.
diff: @@ -424,19 +424,43 @@ TfLiteStatus InterpreterBuilder::ParseSparsity(
  return kTfLiteOk;
  }
  + if (src_sparsity->traversal_order() == nullptr ||
  + src_sparsity->dim_metadata() == nullptr) {
  + error_reporter_->Report("Invalid sparsity parameter.");
  + return kTfLiteError;
  + }
  +
  + const size_t dim_metadata_size = src_sparsity->dim_metadata()->size();
  + // Validate sparsity params before allocating the TfLiteSparsity output.
  + for (int i = 0; i < dim_metadata_size; i++) {
  + const auto* src_metadata = src_sparsity->dim_metadata()->Get(i);
  + if (src_metadata->format() != DimensionType_DENSE &&
  + src_metadata->format() != DimensionType_SPARSE_CSR) {
  + error_reporter_->Report("The %dth dimension has unknown type: %d.", i,
  + src_metadata->format());
  + return kTfLiteError;
  + }
  +
  + if (src_metadata->format() == DimensionType_SPARSE_CSR &&
  + (src_metadata->array_indices() == nullptr ||
  + src_metadata->array_segments() == nullptr)) {
  + error_reporter_->Report(
  + "The %dth sparse dimension has invalid parameters.", i);
  + return kTfLiteError;
  + }
  + }
  +
  auto* sparsity =
  reinterpret_cast<TfLiteSparsity*>(malloc(sizeof(TfLiteSparsity)));
  memset(sparsity, 0, sizeof(TfLiteSparsity));
  *sparsity_ptr = sparsity;
  - if (src_sparsity->traversal_order()) {
  const size_t traversal_order_size = src_sparsity->traversal_order()->size();
  sparsity->traversal_order = TfLiteIntArrayCreate(traversal_order_size);
  for (int i = 0; i < traversal_order_size; i++) {
  sparsity->traversal_order->data[i] =
  src_sparsity->traversal_order()->Get(i);
  }
  - }
  if (src_sparsity->block_map()) {
  const size_t block_map_size = src_sparsity->block_map()->size();
  @@ -446,8 +470,6 @@ TfLiteStatus InterpreterBuilder::ParseSparsity(
  }
  }
  - if (src_sparsity->dim_metadata()) {
  - const size_t dim_metadata_size = src_sparsity->dim_metadata()->size();
  sparsity->dim_metadata_size = dim_metadata_size;
  sparsity->dim_metadata = reinterpret_cast<TfLiteDimensionMetadata*>(
  malloc(dim_metadata_size * sizeof(TfLiteDimensionMetadata)));
  @@ -463,10 +485,9 @@ TfLiteStatus InterpreterBuilder::ParseSparsity(
  if (tgt_metadata->format == kTfLiteDimDense) {
  tgt_metadata->dense_size = src_metadata->dense_size();
  - } else if (tgt_metadata->format == kTfLiteDimSparseCSR) {
  + } else {
  const int array_segments_size = src_metadata->array_segments()->size();
  - tgt_metadata->array_segments =
  - TfLiteIntArrayCreate(array_segments_size);
  + tgt_metadata->array_segments = TfLiteIntArrayCreate(array_segments_size);
  for (int j = 0; j < array_segments_size; j++) {
  tgt_metadata->array_segments->data[j] =
  src_metadata->array_segments()->Get(j);
  @@ -477,10 +498,6 @@ TfLiteStatus InterpreterBuilder::ParseSparsity(
  tgt_metadata->array_indices->data[j] =
  src_metadata->array_indices()->Get(j);
  }
  - } else {
  - error_reporter_->Report("Unsupported dimension type.");
  - return kTfLiteError;
  - }
  }
  }
  