Skip to content

Commit 9c2b297

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Enable -Wconversion in src/platform/Darwin. (#25323)
Also fixes incorrect handling of the bssid from CWNetwork.
1 parent 69b939c commit 9c2b297

File tree

9 files changed

+44
-24
lines changed

9 files changed

+44
-24
lines changed

src/app/EventLoggingTypes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static_assert(sizeof(std::underlying_type_t<PriorityLevel>) <= sizeof(unsigned),
9595
*/
9696
struct Timestamp
9797
{
98-
enum class Type
98+
enum class Type : uint8_t
9999
{
100100
kSystem = 0,
101101
kEpoch

src/app/util/af-types.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include <app/ConcreteAttributePath.h>
3737
#include <app/data-model/Nullable.h>
38+
#include <lib/core/DataModelTypes.h>
3839
#include <lib/support/Variant.h>
3940
#include <messaging/ExchangeContext.h>
4041

@@ -123,7 +124,7 @@ typedef struct
123124
*/
124125
typedef struct
125126
{
126-
uint16_t deviceId;
127+
chip::DeviceTypeId deviceId;
127128
uint8_t deviceVersion;
128129
} EmberAfDeviceType;
129130

src/include/platform/BuildTime.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
// Example of __TIME__ string: "21:06:19"
2929

3030
#define COMPUTE_BUILD_YEAR(_date) \
31-
(((_date)[7] - '0') * 1000 + ((_date)[8] - '0') * 100 + ((_date)[9] - '0') * 10 + ((_date)[10] - '0'))
31+
static_cast<uint16_t>(((_date)[7] - '0') * 1000 + ((_date)[8] - '0') * 100 + ((_date)[9] - '0') * 10 + ((_date)[10] - '0'))
3232

33-
#define COMPUTE_BUILD_DAY(_date) ((((_date)[4] >= '0') ? ((_date)[4] - '0') * 10 : 0) + ((_date)[5] - '0'))
33+
#define COMPUTE_BUILD_DAY(_date) static_cast<uint8_t>((((_date)[4] >= '0') ? ((_date)[4] - '0') * 10 : 0) + ((_date)[5] - '0'))
3434

3535
#define BUILD_MONTH_IS_JAN(_date) ((_date)[0] == 'J' && (_date)[1] == 'a')
3636
#define BUILD_MONTH_IS_FEB(_date) ((_date)[0] == 'F')
@@ -63,9 +63,9 @@
6363
? 11 \
6464
: (BUILD_MONTH_IS_DEC(_date)) ? 12 : /* error default */ 99)
6565

66-
#define COMPUTE_BUILD_HOUR(_time) (((_time)[0] - '0') * 10 + (_time)[1] - '0')
67-
#define COMPUTE_BUILD_MIN(_time) (((_time)[3] - '0') * 10 + (_time)[4] - '0')
68-
#define COMPUTE_BUILD_SEC(_time) (((_time)[6] - '0') * 10 + (_time)[7] - '0')
66+
#define COMPUTE_BUILD_HOUR(_time) static_cast<uint8_t>(((_time)[0] - '0') * 10 + (_time)[1] - '0')
67+
#define COMPUTE_BUILD_MIN(_time) static_cast<uint8_t>(((_time)[3] - '0') * 10 + (_time)[4] - '0')
68+
#define COMPUTE_BUILD_SEC(_time) static_cast<uint8_t>(((_time)[6] - '0') * 10 + (_time)[7] - '0')
6969

7070
#define BUILD_DATE_IS_BAD(_date) ((_date) == nullptr || strlen(_date) < strlen("Jan 01 2000") || (_date)[0] == '?')
7171
#define BUILD_TIME_IS_BAD(_time) ((_time) == nullptr || strlen(_time) < strlen("00:00:00") || (_time)[0] == '?')

src/messaging/tests/echo/echo_requester.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void HandleEchoResponseReceived(chip::Messaging::ExchangeContext * ec, chip::Sys
184184
gEchoRespCount++;
185185

186186
printf("Echo Response: %" PRIu64 "/%" PRIu64 "(%.2f%%) len=%u time=%.3fs\n", gEchoRespCount, gEchoCount,
187-
static_cast<double>(gEchoRespCount) * 100 / gEchoCount, payload->DataLength(),
187+
static_cast<double>(gEchoRespCount) * 100 / static_cast<double>(gEchoCount), payload->DataLength(),
188188
static_cast<double>(chip::System::Clock::Milliseconds32(transitTime).count()) / 1000);
189189
}
190190

src/platform/Darwin/BUILD.gn

+8-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ config("darwin_config") {
3636
]
3737
}
3838

39-
cflags = [ "-fobjc-arc" ]
39+
cflags = [
40+
"-fobjc-arc",
41+
"-Wconversion",
42+
]
4043
}
4144

4245
static_library("Darwin") {
@@ -140,6 +143,9 @@ static_library("logging") {
140143
]
141144

142145
configs += [ "${chip_root}/src:includes" ]
143-
cflags = [ "-fobjc-arc" ]
146+
cflags = [
147+
"-fobjc-arc",
148+
"-Wconversion",
149+
]
144150
frameworks = [ "Foundation.framework" ]
145151
}

src/platform/Darwin/BleConnectionDelegateImpl.mm

+2-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ - (void)resetTimer
175175
{
176176
auto timeout =
177177
[self hasDiscriminator] ? kScanningWithDiscriminatorTimeoutInSeconds : kScanningWithoutDiscriminatorTimeoutInSeconds;
178-
dispatch_source_set_timer(_timer, dispatch_walltime(nullptr, timeout * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, 5 * NSEC_PER_SEC);
178+
dispatch_source_set_timer(
179+
_timer, dispatch_walltime(nullptr, static_cast<int64_t>(timeout * NSEC_PER_SEC)), DISPATCH_TIME_FOREVER, 5 * NSEC_PER_SEC);
179180
}
180181

181182
// All our callback dispatch must happen on _chipWorkQueue

src/platform/Darwin/ConnectivityManagerImpl.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <new>
2525

2626
#include <lib/support/CodeUtils.h>
27+
#include <lib/support/SafeInt.h>
2728
#include <lib/support/logging/CHIPLogging.h>
2829

2930
#include <platform/internal/GenericConnectivityManagerImpl_UDP.ipp>
@@ -80,6 +81,10 @@ CHIP_ERROR ConnectivityManagerImpl::GetEthernetInterfaceName(char * outName, siz
8081
{
8182
CHIP_ERROR err = CHIP_ERROR_NOT_IMPLEMENTED;
8283
#if TARGET_OS_OSX
84+
if (!CanCastTo<CFIndex>(maxLen))
85+
{
86+
return CHIP_ERROR_INVALID_ARGUMENT;
87+
}
8388
CFArrayRef interfaces = SCNetworkInterfaceCopyAll();
8489
VerifyOrReturnError(interfaces != nullptr, CHIP_ERROR_INTERNAL);
8590

@@ -102,7 +107,7 @@ CHIP_ERROR ConnectivityManagerImpl::GetEthernetInterfaceName(char * outName, siz
102107
continue;
103108
}
104109

105-
if (!CFStringGetCString(interfaceName, outName, maxLen, kCFStringEncodingUTF8))
110+
if (!CFStringGetCString(interfaceName, outName, static_cast<CFIndex>(maxLen), kCFStringEncodingUTF8))
106111
{
107112
continue;
108113
}

src/platform/Darwin/DnssdHostNameRegistrar.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ namespace Dnssd {
5151
auto interfaceAddress = static_cast<const void *>(&interface.second);
5252
auto interfaceAddressLen = sizeof(interface.second);
5353

54-
LogErrorOnFailure(RegisterInterface(interfaceId, type, interfaceAddress, interfaceAddressLen));
54+
LogErrorOnFailure(
55+
RegisterInterface(interfaceId, type, interfaceAddress, static_cast<uint16_t>(interfaceAddressLen)));
5556
}
5657
}
5758

src/platform/Darwin/WiFi/NetworkCommissioningWiFiDriver.mm

+17-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <lib/support/BytesToHex.h>
1819
#include <lib/support/CodeUtils.h>
1920
#include <platform/CHIPDeviceLayer.h>
2021
#include <platform/Darwin/ConfigurationManagerImpl.h>
@@ -72,18 +73,23 @@ bool Next(WiFiScanResponse & scanResponse) override
7273
void CopyNetworkInformationTo(WiFiScanResponse & destination, CWNetwork * source)
7374
{
7475
destination.security = GetWiFiSecurity(source);
75-
destination.ssidLen = [source.ssid length];
76-
destination.channel = source.wlanChannel.channelNumber;
76+
destination.channel = static_cast<uint16_t>(source.wlanChannel.channelNumber);
7777
destination.wiFiBand = GetWiFiBand(source.wlanChannel);
78-
destination.rssi = source.rssiValue;
79-
CopyStringTo(destination.ssid, source.ssid, DeviceLayer::Internal::kMaxWiFiSSIDLength);
80-
CopyStringTo(destination.bssid, source.bssid, DeviceLayer::Internal::kWiFiBSSIDLength);
81-
}
82-
83-
void CopyStringTo(uint8_t * destination, NSString * source, size_t maxLength)
84-
{
85-
NSData * data = [source dataUsingEncoding:NSUTF8StringEncoding];
86-
memcpy(destination, [data bytes], std::min([data length], maxLength));
78+
destination.rssi = static_cast<int8_t>(source.rssiValue);
79+
80+
NSData * ssidData = source.ssidData;
81+
destination.ssidLen = static_cast<uint8_t>(std::min(ssidData.length, DeviceLayer::Internal::kMaxWiFiSSIDLength));
82+
memcpy(destination.ssid, ssidData.bytes, destination.ssidLen);
83+
84+
// source.bssid looks like "00:00:00:00:00:00" if it's not nil.
85+
NSString * bssid = source.bssid;
86+
// 3 chars per byte, except the last byte.
87+
if (bssid.length == 3 * sizeof(destination.bssid) - 1) {
88+
const char * chars = bssid.UTF8String;
89+
for (size_t i = 0; i < sizeof(destination.bssid); ++i) {
90+
Encoding::HexToBytes(&chars[3 * i], 2, &destination.bssid[i], 1);
91+
}
92+
}
8793
}
8894

8995
WiFiSecurity GetWiFiSecurity(CWNetwork * network)

0 commit comments

Comments
 (0)