Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1bc7dc4

Browse files
committed
fixed conversion to array crashing when sequence object does not implement PySequence_Size
1 parent b948f58 commit 1bc7dc4

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/runtime/converter.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,22 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
884884
result = null;
885885

886886
bool IsSeqObj = Runtime.PySequence_Check(value);
887-
var len = IsSeqObj ? Runtime.PySequence_Size(value) : -1;
887+
int len = -1;
888+
if (IsSeqObj)
889+
{
890+
long longLen = Runtime.PySequence_Size(value);
891+
if (longLen > int.MaxValue)
892+
{
893+
if (setError)
894+
{
895+
Exceptions.SetError(Exceptions.NotImplementedError, ".NET does not support lists with over 2^31 elements");
896+
}
897+
898+
return false;
899+
}
900+
901+
len = longLen < 0 ? -1 : checked((int)longLen);
902+
}
888903

889904
IntPtr IterObject = Runtime.PyObject_GetIter(value);
890905

@@ -900,7 +915,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
900915

901916
var listType = typeof(List<>);
902917
var constructedListType = listType.MakeGenericType(elementType);
903-
IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) :
918+
IList list = len > 0 ? (IList) Activator.CreateInstance(constructedListType, new Object[] {len}) :
904919
(IList) Activator.CreateInstance(constructedListType);
905920
IntPtr item;
906921

0 commit comments

Comments
 (0)