@@ -40,11 +40,11 @@ impl std::fmt::Display for ParseError {
40
40
41
41
impl std:: error:: Error for ParseError { }
42
42
43
- fn sindex_to_uindex ( index : isize , len : usize ) -> usize {
43
+ fn sindex_to_uindex ( index : isize , len : usize ) -> Option < usize > {
44
44
if index >= 0 {
45
- index as usize
45
+ Some ( index as usize )
46
46
} else {
47
- len - index. unsigned_abs ( )
47
+ len. checked_sub ( index. unsigned_abs ( ) )
48
48
}
49
49
}
50
50
@@ -80,7 +80,7 @@ impl Expression {
80
80
Self :: Subscript ( expr, index) => match expr. get ( root) {
81
81
Some ( value) => match value. kind {
82
82
ValueKind :: Array ( ref array) => {
83
- let index = sindex_to_uindex ( index, array. len ( ) ) ;
83
+ let index = sindex_to_uindex ( index, array. len ( ) ) ? ;
84
84
85
85
if index >= array. len ( ) {
86
86
None
@@ -141,7 +141,7 @@ impl Expression {
141
141
142
142
match value. kind {
143
143
ValueKind :: Array ( ref mut array) => {
144
- let index = sindex_to_uindex ( index, array. len ( ) ) ;
144
+ let index = sindex_to_uindex ( index, array. len ( ) ) ? ;
145
145
146
146
if index >= array. len ( ) {
147
147
array. resize ( index + 1 , Value :: new ( None , ValueKind :: Nil ) ) ;
@@ -216,7 +216,7 @@ impl Expression {
216
216
}
217
217
218
218
if let ValueKind :: Array ( ref mut array) = parent. kind {
219
- let uindex = sindex_to_uindex ( index, array. len ( ) ) ;
219
+ let uindex = sindex_to_uindex ( index, array. len ( ) ) . unwrap ( ) ;
220
220
if uindex >= array. len ( ) {
221
221
array. resize ( uindex + 1 , Value :: new ( None , ValueKind :: Nil ) ) ;
222
222
}
0 commit comments