Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43f748b

Browse files
committed
Check (U)IntPtr size explicitly
1 parent775efd5 commit43f748b

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
internalstaticboolTypeManagerInitialized=>_typesInitialized;
6060
internalstaticreadonlyboolIs32Bit=IntPtr.Size==4;
6161

62+
// Available in newer .NET Core versions (>= 5) as IntPtr.MaxValue etc.
63+
internalstaticreadonlylongIntPtrMaxValue=Is32Bit?Int32.MaxValue:Int64.MaxValue;
64+
internalstaticreadonlylongIntPtrMinValue=Is32Bit?Int32.MinValue:Int64.MinValue;
65+
internalstaticreadonlyulongUIntPtrMaxValue=Is32Bit?UInt32.MaxValue:UInt64.MaxValue;
66+
6267
// .NET core: System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
6368
internalstaticboolIsWindows=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
elseif(Runtime.PyInt_Check(op))
174174
{
175175
long?num=Runtime.PyLong_AsLongLong(op);
176-
if(numislongn)
176+
if(numislongn&&n>=Runtime.IntPtrMinValue&&n<=Runtime.IntPtrMaxValue)
177177
{
178178
result=newIntPtr(n);
179179
}
180+
else
181+
{
182+
Exceptions.SetError(Exceptions.OverflowError,"value not in range for IntPtr");
183+
returndefault;
184+
}
180185
}
181186
}
182187

@@ -200,10 +205,15 @@ private static NewReference NewPrimitive(BorrowedReference tp, BorrowedReference
200205
elseif(Runtime.PyInt_Check(op))
201206
{
202207
ulong?num=Runtime.PyLong_AsUnsignedLongLong(op);
203-
if(numisulongn)
208+
if(numisulongn&&n<Runtime.UIntPtrMaxValue)
204209
{
205210
result=newUIntPtr(n);
206211
}
212+
else
213+
{
214+
Exceptions.SetError(Exceptions.OverflowError,"value not in range for UIntPtr");
215+
returndefault;
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
assertob.IntPtrField.ToInt64()==v
702702

703703
forvin [min_intptr-1,max_intptr+1]:
704-
withpytest.raises(TypeError):
704+
withpytest.raises(OverflowError):
705705
IntPtr(v)
706706

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

712712
forvin [min_uintptr-1,max_uintptr+1,min_intptr]:
713-
withpytest.raises(TypeError):
713+
withpytest.raises(OverflowError):
714714
UIntPtr(v)
715715

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp