From: zhouhao02 Date: Sun, 8 Oct 2023 01:21:10 +0000 (+0800) Subject: Fix some SVACE issues related to methods of Marshal. X-Git-Tag: accepted/tizen/8.0/unified/20240613.065534~186 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=405a645078afb874b19952b422017ecc74adf1a0;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git Fix some SVACE issues related to methods of Marshal. --- diff --git a/src/Tizen.NUI.Physics2D/src/internal/chipmunk/NativeInterop.cs b/src/Tizen.NUI.Physics2D/src/internal/chipmunk/NativeInterop.cs index 5859c0b..a86f6bf 100644 --- a/src/Tizen.NUI.Physics2D/src/internal/chipmunk/NativeInterop.cs +++ b/src/Tizen.NUI.Physics2D/src/internal/chipmunk/NativeInterop.cs @@ -111,7 +111,10 @@ namespace Tizen.NUI.Physics2D.Chipmunk internal static IntPtr StructureArrayToPtr(IReadOnlyList items) { var size = SizeOf(); - var memory = Marshal.AllocHGlobal(size * items.Count); + int allocBytes = checked(size * items.Count); + Debug.Assert(allocBytes > 0, "The memory to be allocated should be greater than 0"); + + var memory = Marshal.AllocHGlobal(allocBytes); for (var i = 0; i < items.Count; i++) { diff --git a/src/Tizen.NUI.Physics2D/src/internal/chipmunk/cpSpaceDebugDrawOptions.cs b/src/Tizen.NUI.Physics2D/src/internal/chipmunk/cpSpaceDebugDrawOptions.cs index 18e7fd7..d787f6e 100644 --- a/src/Tizen.NUI.Physics2D/src/internal/chipmunk/cpSpaceDebugDrawOptions.cs +++ b/src/Tizen.NUI.Physics2D/src/internal/chipmunk/cpSpaceDebugDrawOptions.cs @@ -104,8 +104,15 @@ namespace Tizen.NUI.Physics2D.Chipmunk private IntPtr ToPointer() { IntPtr drawOptionsPtr = NativeInterop.AllocStructure(); - - Marshal.StructureToPtr(this, drawOptionsPtr, false); + try + { + Marshal.StructureToPtr(this, drawOptionsPtr, false); + } + catch (Exception exception) + { + Tizen.Log.Fatal("NUI", "[Error] got exception during Marshal.StructureToPtr, this should not occur, message : " + exception.Message); + } + return drawOptionsPtr; } diff --git a/src/Tizen.NUI.Physics2D/src/public/chipmunk/Body.cs b/src/Tizen.NUI.Physics2D/src/public/chipmunk/Body.cs index 647c0ef..31362da 100644 --- a/src/Tizen.NUI.Physics2D/src/public/chipmunk/Body.cs +++ b/src/Tizen.NUI.Physics2D/src/public/chipmunk/Body.cs @@ -728,11 +728,12 @@ namespace Tizen.NUI.Physics2D.Chipmunk get { int count = NativeMethods.cpBodyGetContactedBodiesCount(body); + int intptrBytes = checked(IntPtr.Size * count); - if (count == 0) + if (intptrBytes == 0) return Array.Empty(); - IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count); + IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes); NativeMethods.cpBodyGetUserDataContactedBodies(body, ptrBodies); IntPtr[] userDataArray = new IntPtr[count]; diff --git a/src/Tizen.NUI.Physics2D/src/public/chipmunk/Space.cs b/src/Tizen.NUI.Physics2D/src/public/chipmunk/Space.cs index db27ebd..3013687 100755 --- a/src/Tizen.NUI.Physics2D/src/public/chipmunk/Space.cs +++ b/src/Tizen.NUI.Physics2D/src/public/chipmunk/Space.cs @@ -607,11 +607,12 @@ namespace Tizen.NUI.Physics2D.Chipmunk get { int count = NativeMethods.cpSpaceGetBodyCount(space); + int intptrBytes = checked(IntPtr.Size * count); - if (count == 0) + if (intptrBytes == 0) return Array.Empty(); - IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count); + IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes); NativeMethods.cpSpaceGetBodiesUserDataArray(space, ptrBodies); IntPtr[] userDataArray = new IntPtr[count]; @@ -641,11 +642,12 @@ namespace Tizen.NUI.Physics2D.Chipmunk get { int count = NativeMethods.cpSpaceGetDynamicBodyCount(space); + int intptrBytes = checked(IntPtr.Size * count); - if (count == 0) + if (intptrBytes == 0) return Array.Empty(); - IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count); + IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes); NativeMethods.cpSpaceGetDynamicBodiesUserDataArray(space, ptrBodies); IntPtr[] userDataArray = new IntPtr[count]; diff --git a/src/Tizen.NUI.Physics2D/src/public/chipmunk/SpaceRef.cs b/src/Tizen.NUI.Physics2D/src/public/chipmunk/SpaceRef.cs index 9313ff1..926af93 100644 --- a/src/Tizen.NUI.Physics2D/src/public/chipmunk/SpaceRef.cs +++ b/src/Tizen.NUI.Physics2D/src/public/chipmunk/SpaceRef.cs @@ -526,11 +526,12 @@ namespace Tizen.NUI.Physics2D.Chipmunk get { int count = NativeMethods.cpSpaceGetBodyCount(space); + int intptrBytes = checked(IntPtr.Size * count); - if (count == 0) + if (intptrBytes == 0) return Array.Empty(); - IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count); + IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes); NativeMethods.cpSpaceGetBodiesUserDataArray(space, ptrBodies); IntPtr[] userDataArray = new IntPtr[count]; @@ -560,11 +561,12 @@ namespace Tizen.NUI.Physics2D.Chipmunk get { int count = NativeMethods.cpSpaceGetDynamicBodyCount(space); + int intptrBytes = checked(IntPtr.Size * count); - if (count == 0) + if (intptrBytes == 0) return Array.Empty(); - IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count); + IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes); NativeMethods.cpSpaceGetDynamicBodiesUserDataArray(space, ptrBodies); IntPtr[] userDataArray = new IntPtr[count];