Skip to content

Commit 9092552

Browse files
Right sizing MACAddress buffers
1 parent 74a54b6 commit 9092552

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/NsdResolveListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ private boolean isPassingDeviceTypeFilter(DiscoveredNodeData discoveredNodeData)
143143

144144
private void addCommissioningInfo(DiscoveredNodeData discoveredNodeData) {
145145
if (preCommissionedVideoPlayers != null) {
146+
long currentUnixTimeMS = System.currentTimeMillis();
146147
for (VideoPlayer videoPlayer : preCommissionedVideoPlayers) {
147148
if (videoPlayer.isSameAs(discoveredNodeData)) {
148149
Log.d(
149150
TAG,
150151
"Matching Video Player with the following information found for DiscoveredNodeData"
151152
+ videoPlayer);
152-
long currentUnixTimeMS = System.currentTimeMillis();
153153
Log.d(TAG, "Updating discovery timestamp for VideoPlayer to " + currentUnixTimeMS);
154154
videoPlayer.setLastDiscoveredMs(currentUnixTimeMS);
155155
discoveredNodeData.setConnectableVideoPlayer(videoPlayer);

examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe
183183

184184
if (MACAddress != nullptr)
185185
{
186-
chip::CharSpan MACAddressSpan(MACAddress, strlen(MACAddress) - 1);
186+
chip::CharSpan MACAddressSpan(MACAddress, 2 * 2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength);
187187
outTargetVideoPlayerInfo.SetMACAddress(MACAddressSpan);
188188
}
189189

@@ -257,9 +257,9 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
257257
jstring MACAddress = nullptr;
258258
if (targetVideoPlayerInfo->GetMACAddress() != nullptr && targetVideoPlayerInfo->GetMACAddress()->data() != nullptr)
259259
{
260-
char MACAddressWithNil[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength + 1];
261-
strncpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(),
262-
targetVideoPlayerInfo->GetMACAddress()->size());
260+
char MACAddressWithNil[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength + 1];
261+
memcpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(),
262+
targetVideoPlayerInfo->GetMACAddress()->size());
263263
MACAddressWithNil[targetVideoPlayerInfo->GetMACAddress()->size()] = '\0';
264264
MACAddress = env->NewStringUTF(MACAddressWithNil);
265265
}

examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class TargetVideoPlayerInfo
9595
bool IsAsleep() { return mIsAsleep; }
9696
void SetMACAddress(chip::CharSpan MACAddress)
9797
{
98-
memcpy(mMACAddressBuf, MACAddress.data(), MACAddress.size());
99-
mMACAddress = chip::CharSpan(mMACAddressBuf, MACAddress.size());
98+
memcpy(mMACAddressBuf, MACAddress.data(), sizeof(mMACAddressBuf));
99+
mMACAddress = chip::CharSpan(mMACAddressBuf, sizeof(mMACAddressBuf));
100100
}
101101
chip::System::Clock::Timestamp GetLastDiscovered() { return mLastDiscovered; }
102102
void SetLastDiscovered(chip::System::Clock::Timestamp lastDiscovered) { mLastDiscovered = lastDiscovered; }
@@ -106,7 +106,7 @@ class TargetVideoPlayerInfo
106106
// it was recently discoverable if its mLastDiscovered.count is within
107107
// CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_HOURS of current time
108108
chip::System::Clock::Timestamp currentUnixTimeMS = chip::System::Clock::kZero;
109-
chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS);
109+
VerifyOrReturnValue(chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS) == CHIP_NO_ERROR, true);
110110
ChipLogProgress(AppServer, "WasRecentlyDiscoverable currentUnixTimeMS: %lu mLastDiscovered: %lu",
111111
static_cast<unsigned long>(currentUnixTimeMS.count()), static_cast<unsigned long>(mLastDiscovered.count()));
112112
return mLastDiscovered.count() >
@@ -211,7 +211,7 @@ class TargetVideoPlayerInfo
211211
char mInstanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1];
212212
uint16_t mPort;
213213
chip::CharSpan mMACAddress;
214-
char mMACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength];
214+
char mMACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
215215
chip::System::Clock::Timestamp mLastDiscovered;
216216
bool mIsAsleep = false;
217217
bool mInitialized = false;

examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
267267
char hostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
268268
size_t numIPs = 0;
269269
Inet::IPAddress ipAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];
270-
uint64_t lastDiscoveredMs = 0;
271-
char MACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength] = {};
272-
uint32_t MACAddressLength = 0;
273-
char instanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1] = {};
274-
uint16_t port = 0;
270+
uint64_t lastDiscoveredMs = 0;
271+
char MACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength] = {};
272+
uint32_t MACAddressLength = 0;
273+
char instanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1] = {};
274+
uint16_t port = 0;
275275

276276
CHIP_ERROR err;
277277
while ((err = reader.Next()) == CHIP_NO_ERROR)
@@ -349,7 +349,7 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide
349349
{
350350
MACAddressLength = reader.GetLength();
351351
ReturnErrorOnFailure(reader.GetBytes(reinterpret_cast<uint8_t *>(MACAddressBuf),
352-
2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength));
352+
2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength));
353353
continue;
354354
}
355355

0 commit comments

Comments
 (0)