Program: 106301
Project: 106301
Buggy source:
protected OptionValue[] configureVnc(OptionValue[] optionsToMerge,VmwareHypervisorHost hyperHost,String vmName,String vncPassword,String keyboardLayout) throws Exception {
  VirtualMachineMO vmMo=hyperHost.findVmOnHyperHost(vmName);
  VmwareManager mgr=hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
  if (!mgr.beginExclusiveOperation(600))   throw new Exception("String_Node_Str");
  try {
    int maxVncPorts=64;
    int vncPort=0;
    Random random=new Random();
    HostMO vmOwnerHost=vmMo.getRunningHost();
    ManagedObjectReference morParent=vmOwnerHost.getParentMor();
    HashMap<String,Integer> portInfo;
    if (morParent.getProtocol().equalsIgnoreCase("String_Node_Str")) {
      ClusterMO clusterMo=new ClusterMO(vmOwnerHost.getContext(),morParent);
      portInfo=clusterMo.getVmVncPortsOnCluster();
    }
 else {
      portInfo=vmOwnerHost.getVmVncPortsOnHost();
    }
    Collection<Integer> existingPorts=portInfo.values();
    int val=random.nextInt(maxVncPorts);
    int startVal=val;
    do {
      if (!existingPorts.contains(5900 + val)) {
        vncPort=5900 + val;
        break;
      }
      val=(++val) % maxVncPorts;
    }
 while (val != startVal);
    if (vncPort == 0) {
      s_logger.info("String_Node_Str");
      Pair<Integer,Integer> additionalRange=mgr.getAddiionalVncPortRange();
      maxVncPorts=additionalRange.second();
      val=random.nextInt(maxVncPorts);
      startVal=val;
      do {
        if (!existingPorts.contains(additionalRange.first() + val)) {
          vncPort=additionalRange.first() + val;
          break;
        }
        val=(++val) % maxVncPorts;
      }
 while (val != startVal);
    }
    if (vncPort == 0) {
      throw new Exception("String_Node_Str");
    }
    if (s_logger.isInfoEnabled()) {
      s_logger.info("String_Node_Str" + vmName + "String_Node_Str"+ vncPort+ "String_Node_Str"+ vmOwnerHost.getHyperHostName());
    }
    return VmwareHelper.composeVncOptions(optionsToMerge,true,vncPassword,vncPort,keyboardLayout);
  }
  finally {
    try {
      mgr.endExclusiveOperation();
    }
 catch (    Throwable e) {
      assert(false);
      s_logger.error("String_Node_Str",e);
    }
  }
}

Fixed version (ground truth):
protected OptionValue[] configureVnc(OptionValue[] optionsToMerge,VmwareHypervisorHost hyperHost,String vmName,String vncPassword,String keyboardLayout) throws Exception {
  VirtualMachineMO vmMo=hyperHost.findVmOnHyperHost(vmName);
  VmwareManager mgr=hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
  if (!mgr.beginExclusiveOperation(600))   throw new Exception("String_Node_Str");
  try {
    int maxVncPorts=64;
    int vncPort=0;
    Random random=new Random();
    HostMO vmOwnerHost=vmMo.getRunningHost();
    ManagedObjectReference morParent=vmOwnerHost.getParentMor();
    HashMap<String,Integer> portInfo;
    if (morParent.getType().equalsIgnoreCase("String_Node_Str")) {
      ClusterMO clusterMo=new ClusterMO(vmOwnerHost.getContext(),morParent);
      portInfo=clusterMo.getVmVncPortsOnCluster();
    }
 else {
      portInfo=vmOwnerHost.getVmVncPortsOnHost();
    }
    Collection<Integer> existingPorts=portInfo.values();
    int val=random.nextInt(maxVncPorts);
    int startVal=val;
    do {
      if (!existingPorts.contains(5900 + val)) {
        vncPort=5900 + val;
        break;
      }
      val=(++val) % maxVncPorts;
    }
 while (val != startVal);
    if (vncPort == 0) {
      s_logger.info("String_Node_Str");
      Pair<Integer,Integer> additionalRange=mgr.getAddiionalVncPortRange();
      maxVncPorts=additionalRange.second();
      val=random.nextInt(maxVncPorts);
      startVal=val;
      do {
        if (!existingPorts.contains(additionalRange.first() + val)) {
          vncPort=additionalRange.first() + val;
          break;
        }
        val=(++val) % maxVncPorts;
      }
 while (val != startVal);
    }
    if (vncPort == 0) {
      throw new Exception("String_Node_Str");
    }
    if (s_logger.isInfoEnabled()) {
      s_logger.info("String_Node_Str" + vmName + "String_Node_Str"+ vncPort+ "String_Node_Str"+ vmOwnerHost.getHyperHostName());
    }
    return VmwareHelper.composeVncOptions(optionsToMerge,true,vncPassword,vncPort,keyboardLayout);
  }
  finally {
    try {
      mgr.endExclusiveOperation();
    }
 catch (    Throwable e) {
      assert(false);
      s_logger.error("String_Node_Str",e);
    }
  }
}