8000 Check (U)IntPtr size explicitly · pythonnet/pythonnet@43f748b · GitHub
[go: up one dir, main page]

Skip to content

Commit 43f748b

Browse files
committed
Check (U)IntPtr size explicitly
1 parent 775efd5 commit 43f748b

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/runtime/Runtime.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ private static string GetDefaultDllName(Version version)
5959
internal static bool TypeManagerInitialized => _typesInitialized;
6060
internal static readonly bool Is32Bit = IntPtr.Size == 4;
6161

62+
// Available in newer .NET Core versions (>= 5) as IntPtr.MaxValue etc.
63+
internal static readonly long IntPtrMaxValue = Is32Bit ? Int32.MaxValue : Int64.MaxValue;
64+
internal static readonly long IntPtrMinValue = Is32Bit ? Int32.MinValue : Int64.MinValue;
65+
internal static readonly ulong UIntPtrMaxValue = Is32Bit ? UInt32.MaxValue : UInt64.MaxValue;
66+
6267
// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
6368
internal static bool IsWindows = Environment.OSVersion.Platform == PlatformID.Win32NT;
6469

src/runtime/Types/ClassObject.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,15 @@ private static NewReference NewPrimitive(BorrowedReference tp, BorrowedReference
173173
else if (Runtime.PyInt_Check(op))
174174
{
175175
long? num = Runtime.PyLong_AsLongLong(op);
176-
if (num is long n)
176+
if (num is long n && n >= Runtime.IntPtrMinValue && n <= Runtime.IntPtrMaxValue)
177177
{
178178
result = new IntPtr(n);
179179
}
180+
else
181+
{
182+
Exceptions.SetError(Exceptions.OverflowError, "value not in range for IntPtr");
183+
return default;
184+
}
180185
}
181186
}
182187

@@ -200,10 +205,15 @@ private static NewReference NewPrimitive(BorrowedReference tp, BorrowedReference
200205
else if (Runtime.PyInt_Check(op))
201206
{
202207
ulong? num = Runtime.PyLong_AsUnsignedLongLong(op);
203-
if (num is ulong n)
208+
if (num is ulong n && n < Runtime.UIntPtrMaxValue)
204209
{
205210
result = new UIntPtr(n);
206211
}
212+
else
213+
{
214+
Exceptions.SetError(Exceptions.OverflowError, "value not in range for UIntPtr");
215+
return default;
216+
}
207217
}
208218
}
209219

tests/test_conversion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ def test_intptr_construction():
701701
assert ob.IntPtrField.ToInt64() == v
702702

703703
for v in [min_intptr - 1, max_intptr + 1]:
704-
with pytest.raises(TypeError):
704+
with pytest.raises(OverflowError):
705705
IntPtr(v)
706706

707707
for v in [0, 1024, min_uintptr, max_uintptr, max_intptr]:
@@ -710,6 +710,6 @@ def test_intptr_construction():
710710
assert ob.UIntPtrField.ToUInt64() == v
711711

712712
for v in [min_uintptr - 1, max_uintptr + 1, min_intptr]:
713-
with pytest.raises(TypeError):
713+
with pytest.raises(OverflowError):
714714
UIntPtr(v)
715715

0 commit comments

Comments
 (0)
0