diff --git a/runtime/executor/tensor_parser_portable.cpp b/runtime/executor/tensor_parser_portable.cpp index 3f190060f7e..79e4c4bd964 100644 --- a/runtime/executor/tensor_parser_portable.cpp +++ b/runtime/executor/tensor_parser_portable.cpp @@ -101,6 +101,19 @@ Result parseTensor( sizes = const_cast(serialized_sizes); dim_order = const_cast(serialized_dim_order); } + // Validate sizes before using them in case the PTE data is bad. We can't + // detect bad positive values, but we can reject negative values, which would + // otherwise panic in the TensorImpl ctor. dim_order_to_stride() will validate + // dim_order. + for (int i = 0; i < dim; i++) { + ET_CHECK_OR_RETURN_ERROR( + sizes[i] >= 0, + InvalidProgram, + "Negative size[%d] %" PRId32, + i, + sizes[i]); + } + // We will remove strides from schema. // Allocating strides buffer here and populating it. // In subsequent diffs we can remove strides accessor, however this