From 131637264bb892e973329346509aa5775effcbb8 Mon Sep 17 00:00:00 2001 From: Junior Martinez <67972863+jmartinez-silabs@users.noreply.github.com> Date: Tue, 11 Jul 2023 16:54:52 -0400 Subject: [PATCH] Fix thread network scan list encode when no netowrk was found (#27896) --- .../network-commissioning.cpp | 91 ++++++++++--------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/src/app/clusters/network-commissioning/network-commissioning.cpp b/src/app/clusters/network-commissioning/network-commissioning.cpp index dcce1ab81683c2..1026105160ffbb 100644 --- a/src/app/clusters/network-commissioning/network-commissioning.cpp +++ b/src/app/clusters/network-commissioning/network-commissioning.cpp @@ -542,62 +542,67 @@ void Instance::OnFinished(Status status, CharSpan debugText, ThreadScanResponseI SuccessOrExit(err = writer->StartContainer(TLV::ContextTag(Commands::ScanNetworksResponse::Fields::kThreadScanResults), TLV::TLVType::kTLVType_Array, listContainerType)); - VerifyOrExit(scanResponseArray.Alloc(chip::min(networks->Count(), kMaxNetworksInScanResponse)), err = CHIP_ERROR_NO_MEMORY); - for (; networks != nullptr && networks->Next(scanResponse);) + // If no network was found, we encode an empty list, don't call a zero-sized alloc. + if (networks->Count() > 0) { - if ((scanResponseArrayLength == kMaxNetworksInScanResponse) && - (scanResponseArray[scanResponseArrayLength - 1].rssi > scanResponse.rssi)) + VerifyOrExit(scanResponseArray.Alloc(chip::min(networks->Count(), kMaxNetworksInScanResponse)), err = CHIP_ERROR_NO_MEMORY); + for (; networks != nullptr && networks->Next(scanResponse);) { - continue; - } + if ((scanResponseArrayLength == kMaxNetworksInScanResponse) && + (scanResponseArray[scanResponseArrayLength - 1].rssi > scanResponse.rssi)) + { + continue; + } - bool isDuplicated = false; + bool isDuplicated = false; - for (size_t i = 0; i < scanResponseArrayLength; i++) - { - if ((scanResponseArray[i].panId == scanResponse.panId) && - (scanResponseArray[i].extendedPanId == scanResponse.extendedPanId)) + for (size_t i = 0; i < scanResponseArrayLength; i++) { - if (scanResponseArray[i].rssi < scanResponse.rssi) + if ((scanResponseArray[i].panId == scanResponse.panId) && + (scanResponseArray[i].extendedPanId == scanResponse.extendedPanId)) { - scanResponseArray[i] = scanResponseArray[--scanResponseArrayLength]; + if (scanResponseArray[i].rssi < scanResponse.rssi) + { + scanResponseArray[i] = scanResponseArray[--scanResponseArrayLength]; + } + else + { + isDuplicated = true; + } + break; } - else - { - isDuplicated = true; - } - break; } - } - if (isDuplicated) - { - continue; + if (isDuplicated) + { + continue; + } + + if (scanResponseArrayLength < kMaxNetworksInScanResponse) + { + scanResponseArrayLength++; + } + scanResponseArray[scanResponseArrayLength - 1] = scanResponse; + Sorting::InsertionSort( + scanResponseArray.Get(), scanResponseArrayLength, + [](const ThreadScanResponse & a, const ThreadScanResponse & b) -> bool { return a.rssi > b.rssi; }); } - if (scanResponseArrayLength < kMaxNetworksInScanResponse) + for (size_t i = 0; i < scanResponseArrayLength; i++) { - scanResponseArrayLength++; + Structs::ThreadInterfaceScanResultStruct::Type result; + Encoding::BigEndian::Put64(extendedAddressBuffer, scanResponseArray[i].extendedAddress); + result.panId = scanResponseArray[i].panId; + result.extendedPanId = scanResponseArray[i].extendedPanId; + result.networkName = CharSpan(scanResponseArray[i].networkName, scanResponseArray[i].networkNameLen); + result.channel = scanResponseArray[i].channel; + result.version = scanResponseArray[i].version; + result.extendedAddress = ByteSpan(extendedAddressBuffer); + result.rssi = scanResponseArray[i].rssi; + result.lqi = scanResponseArray[i].lqi; + + SuccessOrExit(err = DataModel::Encode(*writer, TLV::AnonymousTag(), result)); } - scanResponseArray[scanResponseArrayLength - 1] = scanResponse; - Sorting::InsertionSort(scanResponseArray.Get(), scanResponseArrayLength, - [](const ThreadScanResponse & a, const ThreadScanResponse & b) -> bool { return a.rssi > b.rssi; }); - } - - for (size_t i = 0; i < scanResponseArrayLength; i++) - { - Structs::ThreadInterfaceScanResultStruct::Type result; - Encoding::BigEndian::Put64(extendedAddressBuffer, scanResponseArray[i].extendedAddress); - result.panId = scanResponseArray[i].panId; - result.extendedPanId = scanResponseArray[i].extendedPanId; - result.networkName = CharSpan(scanResponseArray[i].networkName, scanResponseArray[i].networkNameLen); - result.channel = scanResponseArray[i].channel; - result.version = scanResponseArray[i].version; - result.extendedAddress = ByteSpan(extendedAddressBuffer); - result.rssi = scanResponseArray[i].rssi; - result.lqi = scanResponseArray[i].lqi; - - SuccessOrExit(err = DataModel::Encode(*writer, TLV::AnonymousTag(), result)); } SuccessOrExit(err = writer->EndContainer(listContainerType));