[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 5859c0b618a31cd500cfeb0777f6550d7095556c..a86f6bf4ce6d2159569427c59d2d4a735abc376a 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 18e7fd709907d5fd64932f6e5b497dab451c1b8f..d787f6e8104bd6a74d8099e887e04fa9ea38f9ab 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 647c0ef5746b1c29a13096790506a5a3f13b2e1a..31362da5afc4dd6f5d2d01a4450b38c50b47b350 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 db27ebd845b3e8a9f5f6a5489d67be0c8e9f1421..3013687780baae6b3f4f5cb72511765ae3cd3bc7 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 9313ff15e2feff89df8d45d8545ec7a43d68082f..926af934ee9d5c2a5d45efdb9dc99aad94c7bb3e 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];