heaps: Avoid possible divide by zero error
authorSam Nelson <sam.nelson@ti.com>
Tue, 8 Oct 2019 04:48:04 +0000 (00:48 -0400)
committerSam Nelson <sam.nelson@ti.com>
Tue, 8 Oct 2019 19:51:06 +0000 (15:51 -0400)
Signed-off-by: Sam Nelson <sam.nelson@ti.com>
packages/ti/sdo/ipc/heaps/HeapBufMP.c
packages/ti/sdo/ipc/heaps/HeapMemMP.c
packages/ti/sdo/ipc/heaps/HeapMultiBufMP.c

index a3d12937bb1c55f3c78f897aab84d2b6e9b30f05..81644004acfaf88d79710d768bc09ad438aec225 100644 (file)
@@ -487,11 +487,20 @@ Int ti_sdo_ipc_heaps_HeapBufMP_Instance_init(
         /* Assert that the buffer is in a valid shared region */
         Assert_isTrue(obj->regionId != SharedRegion_INVALIDREGIONID,
                       ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
-
+        /* Additional check to handle case when Assert is disabled */
+        if (obj->regionId == SharedRegion_INVALIDREGIONID) {
+            return(5);
+        }
         /* Assert that sharedAddr is cached aligned if region requires align. */
         Assert_isTrue(((UArg)params->sharedAddr %
                       SharedRegion_getCacheLineSize(obj->regionId) == 0),
                       ti_sdo_ipc_Ipc_A_addrNotCacheAligned);
+        /* Additional check to handle case when Assert is disabled */
+        if (((UArg)params->sharedAddr %
+                      SharedRegion_getCacheLineSize(obj->regionId) != 0)) {
+            return(6);
+        }
+
 
         obj->objType    = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC;
         obj->attrs      = (ti_sdo_ipc_heaps_HeapBufMP_Attrs *)
index 3c188f208b42cd62554f18383826d2dec3aa71b3..924f7873e511e01d2a92736b0961fee724fe2bd2 100644 (file)
@@ -330,6 +330,7 @@ SizeT HeapMemMP_sharedMemReq(const HeapMemMP_Params *params)
 {
     SizeT memReq, minAlign;
     UInt16 regionId;
+    SizeT       cacheLineSize;
 
     /* Ensure that the sharedBufSize param has been set */
     Assert_isTrue(params->sharedBufSize != 0, ti_sdo_ipc_Ipc_A_invParam);
@@ -343,10 +344,14 @@ SizeT HeapMemMP_sharedMemReq(const HeapMemMP_Params *params)
 
     Assert_isTrue(regionId != SharedRegion_INVALIDREGIONID,
             ti_sdo_ipc_Ipc_A_internal);
+    if (regionId != SharedRegion_INVALIDREGIONID) {
+        return 0;
+    }
 
     minAlign = sizeof(ti_sdo_ipc_heaps_HeapMemMP_Header);
-    if (SharedRegion_getCacheLineSize(regionId) > minAlign) {
-        minAlign = SharedRegion_getCacheLineSize(regionId);
+    cacheLineSize = SharedRegion_getCacheLineSize(regionId);
+    if (cacheLineSize > minAlign) {
+        minAlign = cacheLineSize;
     }
 
     /* Add size of HeapBufMP Attrs */
@@ -409,7 +414,7 @@ Int ti_sdo_ipc_heaps_HeapMemMP_Instance_init(
         localAddr = SharedRegion_getPtr(obj->attrs->gateMPAddr);
         if (localAddr == NULL) {
             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
-            return(1);
+            return(4);
         }
 
         status = GateMP_openByAddr(localAddr, (GateMP_Handle *)&(obj->gate));
@@ -445,11 +450,19 @@ Int ti_sdo_ipc_heaps_HeapMemMP_Instance_init(
         /* Assert that the buffer is in a valid shared region */
         Assert_isTrue(obj->regionId != SharedRegion_INVALIDREGIONID,
                       ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
-
+        /* Additional check to handle case when Assert is disabled */
+        if (obj->regionId == SharedRegion_INVALIDREGIONID) {
+            return(5);
+        }
         /* Assert that sharedAddr is cache aligned */
         Assert_isTrue(((UArg)params->sharedAddr %
                       SharedRegion_getCacheLineSize(obj->regionId) == 0),
                       ti_sdo_ipc_Ipc_A_addrNotCacheAligned);
+        /* Additional check to handle case when Assert is disabled */
+        if (((UArg)params->sharedAddr %
+                      SharedRegion_getCacheLineSize(obj->regionId) != 0)) {
+            return(6);
+        }
 
         obj->objType    = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC;
 
index c09bdb0ff3fe04c0b42ba3713f55a4ff592c7e43..190a3abc6099c4fb3c0dcd31c194d42c2aced467 100644 (file)
@@ -462,7 +462,7 @@ Int ti_sdo_ipc_heaps_HeapMultiBufMP_Instance_init(
         localAddr = SharedRegion_getPtr(obj->attrs->gateMPAddr);
         if (localAddr == NULL) {
             Error_raise(eb, ti_sdo_ipc_Ipc_E_internal, 0, 0);
-            return 1;
+            return 4;
         }
         Assert_isTrue(localAddr != NULL, ti_sdo_ipc_Ipc_A_internal);
 
@@ -498,12 +498,19 @@ Int ti_sdo_ipc_heaps_HeapMultiBufMP_Instance_init(
         /* Assert that the buffer is in a valid shared region */
         Assert_isTrue(obj->regionId != SharedRegion_INVALIDREGIONID,
                       ti_sdo_ipc_Ipc_A_addrNotInSharedRegion);
-
+        /* Additional check to handle case when Assert is disabled */
+        if (obj->regionId == SharedRegion_INVALIDREGIONID) {
+            return(5);
+        }
         /* Assert that sharedAddr is cached aligned if region cache aligned */
         Assert_isTrue(((UArg)params->sharedAddr %
                       SharedRegion_getCacheLineSize(obj->regionId) == 0),
                       ti_sdo_ipc_Ipc_A_addrNotCacheAligned);
-
+        /* Additional check to handle case when Assert is disabled */
+        if (((UArg)params->sharedAddr %
+                      SharedRegion_getCacheLineSize(obj->regionId) != 0)) {
+            return(6);
+        }
         obj->objType    = ti_sdo_ipc_Ipc_ObjType_CREATEDYNAMIC;
 
         /* obj->buf will get alignment-adjusted in postInit */