@@ -63,7 +63,22 @@ class BoxedEvalueList {
6363 * unwrapped vals.
6464 */
6565 BoxedEvalueList (EValue** wrapped_vals, T* unwrapped_vals, int size)
66- : wrapped_vals_(wrapped_vals, size), unwrapped_vals_(unwrapped_vals) {}
66+ : wrapped_vals_(checkWrappedVals(wrapped_vals, size), size),
67+ unwrapped_vals_ (checkUnwrappedVals(unwrapped_vals)) {}
68+
69+ private:
70+ static EValue** checkWrappedVals (EValue** wrapped_vals, int size) {
71+ ET_CHECK_MSG (wrapped_vals != nullptr , " wrapped_vals cannot be null" );
72+ ET_CHECK_MSG (size >= 0 , " size cannot be negative" );
73+ return wrapped_vals;
74+ }
75+
76+ static T* checkUnwrappedVals (T* unwrapped_vals) {
77+ ET_CHECK_MSG (unwrapped_vals != nullptr , " unwrapped_vals cannot be null" );
78+ return unwrapped_vals;
79+ }
80+
81+ public:
6782 /*
6883 * Constructs and returns the list of T specified by the EValue pointers
6984 */
@@ -280,6 +295,7 @@ struct EValue {
280295
281296 /* ***** String Type ******/
282297 /* implicit*/ EValue(executorch::aten::ArrayRef<char >* s) : tag(Tag::String) {
298+ ET_CHECK_MSG (s != nullptr , " ArrayRef<char> pointer cannot be null" );
283299 payload.copyable_union .as_string_ptr = s;
284300 }
285301
@@ -289,13 +305,18 @@ struct EValue {
289305
290306 std::string_view toString () const {
291307 ET_CHECK_MSG (isString (), " EValue is not a String." );
308+ ET_CHECK_MSG (
309+ payload.copyable_union .as_string_ptr != nullptr ,
310+ " EValue string pointer is null." );
292311 return std::string_view (
293312 payload.copyable_union .as_string_ptr ->data (),
294313 payload.copyable_union .as_string_ptr ->size ());
295314 }
296315
297316 /* ***** Int List Type ******/
298317 /* implicit*/ EValue(BoxedEvalueList<int64_t >* i) : tag(Tag::ListInt) {
318+ ET_CHECK_MSG (
319+ i != nullptr , " BoxedEvalueList<int64_t> pointer cannot be null" );
299320 payload.copyable_union .as_int_list_ptr = i;
300321 }
301322
@@ -305,12 +326,16 @@ struct EValue {
305326
306327 executorch::aten::ArrayRef<int64_t > toIntList () const {
307328 ET_CHECK_MSG (isIntList (), " EValue is not an Int List." );
329+ ET_CHECK_MSG (
330+ payload.copyable_union .as_int_list_ptr != nullptr ,
331+ " EValue int list pointer is null." );
308332 return (payload.copyable_union .as_int_list_ptr )->get ();
309333 }
310334
311335 /* ***** Bool List Type ******/
312336 /* implicit*/ EValue(executorch::aten::ArrayRef<bool >* b)
313337 : tag(Tag::ListBool) {
338+ ET_CHECK_MSG (b != nullptr , " ArrayRef<bool> pointer cannot be null" );
314339 payload.copyable_union .as_bool_list_ptr = b;
315340 }
316341
@@ -320,12 +345,16 @@ struct EValue {
320345
321346 executorch::aten::ArrayRef<bool > toBoolList () const {
322347 ET_CHECK_MSG (isBoolList (), " EValue is not a Bool List." );
348+ ET_CHECK_MSG (
349+ payload.copyable_union .as_bool_list_ptr != nullptr ,
350+ " EValue bool list pointer is null." );
323351 return *(payload.copyable_union .as_bool_list_ptr );
324352 }
325353
326354 /* ***** Double List Type ******/
327355 /* implicit*/ EValue(executorch::aten::ArrayRef<double >* d)
328356 : tag(Tag::ListDouble) {
357+ ET_CHECK_MSG (d != nullptr , " ArrayRef<double> pointer cannot be null" );
329358 payload.copyable_union .as_double_list_ptr = d;
330359 }
331360
@@ -335,12 +364,17 @@ struct EValue {
335364
336365 executorch::aten::ArrayRef<double > toDoubleList () const {
337366 ET_CHECK_MSG (isDoubleList (), " EValue is not a Double List." );
367+ ET_CHECK_MSG (
368+ payload.copyable_union .as_double_list_ptr != nullptr ,
369+ " EValue double list pointer is null." );
338370 return *(payload.copyable_union .as_double_list_ptr );
339371 }
340372
341373 /* ***** Tensor List Type ******/
342374 /* implicit*/ EValue(BoxedEvalueList<executorch::aten::Tensor>* t)
343375 : tag(Tag::ListTensor) {
376+ ET_CHECK_MSG (
377+ t != nullptr , " BoxedEvalueList<Tensor> pointer cannot be null" );
344378 payload.copyable_union .as_tensor_list_ptr = t;
345379 }
346380
@@ -350,13 +384,19 @@ struct EValue {
350384
351385 executorch::aten::ArrayRef<executorch::aten::Tensor> toTensorList () const {
352386 ET_CHECK_MSG (isTensorList (), " EValue is not a Tensor List." );
387+ ET_CHECK_MSG (
388+ payload.copyable_union .as_tensor_list_ptr != nullptr ,
389+ " EValue tensor list pointer is null." );
353390 return payload.copyable_union .as_tensor_list_ptr ->get ();
354391 }
355392
356393 /* ***** List Optional Tensor Type ******/
357394 /* implicit*/ EValue(
358395 BoxedEvalueList<std::optional<executorch::aten::Tensor>>* t)
359396 : tag(Tag::ListOptionalTensor) {
397+ ET_CHECK_MSG (
398+ t != nullptr ,
399+ " BoxedEvalueList<optional<Tensor>> pointer cannot be null" );
360400 payload.copyable_union .as_list_optional_tensor_ptr = t;
361401 }
362402
@@ -366,6 +406,11 @@ struct EValue {
366406
367407 executorch::aten::ArrayRef<std::optional<executorch::aten::Tensor>>
368408 toListOptionalTensor () const {
409+ ET_CHECK_MSG (
410+ isListOptionalTensor (), " EValue is not a List Optional Tensor." );
411+ ET_CHECK_MSG (
412+ payload.copyable_union .as_list_optional_tensor_ptr != nullptr ,
413+ " EValue list optional tensor pointer is null." );
369414 return payload.copyable_union .as_list_optional_tensor_ptr ->get ();
370415 }
371416
@@ -445,11 +490,15 @@ struct EValue {
445490 // minor performance bump for a code maintainability hit
446491 if (isTensor ()) {
447492 payload.as_tensor .~Tensor ();
448- } else if (isTensorList ()) {
493+ } else if (
494+ isTensorList () &&
495+ payload.copyable_union .as_tensor_list_ptr != nullptr ) {
449496 for (auto & tensor : toTensorList ()) {
450497 tensor.~Tensor ();
451498 }
452- } else if (isListOptionalTensor ()) {
499+ } else if (
500+ isListOptionalTensor () &&
501+ payload.copyable_union .as_list_optional_tensor_ptr != nullptr ) {
453502 for (auto & optional_tensor : toListOptionalTensor ()) {
454503 optional_tensor.~optional ();
455504 }
0 commit comments