From: Pawel Winogrodzki Date: Wed, 3 May 2017 06:29:30 +0000 (-0700) Subject: IOT-1583: Fixing /W3 warnings in /resource directory - part 3. X-Git-Tag: 1.3.0~194 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=27024c3512d77d75296debc07d65c44045998b51;p=platform%2Fupstream%2Fiotivity.git IOT-1583: Fixing /W3 warnings in /resource directory - part 3. Fixing all /W3s inside connectivity, routing, security and stack directories. Change-Id: I9e99d5f813d6f3f399983759da0b98eeb03c9388 Signed-off-by: Pawel Winogrodzki Reviewed-on: https://gerrit.iotivity.org/gerrit/19559 Tested-by: jenkins-iotivity Reviewed-by: Phil Coval Reviewed-by: Alex Kelley Reviewed-by: Mike Fenelon --- diff --git a/resource/c_common/platform_features.h b/resource/c_common/platform_features.h index a90bc41..79d39b8 100644 --- a/resource/c_common/platform_features.h +++ b/resource/c_common/platform_features.h @@ -39,7 +39,10 @@ # 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 diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp index e0dade0..ee934d9 100755 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/occlient.cpp @@ -18,6 +18,11 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// 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 #include diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index 18d2ff9..a53ec1b 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -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) { diff --git a/resource/csdk/stack/test/stacktests.cpp b/resource/csdk/stack/test/stacktests.cpp index 39a804a..4bee8ef 100644 --- a/resource/csdk/stack/test/stacktests.cpp +++ b/resource/csdk/stack/test/stacktests.cpp @@ -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) diff --git a/resource/src/InProcClientWrapper.cpp b/resource/src/InProcClientWrapper.cpp index 2428cea..dfd1927 100644 --- a/resource/src/InProcClientWrapper.cpp +++ b/resource/src/InProcClientWrapper.cpp @@ -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 lock(*cLock); @@ -1325,7 +1333,7 @@ namespace OC result = OCCancel(handle, static_cast(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; diff --git a/resource/src/InProcServerWrapper.cpp b/resource/src/InProcServerWrapper.cpp index 333b056..3a6f368 100644 --- a/resource/src/InProcServerWrapper.cpp +++ b/resource/src/InProcServerWrapper.cpp @@ -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(it->getOptionID()); response.sendVendorSpecificHeaderOptions[i].optionLength = - (it->getOptionData()).length() + 1; + (uint16_t)optionDataLength; std::string optionData = it->getOptionData(); std::copy(optionData.begin(), optionData.end(), diff --git a/resource/src/OCPlatform_impl.cpp b/resource/src/OCPlatform_impl.cpp index 0908d1f..633d205 100644 --- a/resource/src/OCPlatform_impl.cpp +++ b/resource/src/OCPlatform_impl.cpp @@ -291,7 +291,7 @@ namespace OC const std::shared_ptr 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(QoS)); OCRepPayloadDestroy(pl);