Program: 103347
Project: 103347
Buggy source:
/** 
 * Checks current interfaces configuration against the last saved active interfaces.
 * @param fireEvents whether we will fire events when we detectthat interface is changed. When we start we query the interfaces just to check which are online, without firing events.
 * @param waitBeforeFiringUpEvents milliseconds to wait beforefiring events for interfaces up, sometimes we must wait a little bit and give time for interfaces to configure fully (dns on linux).
 */
private void checkNetworkInterfaces(boolean fireEvents,int waitBeforeFiringUpEvents) throws SocketException {
  Enumeration<NetworkInterface> e=NetworkInterface.getNetworkInterfaces();
  Map<String,List<InetAddress>> currentActiveInterfaces=new HashMap<String,List<InetAddress>>();
  while (e.hasMoreElements()) {
    NetworkInterface networkInterface=e.nextElement();
    if (isInterfaceLoopback(networkInterface))     continue;
    if (isInterfaceUp(networkInterface)) {
      List<InetAddress> addresses=new ArrayList<InetAddress>();
      Enumeration<InetAddress> as=networkInterface.getInetAddresses();
      while (as.hasMoreElements()) {
        InetAddress inetAddress=as.nextElement();
        if (inetAddress.isLinkLocalAddress())         continue;
        addresses.add(inetAddress);
      }
      if (addresses.size() > 0)       currentActiveInterfaces.put(networkInterface.getName(),addresses);
    }
  }
  List<String> inactiveActiveInterfaces=new ArrayList<String>(activeInterfaces.keySet());
  List<String> currentActiveInterfacesSet=new ArrayList<String>(currentActiveInterfaces.keySet());
  inactiveActiveInterfaces.removeAll(currentActiveInterfacesSet);
  for (int i=0; i < inactiveActiveInterfaces.size(); i++) {
    String iface=inactiveActiveInterfaces.get(i);
    if (!currentActiveInterfacesSet.contains(iface)) {
      if (fireEvents)       fireChangeEvent(new ChangeEvent(iface,ChangeEvent.IFACE_DOWN));
      activeInterfaces.remove(iface);
    }
  }
  Iterator<Map.Entry<String,List<InetAddress>>> activeEntriesIter=activeInterfaces.entrySet().iterator();
  while (activeEntriesIter.hasNext()) {
    Map.Entry<String,List<InetAddress>> entry=activeEntriesIter.next();
    Iterator<InetAddress> addrIter=entry.getValue().iterator();
    while (addrIter.hasNext()) {
      InetAddress addr=addrIter.next();
      List<InetAddress> addresses=currentActiveInterfaces.get(entry.getKey());
      if (addresses != null && !addresses.contains(addr)) {
        if (fireEvents)         fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.ADDRESS_DOWN,addr));
        addrIter.remove();
      }
    }
  }
  if (waitBeforeFiringUpEvents > 0 && currentActiveInterfaces.size() != 0) {
synchronized (this) {
      try {
        wait(1000);
      }
 catch (      InterruptedException ex) {
      }
    }
  }
  activeEntriesIter=currentActiveInterfaces.entrySet().iterator();
  while (activeEntriesIter.hasNext()) {
    Map.Entry<String,List<InetAddress>> entry=activeEntriesIter.next();
    for (    InetAddress addr : entry.getValue()) {
      List<InetAddress> addresses=activeInterfaces.get(entry.getKey());
      if (addresses != null && !addresses.contains(addr)) {
        if (fireEvents)         fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.ADDRESS_UP,addr));
        addresses.add(addr);
      }
    }
  }
  Iterator<String> ifaceIter=activeInterfaces.keySet().iterator();
  while (ifaceIter.hasNext()) {
    currentActiveInterfaces.remove(ifaceIter.next());
  }
  activeEntriesIter=currentActiveInterfaces.entrySet().iterator();
  while (activeEntriesIter.hasNext()) {
    Map.Entry<String,List<InetAddress>> entry=activeEntriesIter.next();
    for (    InetAddress addr : entry.getValue()) {
      if (fireEvents)       fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.ADDRESS_UP,addr));
    }
    if (fireEvents)     fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.IFACE_UP));
    activeInterfaces.put(entry.getKey(),entry.getValue());
  }
}

Fixed version (ground truth):
/** 
 * Checks current interfaces configuration against the last saved active interfaces.
 * @param fireEvents whether we will fire events when we detectthat interface is changed. When we start we query the interfaces just to check which are online, without firing events.
 * @param waitBeforeFiringUpEvents milliseconds to wait beforefiring events for interfaces up, sometimes we must wait a little bit and give time for interfaces to configure fully (dns on linux).
 */
private void checkNetworkInterfaces(boolean fireEvents,int waitBeforeFiringUpEvents) throws SocketException {
  Enumeration<NetworkInterface> e=NetworkInterface.getNetworkInterfaces();
  Map<String,List<InetAddress>> currentActiveInterfaces=new HashMap<String,List<InetAddress>>();
  while (e.hasMoreElements()) {
    NetworkInterface networkInterface=e.nextElement();
    if (isInterfaceLoopback(networkInterface))     continue;
    if (isInterfaceUp(networkInterface)) {
      List<InetAddress> addresses=new ArrayList<InetAddress>();
      Enumeration<InetAddress> as=networkInterface.getInetAddresses();
      while (as.hasMoreElements()) {
        InetAddress inetAddress=as.nextElement();
        if (inetAddress.isLinkLocalAddress())         continue;
        addresses.add(inetAddress);
      }
      if (addresses.size() > 0)       currentActiveInterfaces.put(networkInterface.getName(),addresses);
    }
  }
  List<String> inactiveActiveInterfaces=new ArrayList<String>(activeInterfaces.keySet());
  List<String> currentActiveInterfacesSet=new ArrayList<String>(currentActiveInterfaces.keySet());
  inactiveActiveInterfaces.removeAll(currentActiveInterfacesSet);
  for (int i=0; i < inactiveActiveInterfaces.size(); i++) {
    String iface=inactiveActiveInterfaces.get(i);
    if (!currentActiveInterfacesSet.contains(iface)) {
      if (fireEvents)       eventDispatcher.fireChangeEvent(new ChangeEvent(iface,ChangeEvent.IFACE_DOWN));
      activeInterfaces.remove(iface);
    }
  }
  Iterator<Map.Entry<String,List<InetAddress>>> activeEntriesIter=activeInterfaces.entrySet().iterator();
  while (activeEntriesIter.hasNext()) {
    Map.Entry<String,List<InetAddress>> entry=activeEntriesIter.next();
    Iterator<InetAddress> addrIter=entry.getValue().iterator();
    while (addrIter.hasNext()) {
      InetAddress addr=addrIter.next();
      List<InetAddress> addresses=currentActiveInterfaces.get(entry.getKey());
      if (addresses != null && !addresses.contains(addr)) {
        if (fireEvents)         eventDispatcher.fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.ADDRESS_DOWN,addr));
        addrIter.remove();
      }
    }
  }
  if (waitBeforeFiringUpEvents > 0 && currentActiveInterfaces.size() != 0) {
synchronized (this) {
      try {
        wait(waitBeforeFiringUpEvents);
      }
 catch (      InterruptedException ex) {
      }
    }
  }
  activeEntriesIter=currentActiveInterfaces.entrySet().iterator();
  while (activeEntriesIter.hasNext()) {
    Map.Entry<String,List<InetAddress>> entry=activeEntriesIter.next();
    for (    InetAddress addr : entry.getValue()) {
      List<InetAddress> addresses=activeInterfaces.get(entry.getKey());
      if (addresses != null && !addresses.contains(addr)) {
        if (fireEvents)         eventDispatcher.fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.ADDRESS_UP,addr));
        addresses.add(addr);
      }
    }
  }
  Iterator<String> ifaceIter=activeInterfaces.keySet().iterator();
  while (ifaceIter.hasNext()) {
    currentActiveInterfaces.remove(ifaceIter.next());
  }
  activeEntriesIter=currentActiveInterfaces.entrySet().iterator();
  while (activeEntriesIter.hasNext()) {
    final Map.Entry<String,List<InetAddress>> entry=activeEntriesIter.next();
    for (    InetAddress addr : entry.getValue()) {
      if (fireEvents)       eventDispatcher.fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.ADDRESS_UP,addr));
    }
    if (fireEvents) {
      int wait=waitBeforeFiringUpEvents;
      if (wait == 0) {
        wait=500;
      }
      eventDispatcher.fireChangeEvent(new ChangeEvent(entry.getKey(),ChangeEvent.IFACE_UP),wait);
    }
    activeInterfaces.put(entry.getKey(),entry.getValue());
  }
}