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

Commita6cdc35

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

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp