Add synchronized to make sure data correctness

Both mNetworkAgent and mConfig are mutable, add synchronized to
make sure the data correctness of those two vars when getNetId()
and getVpnConfig() are called.

Bug: 168576152
Test: atest FrameworkNetTests
      atest CtsHostsideNetworkTests:HostsideVpnTests
Change-Id: I4133de84c904009b5fbf4208853773fc79fdd17f
This commit is contained in:
lucaslin
2020-09-22 20:52:27 +08:00
committed by Lucas Lin
parent ce872e9898
commit bceef6f423

View File

@@ -1128,7 +1128,13 @@ public class Vpn {
return mNetworkInfo;
}
public int getNetId() {
/**
* Return netId of current running VPN network.
*
* @return a netId if there is a running VPN network or NETID_UNSET if there is no running VPN
* network or network is null.
*/
public synchronized int getNetId() {
final NetworkAgent agent = mNetworkAgent;
if (null == agent) return NETID_UNSET;
final Network network = agent.getNetwork();
@@ -1706,7 +1712,7 @@ public class Vpn {
/**
* Return the configuration of the currently running VPN.
*/
public VpnConfig getVpnConfig() {
public synchronized VpnConfig getVpnConfig() {
enforceControlPermission();
return mConfig;
}