diff --git a/services/service/src/softbus/softbus_listener.cpp b/services/service/src/softbus/softbus_listener.cpp index 565f8a932716f499f95b81c1a7a6dac8d7b7d041..8f0e5dcff64e727c168d4cd472e049c5f01898a9 100644 --- a/services/service/src/softbus/softbus_listener.cpp +++ b/services/service/src/softbus/softbus_listener.cpp @@ -47,6 +47,7 @@ const int32_t SOFTBUS_SUBSCRIBE_ID_MASK = 0x0000FFFF; const int32_t MAX_CACHED_DISCOVERED_DEVICE_SIZE = 100; const int32_t MAX_SOFTBUS_MSG_LEN = 2000; const int32_t MAX_OSTYPE_SIZE = 1000; +constexpr int32_t MAX_CACHED_MAP_NUM = 5000; #if (defined(__LITEOS_M__) || defined(LITE_DEVICE)) constexpr const char* DEVICE_ONLINE = "deviceOnLine"; constexpr const char* DEVICE_OFFLINE = "deviceOffLine"; @@ -494,6 +495,10 @@ void SoftbusListener::OnSoftbusDeviceFound(const DeviceInfo *device) { std::lock_guard lock(g_lockDeviceIdSet); if (deviceIdSet.find(std::string(dmDevInfo.deviceId)) == deviceIdSet.end()) { + if (deviceIdSet.size() >= MAX_CACHED_MAP_NUM) { + LOGE("deviceIdSet size exceed the limit!"); + deviceIdSet.erase(deviceIdSet.begin()); + } deviceIdSet.insert(std::string(dmDevInfo.deviceId)); struct RadarInfo info = { .funcName = "OnSoftbusDeviceFound", @@ -519,6 +524,10 @@ void SoftbusListener::OnSoftbusDeviceFound(const DeviceInfo *device) return; } std::lock_guard lock(g_lnnCbkMapMutex); + if (discoveredDeviceActionIdMap.size() >= MAX_CACHED_MAP_NUM) { + LOGE("discoveredDeviceActionIdMap size exceed the limit!"); + discoveredDeviceActionIdMap.erase(discoveredDeviceActionIdMap.begin()); + } discoveredDeviceActionIdMap[dmDevInfo.deviceId] = actionId; CacheDiscoveredDevice(device); for (auto &iter : lnnOpsCbkMap) { @@ -745,6 +754,10 @@ int32_t SoftbusListener::RegisterSoftbusLnnOpsCbk(const std::string &pkgName, return ERR_DM_POINT_NULL; } std::lock_guard lock(g_lnnCbkMapMutex); + if (lnnOpsCbkMap.size() >= MAX_CACHED_MAP_NUM) { + LOGE("lnnOpsCbkMap size exceed the limit!"); + return ERR_DM_FAILED; + } lnnOpsCbkMap.erase(pkgName); lnnOpsCbkMap.emplace(pkgName, callback); return DM_OK;