Skip to content

Commit 610c421

Browse files
committed
refactor: use non-panicking array access
1 parent 7806db3 commit 610c421

File tree

1 file changed

+62
-13
lines changed

1 file changed

+62
-13
lines changed

clarity/src/vm/analysis/type_checker/v2_1/natives/post_conditions.rs

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,20 @@ pub fn check_restrict_assets(
3939
) -> Result<TypeSignature, CheckError> {
4040
check_arguments_at_least(3, args)?;
4141

42-
let asset_owner = &args[0];
43-
let allowance_list = args[1]
42+
let asset_owner = args
43+
.first()
44+
.ok_or(CheckErrors::CheckerImplementationFailure)?;
45+
let allowance_list = args
46+
.get(1)
47+
.ok_or(CheckErrors::CheckerImplementationFailure)?
4448
.match_list()
4549
.ok_or(CheckErrors::ExpectedListOfAllowances(
4650
"restrict-assets?".into(),
4751
2,
4852
))?;
49-
let body_exprs = &args[2..];
53+
let body_exprs = args
54+
.get(2..)
55+
.ok_or(CheckErrors::CheckerImplementationFailure)?;
5056

5157
if allowance_list.len() > MAX_ALLOWANCES {
5258
return Err(CheckErrors::TooManyAllowances(MAX_ALLOWANCES, allowance_list.len()).into());
@@ -90,13 +96,17 @@ pub fn check_as_contract(
9096
) -> Result<TypeSignature, CheckError> {
9197
check_arguments_at_least(2, args)?;
9298

93-
let allowance_list = args[0]
99+
let allowance_list = args
100+
.first()
101+
.ok_or(CheckErrors::CheckerImplementationFailure)?
94102
.match_list()
95103
.ok_or(CheckErrors::ExpectedListOfAllowances(
96104
"as-contract?".into(),
97105
1,
98106
))?;
99-
let body_exprs = &args[1..];
107+
let body_exprs = args
108+
.get(1..)
109+
.ok_or(CheckErrors::CheckerImplementationFailure)?;
100110

101111
if allowance_list.len() > MAX_ALLOWANCES {
102112
return Err(CheckErrors::TooManyAllowances(MAX_ALLOWANCES, allowance_list.len()).into());
@@ -185,7 +195,12 @@ fn check_allowance_with_stx(
185195
) -> Result<bool, CheckError> {
186196
check_argument_count(1, args)?;
187197

188-
checker.type_check_expects(&args[0], context, &TypeSignature::UIntType)?;
198+
checker.type_check_expects(
199+
args.first()
200+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
201+
context,
202+
&TypeSignature::UIntType,
203+
)?;
189204

190205
Ok(false)
191206
}
@@ -199,9 +214,24 @@ fn check_allowance_with_ft(
199214
) -> Result<bool, CheckError> {
200215
check_argument_count(3, args)?;
201216

202-
checker.type_check_expects(&args[0], context, &TypeSignature::PrincipalType)?;
203-
checker.type_check_expects(&args[1], context, &ASCII_128)?;
204-
checker.type_check_expects(&args[2], context, &TypeSignature::UIntType)?;
217+
checker.type_check_expects(
218+
args.first()
219+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
220+
context,
221+
&TypeSignature::PrincipalType,
222+
)?;
223+
checker.type_check_expects(
224+
args.get(1)
225+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
226+
context,
227+
&ASCII_128,
228+
)?;
229+
checker.type_check_expects(
230+
args.get(2)
231+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
232+
context,
233+
&TypeSignature::UIntType,
234+
)?;
205235

206236
Ok(false)
207237
}
@@ -215,11 +245,25 @@ fn check_allowance_with_nft(
215245
) -> Result<bool, CheckError> {
216246
check_argument_count(3, args)?;
217247

218-
checker.type_check_expects(&args[0], context, &TypeSignature::PrincipalType)?;
219-
checker.type_check_expects(&args[1], context, &ASCII_128)?;
248+
checker.type_check_expects(
249+
args.first()
250+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
251+
context,
252+
&TypeSignature::PrincipalType,
253+
)?;
254+
checker.type_check_expects(
255+
args.get(1)
256+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
257+
context,
258+
&ASCII_128,
259+
)?;
220260

221261
// Asset identifiers must be a Clarity list with any type of elements
222-
let id_list_ty = checker.type_check(&args[2], context)?;
262+
let id_list_ty = checker.type_check(
263+
args.get(2)
264+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
265+
context,
266+
)?;
223267
let TypeSignature::SequenceType(SequenceSubtype::ListType(list_data)) = id_list_ty else {
224268
return Err(CheckErrors::WithNftExpectedListOfIdentifiers.into());
225269
};
@@ -243,7 +287,12 @@ fn check_allowance_with_stacking(
243287
) -> Result<bool, CheckError> {
244288
check_argument_count(1, args)?;
245289

246-
checker.type_check_expects(&args[0], context, &TypeSignature::UIntType)?;
290+
checker.type_check_expects(
291+
args.first()
292+
.ok_or(CheckErrors::CheckerImplementationFailure)?,
293+
context,
294+
&TypeSignature::UIntType,
295+
)?;
247296

248297
Ok(false)
249298
}

0 commit comments

Comments
 (0)