diff --git a/src/core/hle/service/ldn/lan_discovery.cpp b/src/core/hle/service/ldn/lan_discovery.cpp index 237f4ba0e..a44a00d88 100644 --- a/src/core/hle/service/ldn/lan_discovery.cpp +++ b/src/core/hle/service/ldn/lan_discovery.cpp @@ -77,6 +77,8 @@ void LANDiscovery::SetState(State new_state) { } Result LANDiscovery::GetNetworkInfo(NetworkInfo& out_network) const { + // Lock must be held while copying to prevent the Network thread from corrupting the copy + std::scoped_lock lock{packet_mutex}; if (state == State::AccessPointCreated || state == State::StationConnected) { std::memcpy(&out_network, &network_info, sizeof(network_info)); return ResultSuccess; @@ -117,7 +119,7 @@ Result LANDiscovery::Scan(std::span out_networks, s16& out_count, } LOG_INFO(Service_LDN, "Waiting for scan replies"); - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::scoped_lock lock{packet_mutex}; for (const auto& [key, info] : scan_results) { @@ -567,10 +569,19 @@ void LANDiscovery::ReceivePacket(const Network::LDNPacket& packet) { } case Network::LDNPacketType::SyncNetwork: { if (state == State::StationOpened || state == State::StationConnected) { - LOG_INFO(Frontend, "SyncNetwork packet received!"); + if (packet.data.size() < sizeof(NetworkInfo)) { + LOG_ERROR(Service_LDN, "SyncNetwork packet too small!"); + break; + } + NetworkInfo info{}; std::memcpy(&info, packet.data.data(), sizeof(NetworkInfo)); + if (info.ldn.node_count == 0) { + LOG_ERROR(Service_LDN, "SyncNetwork reported 0 nodes! Ignoring to prevent SDK crash."); + break; + } + OnSyncNetwork(info); } else { LOG_INFO(Frontend, "SyncNetwork packet received but in wrong State!"); diff --git a/src/core/hle/service/ldn/lan_discovery.h b/src/core/hle/service/ldn/lan_discovery.h index 855f58661..ce9d51aac 100644 --- a/src/core/hle/service/ldn/lan_discovery.h +++ b/src/core/hle/service/ldn/lan_discovery.h @@ -112,7 +112,7 @@ protected: static constexpr Ssid fake_ssid{"CitronFakeSsidForLdn"}; bool inited{}; - std::mutex packet_mutex; + mutable std::mutex packet_mutex; std::array stations; std::array node_changes{}; std::array node_last_states{};