From 405a645078afb874b19952b422017ecc74adf1a0 Mon Sep 17 00:00:00 2001 From: zhouhao02 Date: Sun, 8 Oct 2023 09:21:10 +0800 Subject: [PATCH] Fix some SVACE issues related to methods of Marshal. --- .../src/internal/chipmunk/NativeInterop.cs | 5 ++++- .../src/internal/chipmunk/cpSpaceDebugDrawOptions.cs | 11 +++++++++-- src/Tizen.NUI.Physics2D/src/public/chipmunk/Body.cs | 5 +++-- src/Tizen.NUI.Physics2D/src/public/chipmunk/Space.cs | 10 ++++++---- src/Tizen.NUI.Physics2D/src/public/chipmunk/SpaceRef.cs | 10 ++++++---- 5 files changed, 28 insertions(+), 13 deletions(-) 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]; -- 2.7.4