From: zhouhao02 Date: Sun, 8 Oct 2023 01:21:10 +0000 (+0800) Subject: [NUI] Fix some SVACE issues related to methods of Marshal. X-Git-Tag: submit/tizen/20231025.151412~1^2~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f510b0a96fd0bcb01ebda04ffb3374d16d8f6990;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [NUI] 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 5859c0b61..a86f6bf4c 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 18e7fd709..d787f6e81 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 647c0ef57..31362da5a 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 db27ebd84..301368778 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 9313ff15e..926af934e 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];