From 3150237c8f7b4e3e820bc71985d25e19213469d9 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 1 Mar 2023 11:07:08 -0500 Subject: [PATCH] Enable -Wconversion for nrfconnect platform bits. (#25383) --- .../GenericThreadStackManagerImpl_OpenThread.cpp | 5 +++-- src/platform/Zephyr/BUILD.gn | 2 ++ .../Zephyr/DiagnosticDataProviderImpl.cpp | 2 +- src/platform/Zephyr/PlatformManagerImpl.cpp | 16 +++++++++++++--- src/platform/nrfconnect/BUILD.gn | 2 ++ .../nrfconnect/OTAImageProcessorImpl.cpp | 12 ++++++++++-- src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp | 2 +- src/platform/nrfconnect/wifi/WiFiManager.cpp | 8 ++++---- 8 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp index 3f0410c0cd0b40..3489213552e2f4 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp @@ -482,7 +482,8 @@ void GenericThreadStackManagerImpl_OpenThread::_OnNetworkScanFinished scanResponse.lqi = aResult->mLqi; scanResponse.extendedAddress = Encoding::BigEndian::Get64(aResult->mExtAddress.m8); scanResponse.extendedPanId = Encoding::BigEndian::Get64(aResult->mExtendedPanId.m8); - scanResponse.networkNameLen = strnlen(aResult->mNetworkName.m8, OT_NETWORK_NAME_MAX_SIZE); + static_assert(OT_NETWORK_NAME_MAX_SIZE <= UINT8_MAX, "Network name length won't fit"); + scanResponse.networkNameLen = static_cast(strnlen(aResult->mNetworkName.m8, OT_NETWORK_NAME_MAX_SIZE)); memcpy(scanResponse.networkName, aResult->mNetworkName.m8, scanResponse.networkNameLen); mScanResponseIter.Add(&scanResponse); @@ -1168,7 +1169,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_WriteThreadNetw } else { - lastRssi.SetNonNull(((neighInfo.mLastRssi > 0) ? 0 : neighInfo.mLastRssi)); + lastRssi.SetNonNull(min(static_cast(0), neighInfo.mLastRssi)); } neighborTable.averageRssi = averageRssi; diff --git a/src/platform/Zephyr/BUILD.gn b/src/platform/Zephyr/BUILD.gn index 61834d8b12eb52..748bc6e2419994 100644 --- a/src/platform/Zephyr/BUILD.gn +++ b/src/platform/Zephyr/BUILD.gn @@ -76,4 +76,6 @@ static_library("Zephyr") { if (chip_malloc_sys_heap) { sources += [ "SysHeapMalloc.cpp" ] } + + cflags = [ "-Wconversion" ] } diff --git a/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp index a735d3e0162b79..e7fc5b38428ba8 100644 --- a/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp +++ b/src/platform/Zephyr/DiagnosticDataProviderImpl.cpp @@ -226,7 +226,7 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetTotalOperationalHours(uint32_t & total ReturnErrorOnFailure(ConfigurationMgr().GetTotalOperationalHours(reinterpret_cast(totalHours))); - totalOperationalHours = totalHours + deltaTime < UINT32_MAX ? totalHours + deltaTime : UINT32_MAX; + totalOperationalHours = static_cast(totalHours + deltaTime < UINT32_MAX ? totalHours + deltaTime : UINT32_MAX); return CHIP_NO_ERROR; } diff --git a/src/platform/Zephyr/PlatformManagerImpl.cpp b/src/platform/Zephyr/PlatformManagerImpl.cpp index 6cd42773227ed2..0e5aac4bda8dda 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.cpp +++ b/src/platform/Zephyr/PlatformManagerImpl.cpp @@ -27,6 +27,7 @@ #include +#include #include #include #include @@ -48,11 +49,20 @@ static k_timer sOperationalHoursSavingTimer; static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen) { const struct device * entropy = DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy)); - int ret = entropy_get_entropy(entropy, output, len); + uint16_t clampedLen; + if (CanCastTo(len)) + { + clampedLen = static_cast(len); + } + else + { + clampedLen = UINT16_MAX; + } + int ret = entropy_get_entropy(entropy, output, clampedLen); if (ret == 0) { - *olen = len; + *olen = clampedLen; } else { @@ -85,7 +95,7 @@ void PlatformManagerImpl::UpdateOperationalHours(intptr_t arg) if (ConfigurationMgr().GetTotalOperationalHours(reinterpret_cast(totalOperationalHours)) == CHIP_NO_ERROR) { ConfigurationMgr().StoreTotalOperationalHours( - totalOperationalHours + deltaTime < UINT32_MAX ? totalOperationalHours + deltaTime : UINT32_MAX); + static_cast(totalOperationalHours + deltaTime < UINT32_MAX ? totalOperationalHours + deltaTime : UINT32_MAX)); sInstance.mSavedOperationalHoursSinceBoot = upTimeH; } else diff --git a/src/platform/nrfconnect/BUILD.gn b/src/platform/nrfconnect/BUILD.gn index f4a79e118b8da7..40c6f8cac3d292 100644 --- a/src/platform/nrfconnect/BUILD.gn +++ b/src/platform/nrfconnect/BUILD.gn @@ -114,4 +114,6 @@ static_library("nrfconnect") { if (chip_malloc_sys_heap) { sources += [ "../Zephyr/SysHeapMalloc.cpp" ] } + + cflags = [ "-Wconversion" ] } diff --git a/src/platform/nrfconnect/OTAImageProcessorImpl.cpp b/src/platform/nrfconnect/OTAImageProcessorImpl.cpp index b86439f469656c..9d024991f122a0 100644 --- a/src/platform/nrfconnect/OTAImageProcessorImpl.cpp +++ b/src/platform/nrfconnect/OTAImageProcessorImpl.cpp @@ -164,8 +164,16 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & aBlock) if (error == CHIP_NO_ERROR) { // DFU target library buffers data internally, so do not clone the block data. - error = System::MapErrorZephyr(dfu_multi_image_write(mParams.downloadedBytes, aBlock.data(), aBlock.size())); - mParams.downloadedBytes += aBlock.size(); + if (mParams.downloadedBytes > std::numeric_limits::max()) + { + error = CHIP_ERROR_BUFFER_TOO_SMALL; + } + else + { + error = System::MapErrorZephyr( + dfu_multi_image_write(static_cast(mParams.downloadedBytes), aBlock.data(), aBlock.size())); + mParams.downloadedBytes += aBlock.size(); + } } // Report the result back to the downloader asynchronously. diff --git a/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp b/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp index e8e5d10d73ae97..bfb284f3b84851 100644 --- a/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp +++ b/src/platform/nrfconnect/wifi/NrfWiFiDriver.cpp @@ -48,7 +48,7 @@ bool NrfWiFiDriver::WiFiNetworkIterator::Next(Network & item) } memcpy(item.networkID, mDriver->mStagingNetwork.ssid, mDriver->mStagingNetwork.ssidLen); - item.networkIDLen = mDriver->mStagingNetwork.ssidLen; + item.networkIDLen = static_cast(mDriver->mStagingNetwork.ssidLen); item.connected = false; mExhausted = true; diff --git a/src/platform/nrfconnect/wifi/WiFiManager.cpp b/src/platform/nrfconnect/wifi/WiFiManager.cpp index a47655c0fa9624..6a4585a57723f8 100644 --- a/src/platform/nrfconnect/wifi/WiFiManager.cpp +++ b/src/platform/nrfconnect/wifi/WiFiManager.cpp @@ -252,8 +252,8 @@ CHIP_ERROR WiFiManager::GetWiFiInfo(WiFiInfo & info) const info.mBssId = ByteSpan(mac_string_buf, sizeof(mac_string_buf)); info.mSecurityType = static_cast(status.security); info.mWiFiVersion = static_cast(status.link_mode); - info.mRssi = status.rssi; - info.mChannel = status.channel; + info.mRssi = static_cast(status.rssi); + info.mChannel = static_cast(status.channel); info.mSsidLen = status.ssid_len; memcpy(info.mSsid, status.ssid, status.ssid_len); @@ -291,12 +291,12 @@ void WiFiManager::ScanResultHandler(uint8_t * data) if (scanResult->rssi > Instance().mWiFiParams.mRssi) { Instance().ClearStationProvisioningData(); - Instance().mWiFiParams.mParams.ssid_length = Instance().mWantedNetwork.ssidLen; + Instance().mWiFiParams.mParams.ssid_length = static_cast(Instance().mWantedNetwork.ssidLen); Instance().mWiFiParams.mParams.ssid = Instance().mWantedNetwork.ssid; // Fallback to the WIFI_SECURITY_TYPE_PSK if the security is unknown Instance().mWiFiParams.mParams.security = scanResult->security <= WIFI_SECURITY_TYPE_MAX ? scanResult->security : WIFI_SECURITY_TYPE_PSK; - Instance().mWiFiParams.mParams.psk_length = Instance().mWantedNetwork.passLen; + Instance().mWiFiParams.mParams.psk_length = static_cast(Instance().mWantedNetwork.passLen); // If the security is none, WiFi driver expects the psk to be nullptr if (Instance().mWiFiParams.mParams.security == WIFI_SECURITY_TYPE_NONE)