IOT-1583: Fixing /W3 warnings in /resource directory - part 3. 59/19559/22
authorPawel Winogrodzki <pawelwi@microsoft.com>
Wed, 3 May 2017 06:29:30 +0000 (23:29 -0700)
committerMike Fenelon <mike.fenelon@microsoft.com>
Tue, 9 May 2017 21:48:48 +0000 (21:48 +0000)
Fixing all /W3s inside connectivity, routing, security and stack
directories.

Change-Id: I9e99d5f813d6f3f399983759da0b98eeb03c9388
Signed-off-by: Pawel Winogrodzki <pawelwi@microsoft.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/19559
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
Reviewed-by: Alex Kelley <alexke@microsoft.com>
Reviewed-by: Mike Fenelon <mike.fenelon@microsoft.com>
resource/c_common/platform_features.h
resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp
resource/csdk/stack/src/ocstack.c
resource/csdk/stack/test/stacktests.cpp
resource/src/InProcClientWrapper.cpp
resource/src/InProcServerWrapper.cpp
resource/src/OCPlatform_impl.cpp

index a90bc41..79d39b8 100644 (file)
 #  endif
 #  define OC_STATIC_ASSERT(condition, msg) static_assert(condition, msg)
 #else
-#  define OC_STATIC_ASSERT(condition, msg) ((void)sizeof(char[2*!!(condition) - 1]))
+#  define OC_CAT_(a, b) a ## b
+#  define OC_CAT(a, b) OC_CAT_(a, b)
+#  define OC_STATIC_ASSERT(condition, msg) \
+        typedef int OC_CAT(StaticAssertTemp, __LINE__)[2 * !!(condition) - 1]
 #endif
 
 #ifndef INLINE_API
index e0dade0..ee934d9 100755 (executable)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
+// Warning disabled globally but VS2013 ignores the /wd4200 option in C++ files.
+#if defined(_MSC_VER) && _MSC_VER < 1900
+#pragma warning(disable : 4200)
+#endif
+
 #include "iotivity_config.h"
 #include <stdio.h>
 #include <stdlib.h>
index 18d2ff9..a53ec1b 100644 (file)
@@ -372,7 +372,7 @@ static OCStackResult HandlePresenceResponse(const CAEndpoint_t *endPoint,
  * @param responseInfo CA response info.
  */
 static void HandleCAResponses(const CAEndpoint_t* endPoint,
-        const CAResponseInfo_t* responseInfo);
+    const CAResponseInfo_t* responseInfo);
 
 /**
  * This function will be called back by CA layer when a request is received.
@@ -381,7 +381,7 @@ static void HandleCAResponses(const CAEndpoint_t* endPoint,
  * @param requestInfo CA request info.
  */
 static void HandleCARequests(const CAEndpoint_t* endPoint,
-        const CARequestInfo_t* requestInfo);
+    const CARequestInfo_t* requestInfo);
 
 /**
  * Extract query from a URI.
@@ -1496,7 +1496,7 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
         // check obs header option
         bool obsHeaderOpt = false;
         CAHeaderOption_t *options = responseInfo->info.options;
-        for (uint8_t i = 0; i< responseInfo->info.numOptions; i++)
+        for (uint8_t i = 0; i < responseInfo->info.numOptions; i++)
         {
             if (options && (options[i].optionID == COAP_OPTION_OBSERVE))
             {
@@ -1857,7 +1857,7 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
             response->numRcvdVendorSpecificHeaderOptions = 0;
             if((responseInfo->info.numOptions > 0) && (responseInfo->info.options != NULL))
             {
-                int start = 0;
+                uint8_t start = 0;
                 //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
                 if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
                 {
@@ -1888,9 +1888,9 @@ void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* resp
                     return;
                 }
 
-                for (int i = start; i < responseInfo->info.numOptions; i++)
+                for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
                 {
-                    memcpy (&(response->rcvdVendorSpecificHeaderOptions[i-start]),
+                    memcpy (&(response->rcvdVendorSpecificHeaderOptions[i - start]),
                             &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
                 }
             }
@@ -2071,8 +2071,8 @@ void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* res
      * RI as this option will make no sense to either RI or application.
      */
     RMUpdateInfo((CAHeaderOption_t **) &(responseInfo->info.options),
-                 (uint8_t *) &(responseInfo->info.numOptions),
-                 (CAEndpoint_t *) endPoint);
+        (uint8_t *) &(responseInfo->info.numOptions),
+        (CAEndpoint_t *)endPoint);
 #endif
 
     OCHandleResponse(endPoint, responseInfo);
@@ -2569,8 +2569,8 @@ void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* reque
      * proper destination and remove RM header option.
      */
     RMUpdateInfo((CAHeaderOption_t **) &(requestInfo->info.options),
-                 (uint8_t *) &(requestInfo->info.numOptions),
-                 (CAEndpoint_t *) endPoint);
+        (uint8_t *) &(requestInfo->info.numOptions),
+        (CAEndpoint_t *)endPoint);
 
 #ifdef ROUTING_GATEWAY
     if (isEmptyMsg)
@@ -5983,7 +5983,7 @@ OCStackResult OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t numOptions,
         return OC_STACK_INVALID_PARAM;
     }
 
-    for (uint8_t i = 0; i < numOptions; i++)
+    for (size_t i = 0; i < numOptions; i++)
     {
         if (ocHdrOpt[i].optionID == optionID)
         {
index 39a804a..4bee8ef 100644 (file)
@@ -2585,8 +2585,8 @@ TEST(StackHeaderOption, getHeaderOption)
                                              optionData,
                                              optionDataSize,
                                              &actualDataSize));
-    EXPECT_EQ(optionData[0], 1);
-    EXPECT_EQ(actualDataSize, 8);
+    EXPECT_EQ(1, optionData[0]);
+    EXPECT_EQ(8, actualDataSize);
 }
 
 TEST(StackEndpoints, OCGetSupportedEndpointTpsFlags)
index 2428cea..dfd1927 100644 (file)
@@ -1317,6 +1317,14 @@ namespace OC
         OCStackResult result;
         auto cLock = m_csdkLock.lock();
 
+        if (headerOptions.size() > MAX_HEADER_OPTIONS)
+        {
+            OIC_LOG_V(ERROR, TAG, "%s: passed number of header options"
+                " (%" PRIuPTR ") exceeds the maximum of %d.",
+                __func__, headerOptions.size(), MAX_HEADER_OPTIONS);
+            return OC_STACK_ERROR;
+        }
+
         if (cLock)
         {
             std::lock_guard<std::recursive_mutex> lock(*cLock);
@@ -1325,7 +1333,7 @@ namespace OC
             result = OCCancel(handle,
                     static_cast<OCQualityOfService>(QoS),
                     assembleHeaderOptions(options, headerOptions),
-                    headerOptions.size());
+                    (uint8_t)headerOptions.size());
         }
         else
         {
@@ -1478,10 +1486,19 @@ namespace OC
 
         for (auto it=headerOptions.begin(); it != headerOptions.end(); ++it)
         {
+            size_t headerOptionLength = it->getOptionData().length() + 1;
+            if (headerOptionLength > MAX_HEADER_OPTION_DATA_LENGTH)
+            {
+                OIC_LOG_V(ERROR, TAG, "%s: passed header option's data length"
+                    " (%" PRIuPTR ") exceeds the maximum of %d.",
+                    __func__, headerOptionLength, MAX_HEADER_OPTION_DATA_LENGTH);
+                return nullptr;
+            }
+
             options[i] = OCHeaderOption();
             options[i].protocolID = OC_COAP_ID;
             options[i].optionID = it->getOptionID();
-            options[i].optionLength = it->getOptionData().length() + 1;
+            options[i].optionLength = (uint16_t)headerOptionLength;
             strncpy((char*)options[i].optionData, it->getOptionData().c_str(),
                 sizeof(options[i].optionLength) -1 );
             options[i].optionData[sizeof(options[i].optionLength) - 1] = 0;
index 333b056..3a6f368 100644 (file)
@@ -601,6 +601,12 @@ namespace OC
         auto cLock = m_csdkLock.lock();
         OCStackResult result = OC_STACK_ERROR;
 
+        if (pResponse->getHeaderOptions().size() > MAX_HEADER_OPTIONS)
+        {
+            oclog() << "Error passed too many server header options.\n";
+            return OC_STACK_ERROR;
+        }
+
         if(!pResponse)
         {
             result = OC_STACK_MALFORMED_RESPONSE;
@@ -620,15 +626,29 @@ namespace OC
 
             response.persistentBufferFlag = 0;
 
-            response.numSendVendorSpecificHeaderOptions = serverHeaderOptions.size();
+            OC_STATIC_ASSERT(MAX_HEADER_OPTIONS <= UINT8_MAX,
+                             "Maximum number of headers too large.");
+
+            response.numSendVendorSpecificHeaderOptions = (uint8_t)serverHeaderOptions.size();
             int i = 0;
             for (auto it=serverHeaderOptions.begin(); it != serverHeaderOptions.end(); ++it)
             {
+                size_t optionDataLength = (it->getOptionData()).length() + 1;
+
+                if (optionDataLength > MAX_HEADER_OPTION_DATA_LENGTH)
+                {
+                    oclog() << "Error header " << i << " option data length too large.\n";
+                    return OC_STACK_ERROR;
+                }
+
+                OC_STATIC_ASSERT(MAX_HEADER_OPTION_DATA_LENGTH <= UINT16_MAX,
+                                 "Max header options lenght too large.");
+
                 response.sendVendorSpecificHeaderOptions[i].protocolID = OC_COAP_ID;
                 response.sendVendorSpecificHeaderOptions[i].optionID =
                     static_cast<uint16_t>(it->getOptionID());
                 response.sendVendorSpecificHeaderOptions[i].optionLength =
-                    (it->getOptionData()).length() + 1;
+                    (uint16_t)optionDataLength;
                 std::string optionData = it->getOptionData();
                 std::copy(optionData.begin(),
                          optionData.end(),
index 0908d1f..633d205 100644 (file)
@@ -291,7 +291,7 @@ namespace OC
                                        const std::shared_ptr<OCResourceResponse> pResponse,
                                        QualityOfService QoS)
     {
-        if(!pResponse)
+        if(!pResponse || (observationIds.size() > UINT8_MAX))
         {
          return result_guard(OC_STACK_ERROR);
         }
@@ -299,7 +299,7 @@ namespace OC
         OCRepPayload* pl = pResponse->getResourceRepresentation().getPayload();
         OCStackResult result =
                    OCNotifyListOfObservers(resourceHandle,
-                            &observationIds[0], observationIds.size(),
+                            &observationIds[0], (uint8_t)observationIds.size(),
                             pl,
                             static_cast<OCQualityOfService>(QoS));
         OCRepPayloadDestroy(pl);