[NUI] Fix some SVACE issues related to methods of Marshal.
authorzhouhao02 <haozhou.zhou@samsung.com>
Sun, 8 Oct 2023 01:21:10 +0000 (09:21 +0800)
committerEunki Hong <h.pichulia@gmail.com>
Wed, 25 Oct 2023 12:48:44 +0000 (21:48 +0900)
src/Tizen.NUI.Physics2D/src/internal/chipmunk/NativeInterop.cs
src/Tizen.NUI.Physics2D/src/internal/chipmunk/cpSpaceDebugDrawOptions.cs
src/Tizen.NUI.Physics2D/src/public/chipmunk/Body.cs
src/Tizen.NUI.Physics2D/src/public/chipmunk/Space.cs
src/Tizen.NUI.Physics2D/src/public/chipmunk/SpaceRef.cs

index 5859c0b..a86f6bf 100644 (file)
@@ -111,7 +111,10 @@ namespace Tizen.NUI.Physics2D.Chipmunk
         internal static IntPtr StructureArrayToPtr<T>(IReadOnlyList<T> items)
         {
             var size = SizeOf<T>();
-            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++)
             {
index 18e7fd7..d787f6e 100644 (file)
@@ -104,8 +104,15 @@ namespace Tizen.NUI.Physics2D.Chipmunk
         private IntPtr ToPointer()
         {
             IntPtr drawOptionsPtr = NativeInterop.AllocStructure<cpSpaceDebugDrawOptions>();
-
-            Marshal.StructureToPtr<cpSpaceDebugDrawOptions>(this, drawOptionsPtr, false);
+            try
+            {
+                Marshal.StructureToPtr<cpSpaceDebugDrawOptions>(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;
         }
 
index 647c0ef..31362da 100644 (file)
@@ -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<Body>();
 
-                IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count);
+                IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes);
                 NativeMethods.cpBodyGetUserDataContactedBodies(body, ptrBodies);
 
                 IntPtr[] userDataArray = new IntPtr[count];
index db27ebd..3013687 100755 (executable)
@@ -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<Body>();
 
-                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<Body>();
 
-                IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count);
+                IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes);
                 NativeMethods.cpSpaceGetDynamicBodiesUserDataArray(space, ptrBodies);
 
                 IntPtr[] userDataArray = new IntPtr[count];
index 9313ff1..926af93 100644 (file)
@@ -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<Body>();
 
-                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<Body>();
 
-                IntPtr ptrBodies = Marshal.AllocHGlobal(IntPtr.Size * count);
+                IntPtr ptrBodies = Marshal.AllocHGlobal(intptrBytes);
                 NativeMethods.cpSpaceGetDynamicBodiesUserDataArray(space, ptrBodies);
 
                 IntPtr[] userDataArray = new IntPtr[count];