@@ -99,6 +99,15 @@ static NewReference CreateMultidimensional(Type elementType, long[] dimensions,
9999
100100 static NewReference NewInstance ( Type elementType , BorrowedReference arrayPyType , long [ ] dimensions )
101101 {
102+ for ( int dim = 0 ; dim < dimensions . Length ; dim ++ )
103+ {
104+ if ( dimensions [ dim ] < 0 )
105+ {
106+ Exceptions . SetError ( Exceptions . ValueError , $ "Non-negative number required (dims[{ dim } ])") ;
107+ return default ;
108+ }
109+ }
110+
102111 object result ;
103112 try
104113 {
@@ -142,7 +151,7 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
142151 var items = ( Array ) obj . inst ;
143152 Type itemType = arrObj . type . Value . GetElementType ( ) ;
144153 int rank = items . Rank ;
145- nint index ;
154+ long index ;
146155 object value ;
147156
148157 // Note that CLR 1.0 only supports int indexes - methods to
@@ -169,19 +178,17 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
169178
170179 if ( index < 0 )
171180 {
172- index = items . Length + index ;
181+ index = items . LongLength + index ;
173182 }
174183
175- try
176- {
177- value = items . GetValue ( index ) ;
178- }
179- catch ( IndexOutOfRangeException )
184+ if ( index < 0 || index >= items . LongLength )
180185 {
181186 Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
182187 return default ;
183188 }
184189
190+ value = items . GetValue ( index ) ;
191+
185192 return Converter . ToPython ( value , itemType ) ;
186193 }
187194
@@ -211,23 +218,23 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
211218 return Exceptions . RaiseTypeError ( "invalid index value" ) ;
212219 }
213220
221+ long len = items . GetLongLength ( dimension ) ;
222+
214223 if ( index < 0 )
215224 {
216- index = items . GetLength ( dimension ) + index ;
225+ index = len + index ;
226+ }
227+
228+ if ( index < 0 || index >= len )
229+ {
230+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
231+ return default ;
217232 }
218233
219234 indices [ dimension ] = index ;
220235 }
221236
222- try
223- {
224- value = items . GetValue ( indices ) ;
225- }
226- catch ( IndexOutOfRangeException )
227- {
228- Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
229- return default ;
230- }
237+ value = items . GetValue ( indices ) ;
231238
232239 return Converter . ToPython ( value , itemType ) ;
233240 }
@@ -242,7 +249,7 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
242249 var items = ( Array ) obj . inst ;
243250 Type itemType = obj . inst . GetType ( ) . GetElementType ( ) ;
244251 int rank = items . Rank ;
245- nint index ;
252+ long index ;
246253 object ? value ;
247254
248255 if ( items . IsReadOnly )
@@ -273,19 +280,16 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
273280
274281 if ( index < 0 )
275282 {
276- index = items . Length + index ;
283+ index = items . LongLength + index ;
277284 }
278285
279- try
280- {
281- items . SetValue ( value , index ) ;
282- }
283- catch ( IndexOutOfRangeException )
286+ if ( index < 0 || index >= items . LongLength )
284287 {
285288 Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
286289 return - 1 ;
287290 }
288291
292+ items . SetValue ( value , index ) ;
289293 return 0 ;
290294 }
291295
@@ -314,23 +318,23 @@ static NewReference NewInstance(Type elementType, BorrowedReference arrayPyType,
314318 return - 1 ;
315319 }
316320
321+ long len = items . GetLongLength ( dimension ) ;
322+
317323 if ( index < 0 )
318324 {
319- index = items . GetLength ( dimension ) + index ;
325+ index = len + index ;
326+ }
327+
328+ if ( index < 0 || index >= len )
329+ {
330+ Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
331+ return - 1 ;
320332 }
321333
322334 indices [ dimension ] = index ;
323335 }
324336
325- try
326- {
327- items . SetValue ( value , indices ) ;
328- }
329- catch ( IndexOutOfRangeException )
330- {
331- Exceptions . SetError ( Exceptions . IndexError , "array index out of range" ) ;
332- return - 1 ;
333- }
337+ items . SetValue ( value , indices ) ;
334338
335339 return 0 ;
336340 }
0 commit comments