From e2cec54297abafeab58c5b5bfccec92fafcdcbdb Mon Sep 17 00:00:00 2001 From: Robert David Date: Tue, 14 Apr 2020 12:58:54 -0700 Subject: [PATCH] Check and report allocation failures in InitializeRuntimeTensor. PiperOrigin-RevId: 306497434 Change-Id: I1b94e40f5ea12e5f2c9fbb73be534b12016e76fe --- tensorflow/lite/micro/micro_allocator.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tensorflow/lite/micro/micro_allocator.cc b/tensorflow/lite/micro/micro_allocator.cc index 9cb6cecba44..c72abbd5abf 100644 --- a/tensorflow/lite/micro/micro_allocator.cc +++ b/tensorflow/lite/micro/micro_allocator.cc @@ -351,12 +351,29 @@ TfLiteStatus InitializeRuntimeTensor( reinterpret_cast( allocator->AllocateFromTail(sizeof(TfLiteAffineQuantization), alignof(TfLiteAffineQuantization))); + if (quantization == nullptr) { + TF_LITE_REPORT_ERROR(error_reporter, + "Unable to allocate TfLiteAffineQuantization.\n"); + return kTfLiteError; + } quantization->zero_point = reinterpret_cast(allocator->AllocateFromTail( TfLiteIntArrayGetSizeInBytes(channels), alignof(TfLiteIntArray))); + if (quantization->zero_point == nullptr) { + TF_LITE_REPORT_ERROR(error_reporter, + "Unable to allocate quantization->zero_point.\n"); + return kTfLiteError; + } + quantization->scale = reinterpret_cast( allocator->AllocateFromTail(TfLiteFloatArrayGetSizeInBytes(channels), alignof(TfLiteFloatArray))); + if (quantization->scale == nullptr) { + TF_LITE_REPORT_ERROR(error_reporter, + "Unable to allocate quantization->scale.\n"); + return kTfLiteError; + } + quantization->zero_point->size = channels; quantization->scale->size = channels; int* zero_point_data = quantization->zero_point->data;