@@ -146,30 +146,33 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
146
146
147
147
// Constant data may live inside the flatbuffer data (constant_buffer) or in a
148
148
// separate segment (constant_segment). It should not be in both.
149
- const auto & constant_buffer = flatbuffer_program->constant_buffer ();
150
- const auto & constant_segment = flatbuffer_program-> constant_segment ();
151
-
152
- // Check if the constant data is inside a separate segment.
153
- if (constant_segment != nullptr && constant_segment-> offsets ()-> size () > 0 ) {
149
+ const auto * constant_segment = flatbuffer_program->constant_segment ();
150
+ if ( constant_segment != nullptr && constant_segment-> offsets () != nullptr &&
151
+ constant_segment-> offsets ()-> size () > 0 ) {
152
+ // The constant data is inside a separate segment.
153
+ const auto * constant_buffer = flatbuffer_program-> constant_buffer ();
154
154
ET_CHECK_OR_RETURN_ERROR (
155
- constant_buffer->size () == 0 ,
156
- InvalidState,
157
- " constant_buffer contains %u items, constant_segment.offsets contains %u items. Only one should be used." ,
155
+ constant_buffer == nullptr || constant_buffer->size () == 0 ,
156
+ InvalidProgram,
157
+ " constant_buffer contains %u items, "
158
+ " constant_segment.offsets contains %u items. Only one should be used." ,
158
159
constant_buffer->size (),
159
160
constant_segment->offsets ()->size ());
161
+ const auto * segments = flatbuffer_program->segments ();
162
+ ET_CHECK_OR_RETURN_ERROR (
163
+ segments != nullptr , InvalidProgram, " No segments in program" );
160
164
161
165
// Load constant segment.
162
166
// TODO(T171839323): Add test for segment_index > num available segments.
163
167
ET_CHECK_OR_RETURN_ERROR (
164
- constant_segment->segment_index () <
165
- flatbuffer_program->segments ()->size (),
166
- InvalidArgument,
168
+ constant_segment->segment_index () < segments->size (),
169
+ InvalidProgram,
167
170
" Constant segment index %d invalid for program segments range %d" ,
168
171
constant_segment->segment_index (),
169
- flatbuffer_program-> segments () ->size ());
172
+ segments->size ());
170
173
171
174
const executorch_flatbuffer::DataSegment* data_segment =
172
- flatbuffer_program-> segments () ->Get (constant_segment->segment_index ());
175
+ segments->Get (constant_segment->segment_index ());
173
176
Result<FreeableBuffer> constant_segment_data = loader->Load (
174
177
segment_base_offset + data_segment->offset (), data_segment->size ());
175
178
if (!constant_segment_data.ok ()) {
@@ -199,7 +202,12 @@ Result<executorch_flatbuffer::ExecutionPlan*> get_execution_plan(
199
202
size_t Program::num_methods () const {
200
203
auto internal_program =
201
204
static_cast <const executorch_flatbuffer::Program*>(internal_program_);
202
- return internal_program->execution_plan ()->size ();
205
+ const auto execution_plan = internal_program->execution_plan ();
206
+ if (execution_plan != nullptr ) {
207
+ return execution_plan->size ();
208
+ } else {
209
+ return 0 ;
210
+ }
203
211
}
204
212
205
213
Result<const char *> Program::get_method_name (size_t plan_index) const {
0 commit comments