If a network has been compromised (e.g. someone is listening on the traffic or there has been malware implemented on it), is it safe to leave a device (PC, cell phone, gaming device, etc.) in range of the network, as long as you don't connect to it? Should I disable WiFi when I don't need it to avoid this risk?
If your device isn't connected to the network, either because WiFi is turned off or because your device isn't configured to automatically connect to the network, nothing on that network can affect your device.
However, an attacker may create a network with the same SSID and PSK as one that your device recognizes, such as the WiFi network used at your workplace. Your device will likely connect automatically in that situation.
To mitigate this risk, assume all networks have something listening, even if it is just the router itself. Make sure all your network traffic is encrypted using a VPN.
In short, there is no risk when your device is not connected to the network unless the hacker changes the SSID and password of the WiFi to one of the saved (and enabled auto-connect) ones in your device; which is rare, especially when the hacker has not targeted you.
But to be on the safe side:
- forget (remove) all saved WiFi on your device,
- use VPN for any sensitive activity, e.g. banking stuff,
- be sure about certificates; using Chrome or Firefox will assure you fairly if it is forged or not,
- Having some tools like antivirus and firewall may help.
Generally, scanning for WiFi networks is done passively by your device. This means that a router broadcasts information (such as the name of the network, authentication method etc.) and your device listens. These networks show up in a list which you can chose to connect to, but the router does not know your device is there until it tries to connect. So being in the area of a compromised network will not affect your device.
Someone could spoof a network you are trying to connect to, but they cannot read a list of networks your device likes to connect to and spoof them. They would have to spoof a nearby network, guessing that you will want connect to it. The authentication protocol should then stop you connecting to the spoofed network. Unless the network doesn't have one, in which case you should not set it to connect automatically and use a VPN.