IOT-2539 Fixed incompatible-pointer-types warning 05/22205/3
authorGeorge Nash <george.nash@intel.com>
Tue, 15 Aug 2017 19:50:20 +0000 (12:50 -0700)
committerRick Bell <richard.s.bell@intel.com>
Mon, 18 Sep 2017 17:38:45 +0000 (17:38 +0000)
The function GetResourceFromHandle was returning a PIResource**. When
the actual code was inspected it was returning a PIResourceBase** that
was being cast to a PIResource**. That value was then used as a
PIResource**. The only reason this code worked is because the way C
lays out structs in memory is sequential Since the first element of the
PIResoruceBase is a PIResource treating the the PIResourceBase like
a PIResource worked only by virtue of the memory layout rules for C
structs.

Later in the zigbee_wapper.c it relies again on the memory layout
rules for structs to assign PIResource** to a PIResource_Zigbee**.

This relies too much on internal knowledge of memory layout and is
a potential management problem. The code tied 3 structs together
based on there memory layout.

To fix the issue. The GetResourceFromHandle now returns PIResourceBase**
which was the actual type returned. In the cleanup code the pointer
is change from PIResource* to PIResourceBase*. Now when the value is
passed into the DeleteResource function it no longer generates the
incompatible-pointer-types warning.

In zigbee_wrapper.c we no longer cast the PIResource_Zigbee* to
PIResource* we now pass in the part of the PIResource_Zigbee that is
actually the PIResourceBase.

Bug: https://jira.iotivity.org/browse/IOT-2539
Change-Id: I6b147a52c522d036d016e2cd15e2900e6fc06249
Signed-off-by: George Nash <george.nash@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/21927
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Mats Wichmann <mats@linux.com>
Reviewed-by: Dan Mihai <Daniel.Mihai@microsoft.com>
Reviewed-by: Larry Sachs <larry.j.sachs@intel.com>
Reviewed-by: Rick Bell <richard.s.bell@intel.com>
(cherry picked from commit 25898d822bea23ec7f19b6858026de493031c70e)

plugins/include/internal/pluginlist.h
plugins/src/plugininterface.c
plugins/src/pluginlist.c
plugins/zigbee_wrapper/src/zigbee_wrapper.c

index 925e24a..1e089b9 100644 (file)
@@ -39,11 +39,11 @@ OCStackResult DeletePlugin(PIPluginBase * plugin);
 
 OCStackResult DeletePluginList();
 
-OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResource,
+OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResourceBase ** piResourceBase,
                                     OCResourceHandle * resourceHandle);
 
 OCStackResult GetResourceFromZigBeeNodeId(PIPluginBase * plugin,
-                                          PIResource_Zigbee ** piResource,
+                                          PIResource_Zigbee ** piResourceBase,
                                           const char * nodeId,
                                           const char * endpointId,
                                           const char * clusterId);
index 483a3a5..1199441 100644 (file)
@@ -95,10 +95,10 @@ OCEntityHandlerResult PluginInterfaceEntityHandler(OCEntityHandlerFlag flag,
     else
     {
         OIC_LOG_V(ERROR, TAG, "Error handling request %u", ehResult);
-        PIResource * piResource = NULL;
-        result = GetResourceFromHandle(plugin, &piResource, response->resourceHandle);
-        OIC_LOG_V(ERROR, TAG, "Deleting resource \"%s\" because of failed request.", piResource->uri);
-        result = DeleteResource(plugin, piResource);
+        PIResourceBase * piResourceBase = NULL;
+        result = GetResourceFromHandle(plugin, &piResourceBase, response->resourceHandle);
+        OIC_LOG_V(ERROR, TAG, "Deleting resource \"%s\" because of failed request.", piResourceBase->piResource.uri);
+        result = DeleteResource(plugin, piResourceBase);
         if(result != OC_STACK_OK)
         {
             OIC_LOG_V(ERROR, TAG, "Failed to delete resource after failed request.");
index 2b5b661..ed2becc 100644 (file)
@@ -82,10 +82,10 @@ OCStackResult DeletePluginList()
     return result;
 }
 
-OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResource,
+OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResourceBase **piResourceBase,
                                     OCResourceHandle * resourceHandle)
 {
-    if (!plugin || !resourceHandle || !piResource)
+    if (!plugin || !resourceHandle || !piResourceBase)
     {
         return OC_STACK_INVALID_PARAM;
     }
@@ -95,7 +95,7 @@ OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResou
     {
         if (out->piResource.resourceHandle == resourceHandle)
         {
-            *piResource = (PIResource *) out;
+            *piResourceBase = out;
             return OC_STACK_OK;
         }
     }
index aed5e3c..279a9ba 100644 (file)
@@ -820,10 +820,11 @@ OCEntityHandlerResult processGetRequest(PIPluginBase * plugin,
     uint32_t attributeListIndex = 0;
     OCStackResult stackResult = OC_STACK_OK;
     PIResource_Zigbee * piResource = NULL;
+    PIResourceBase * piResourceBase = &piResource->header;
 
     AttributeList attributeList = { 0, (CIECommandMask) 0,
         .list[0] = { NULL, NULL, OIC_ATTR_NULL, ZB_NULL, { .i = 0 } } };
-    stackResult = GetResourceFromHandle(plugin, (PIResource**) (&piResource),
+    stackResult = GetResourceFromHandle(plugin, &piResourceBase,
                         ehRequest->resource);
     if (stackResult != OC_STACK_OK)
     {
@@ -1000,6 +1001,7 @@ OCEntityHandlerResult processPutRequest(PIPluginBase * plugin,
     }
     OCStackResult stackResult = OC_STACK_OK;
     PIResource_Zigbee *piResource = NULL;
+    PIResourceBase * piResourceBase = &piResource->header;
     AttributeList attributeList = {
         0,
         (CIECommandMask) 0,
@@ -1007,7 +1009,7 @@ OCEntityHandlerResult processPutRequest(PIPluginBase * plugin,
     };
 
     stackResult = GetResourceFromHandle(plugin,
-                                        ((PIResource **) (&piResource)),
+                                        &piResourceBase,
                                         ehRequest->resource);
     if (stackResult != OC_STACK_OK)
     {