When running this code in chromium on a machine with IPv6 disabled, the RTC_DCHECK fails and in release build, it could leak to further crash in chromium's rtc_peer_connection_hanlder.cc.

Here is the right fix.

BUG=webrtc:5061
R=pthatcher@google.com
TBR=pthatcher@webrtc.org

Review URL: https://codereview.webrtc.org/1437933002 .

Cr-Commit-Position: refs/heads/master@{#10607}
diff --git a/webrtc/base/network.h b/webrtc/base/network.h
index ccd205e..8980b5d 100644
--- a/webrtc/base/network.h
+++ b/webrtc/base/network.h
@@ -58,7 +58,8 @@
  public:
   virtual ~DefaultLocalAddressProvider() = default;
   // The default local address is the local address used in multi-homed endpoint
-  // when the any address (0.0.0.0 or ::) is used as the local address.
+  // when the any address (0.0.0.0 or ::) is used as the local address. It's
+  // important to check the return value as a IP family may not be enabled.
   virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
 };
 
diff --git a/webrtc/p2p/base/stunport.cc b/webrtc/p2p/base/stunport.cc
index ab8b2bf..13ecca4 100644
--- a/webrtc/p2p/base/stunport.cc
+++ b/webrtc/p2p/base/stunport.cc
@@ -306,6 +306,9 @@
   // |emit_local_for_anyaddress| is true. This is to allow connectivity for
   // applications which absolutely requires a HOST candidate.
   rtc::SocketAddress addr = address;
+
+  // If MaybeSetDefaultLocalAddress fails, we keep the "any" IP so that at
+  // least the port is listening.
   MaybeSetDefaultLocalAddress(&addr);
 
   AddAddress(addr, addr, rtc::SocketAddress(), UDP_PROTOCOL_NAME, "", "",
@@ -404,17 +407,21 @@
   }
 }
 
-void UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const {
+bool UDPPort::MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const {
   if (!addr->IsAnyIP() || !emit_local_for_anyaddress_ ||
       !Network()->default_local_address_provider()) {
-    return;
+    return true;
   }
   rtc::IPAddress default_address;
   bool result =
       Network()->default_local_address_provider()->GetDefaultLocalAddress(
           addr->family(), &default_address);
-  RTC_DCHECK(result && !default_address.IsNil());
+  if (!result || default_address.IsNil()) {
+    return false;
+  }
+
   addr->SetIP(default_address);
+  return true;
 }
 
 void UDPPort::OnStunBindingRequestSucceeded(
@@ -434,8 +441,9 @@
       !HasCandidateWithAddress(stun_reflected_addr)) {
 
     rtc::SocketAddress related_address = socket_->GetLocalAddress();
-    MaybeSetDefaultLocalAddress(&related_address);
-    if (!(candidate_filter() & CF_HOST)) {
+    // If we can't stamp the related address correctly, empty it to avoid leak.
+    if (!MaybeSetDefaultLocalAddress(&related_address) ||
+        !(candidate_filter() & CF_HOST)) {
       // If candidate filter doesn't have CF_HOST specified, empty raddr to
       // avoid local address leakage.
       related_address = rtc::EmptySocketAddressWithFamily(
diff --git a/webrtc/p2p/base/stunport.h b/webrtc/p2p/base/stunport.h
index 6e7a239..ccd3f36 100644
--- a/webrtc/p2p/base/stunport.h
+++ b/webrtc/p2p/base/stunport.h
@@ -149,8 +149,10 @@
   void SendStunBindingRequests();
 
   // Helper function which will set |addr|'s IP to the default local address if
-  // |addr| is the "any" address and |emit_local_for_anyaddress_| is true.
-  void MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const;
+  // |addr| is the "any" address and |emit_local_for_anyaddress_| is true. When
+  // returning false, it indicates that the operation has failed and the
+  // address shouldn't be used by any candidate.
+  bool MaybeSetDefaultLocalAddress(rtc::SocketAddress* addr) const;
 
  private:
   // A helper class which can be called repeatedly to resolve multiple