Skip to content

Commit 0e76a97

Browse files
authored
Check the number of inputs in Method::set_inputs (#13341)
1 parent 2e44b3c commit 0e76a97

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

runtime/executor/method.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,13 @@ Method::set_input(const EValue& input_evalue, size_t input_idx) {
11781178
ET_NODISCARD Error
11791179
Method::set_inputs(const executorch::aten::ArrayRef<EValue>& input_evalues) {
11801180
const size_t n_input = inputs_size();
1181+
ET_CHECK_OR_RETURN_ERROR(
1182+
input_evalues.size() == n_input,
1183+
InvalidArgument,
1184+
"Invalid number of inputs provided. Expected %" ET_PRIsize_t
1185+
", but got %" ET_PRIsize_t,
1186+
n_input,
1187+
input_evalues.size());
11811188
for (size_t i = 0; i < n_input; ++i) {
11821189
ET_CHECK_OK_OR_RETURN_ERROR(set_input(input_evalues[i], i));
11831190
}
@@ -1250,20 +1257,17 @@ ET_NODISCARD Error Method::get_outputs(EValue* output_evalues, size_t length) {
12501257
initialized(),
12511258
InvalidState,
12521259
"Outputs can not be retrieved until method has been initialized.");
1253-
1260+
const size_t n_output = outputs_size();
12541261
ET_CHECK_OR_RETURN_ERROR(
1255-
length >= outputs_size(),
1262+
length >= n_output,
12561263
InvalidArgument,
12571264
"The given array is not large enough to hold all outputs.");
1258-
1259-
for (size_t i = 0; i < outputs_size(); i++) {
1265+
for (size_t i = 0; i < n_output; ++i) {
12601266
output_evalues[i] = values_[get_output_index(i)];
12611267
}
1262-
1263-
for (size_t i = outputs_size(); i < length; i++) {
1268+
for (size_t i = n_output; i < length; ++i) {
12641269
output_evalues[i] = EValue();
12651270
}
1266-
12671271
return Error::Ok;
12681272
}
12691273

0 commit comments

Comments
 (0)