From 4fd1eb237db2a152b9208da469715f70aa0a94af Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Tue, 17 Oct 2023 10:52:02 -0700 Subject: [PATCH] Right sizing MACAddress buffers --- .../jni/com/chip/casting/NsdResolveListener.java | 2 +- .../App/app/src/main/jni/cpp/ConversionUtils.cpp | 8 ++++---- .../include/TargetVideoPlayerInfo.h | 8 ++++---- .../tv-casting-common/src/PersistenceManager.cpp | 12 ++++++------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/NsdResolveListener.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/NsdResolveListener.java index 253dbee0968874..638425675dc99b 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/NsdResolveListener.java +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/NsdResolveListener.java @@ -143,13 +143,13 @@ private boolean isPassingDeviceTypeFilter(DiscoveredNodeData discoveredNodeData) private void addCommissioningInfo(DiscoveredNodeData discoveredNodeData) { if (preCommissionedVideoPlayers != null) { + long currentUnixTimeMS = System.currentTimeMillis(); for (VideoPlayer videoPlayer : preCommissionedVideoPlayers) { if (videoPlayer.isSameAs(discoveredNodeData)) { Log.d( TAG, "Matching Video Player with the following information found for DiscoveredNodeData" + videoPlayer); - long currentUnixTimeMS = System.currentTimeMillis(); Log.d(TAG, "Updating discovery timestamp for VideoPlayer to " + currentUnixTimeMS); videoPlayer.setLastDiscoveredMs(currentUnixTimeMS); discoveredNodeData.setConnectableVideoPlayer(videoPlayer); diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp index f78c27c4b31383..dfcb2d8f44f69f 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp @@ -183,7 +183,7 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe if (MACAddress != nullptr) { - chip::CharSpan MACAddressSpan(MACAddress, strlen(MACAddress) - 1); + chip::CharSpan MACAddressSpan(MACAddress, 2 * 2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength); outTargetVideoPlayerInfo.SetMACAddress(MACAddressSpan); } @@ -257,9 +257,9 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta jstring MACAddress = nullptr; if (targetVideoPlayerInfo->GetMACAddress() != nullptr && targetVideoPlayerInfo->GetMACAddress()->data() != nullptr) { - char MACAddressWithNil[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength + 1]; - strncpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(), - targetVideoPlayerInfo->GetMACAddress()->size()); + char MACAddressWithNil[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength + 1]; + memcpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(), + targetVideoPlayerInfo->GetMACAddress()->size()); MACAddressWithNil[targetVideoPlayerInfo->GetMACAddress()->size()] = '\0'; MACAddress = env->NewStringUTF(MACAddressWithNil); } diff --git a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h index b2086368467513..8c00cf61da4031 100644 --- a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h +++ b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h @@ -95,8 +95,8 @@ class TargetVideoPlayerInfo bool IsAsleep() { return mIsAsleep; } void SetMACAddress(chip::CharSpan MACAddress) { - memcpy(mMACAddressBuf, MACAddress.data(), MACAddress.size()); - mMACAddress = chip::CharSpan(mMACAddressBuf, MACAddress.size()); + memcpy(mMACAddressBuf, MACAddress.data(), sizeof(mMACAddressBuf)); + mMACAddress = chip::CharSpan(mMACAddressBuf, sizeof(mMACAddressBuf)); } chip::System::Clock::Timestamp GetLastDiscovered() { return mLastDiscovered; } void SetLastDiscovered(chip::System::Clock::Timestamp lastDiscovered) { mLastDiscovered = lastDiscovered; } @@ -106,7 +106,7 @@ class TargetVideoPlayerInfo // it was recently discoverable if its mLastDiscovered.count is within // CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_HOURS of current time chip::System::Clock::Timestamp currentUnixTimeMS = chip::System::Clock::kZero; - chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS); + VerifyOrReturnValue(chip::System::SystemClock().GetClock_RealTimeMS(currentUnixTimeMS) == CHIP_NO_ERROR, true); ChipLogProgress(AppServer, "WasRecentlyDiscoverable currentUnixTimeMS: %lu mLastDiscovered: %lu", static_cast(currentUnixTimeMS.count()), static_cast(mLastDiscovered.count())); return mLastDiscovered.count() > @@ -211,7 +211,7 @@ class TargetVideoPlayerInfo char mInstanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1]; uint16_t mPort; chip::CharSpan mMACAddress; - char mMACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength]; + char mMACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength]; chip::System::Clock::Timestamp mLastDiscovered; bool mIsAsleep = false; bool mInitialized = false; diff --git a/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp b/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp index 32b6eae3714597..8d2db13d95b292 100644 --- a/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/PersistenceManager.cpp @@ -267,11 +267,11 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide char hostName[chip::Dnssd::kHostNameMaxLength + 1] = {}; size_t numIPs = 0; Inet::IPAddress ipAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses]; - uint64_t lastDiscoveredMs = 0; - char MACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength] = {}; - uint32_t MACAddressLength = 0; - char instanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1] = {}; - uint16_t port = 0; + uint64_t lastDiscoveredMs = 0; + char MACAddressBuf[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength] = {}; + uint32_t MACAddressLength = 0; + char instanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1] = {}; + uint16_t port = 0; CHIP_ERROR err; while ((err = reader.Next()) == CHIP_NO_ERROR) @@ -349,7 +349,7 @@ CHIP_ERROR PersistenceManager::ReadAllVideoPlayers(TargetVideoPlayerInfo outVide { MACAddressLength = reader.GetLength(); ReturnErrorOnFailure(reader.GetBytes(reinterpret_cast(MACAddressBuf), - 2 * chip::DeviceLayer::ConfigurationManager::kMaxMACAddressLength)); + 2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength)); continue; }