Program: 15482
Project: 15482
Buggy source:
private static void restoreInstance(final String cluster,final VmInfo runVm){
  try {
    String instanceId=runVm.getInstanceId();
    String reservationId=runVm.getReservationId();
    UserFullName ownerId=Accounts.lookupUserFullNameById(runVm.getOwnerId());
    String placement=cluster;
    byte[] userData=new byte[0];
    if (runVm.getUserData() != null && runVm.getUserData().length() > 1) {
      userData=Base64.decode(runVm.getUserData());
    }
    Integer launchIndex=0;
    try {
      launchIndex=Integer.parseInt(runVm.getLaunchIndex());
    }
 catch (    NumberFormatException e) {
    }
    ImageInfo img=Transactions.one(Images.exampleMachineWithImageId(runVm.getInstanceType().lookupRoot().getId()),Tx.NOOP);
    VmKeyInfo keyInfo=null;
    SshKeyPair key=null;
    if (runVm.getKeyValue() != null || !"String_Node_Str".equals(runVm.getKeyValue())) {
      try {
        SshKeyPair searchKey=EntityWrapper.get(SshKeyPair.class).lookupAndClose(new SshKeyPair(ownerId){
{
            setPublicKey(runVm.getKeyValue());
          }
        }
);
      }
 catch (      Exception e) {
        key=SshKeyPair.NO_KEY;
      }
    }
 else {
      key=SshKeyPair.NO_KEY;
    }
    keyInfo=new VmKeyInfo(key.getDisplayName(),key.getPublicKey(),key.getFingerPrint());
    VmTypeInfo vmType=runVm.getInstanceType();
    List<Network> networks=new ArrayList<Network>();
    for (    String netName : runVm.getGroupNames()) {
      Network notwork=null;
      try {
        notwork=Networks.getInstance().lookup(runVm.getOwnerId() + "String_Node_Str" + netName);
        networks.add(notwork);
        try {
          NetworkToken netToken=Clusters.getInstance().lookup(runVm.getPlacement()).getState().extantAllocation(runVm.getOwnerId(),netName,notwork.getUuid(),runVm.getNetParams().getVlan());
          notwork.addTokenIfAbsent(netToken);
        }
 catch (        NetworkAlreadyExistsException e) {
          LOG.trace(e);
        }
        notwork.extantNetworkIndex(runVm.getPlacement(),runVm.getNetParams().getNetworkIndex());
      }
 catch (      NoSuchElementException e1) {
        try {
          networks.add(notwork);
          NetworkToken netToken=Clusters.getInstance().lookup(runVm.getPlacement()).getState().extantAllocation(runVm.getOwnerId(),netName,notwork.getUuid(),runVm.getNetParams().getVlan());
          notwork.addTokenIfAbsent(netToken);
          Networks.getInstance().registerIfAbsent(notwork,Networks.State.ACTIVE);
        }
 catch (        NetworkAlreadyExistsException e) {
          LOG.trace(e);
        }
      }
    }
    VmInstance vm=new VmInstance(reservationId,launchIndex,instanceId,ownerId,placement,userData,keyInfo,vmType,img.getPlatform().toString(),networks,Integer.toString(runVm.getNetParams().getNetworkIndex()));
    vm.clearPending();
    vm.setLaunchTime(runVm.getLaunchTime());
    vm.updatePublicAddress(VmInstance.DEFAULT_IP);
    vm.setKeyInfo(keyInfo);
    VmInstances.getInstance().register(vm);
  }
 catch (  NoSuchElementException e) {
    ClusterConfiguration config=Clusters.getInstance().lookup(runVm.getPlacement()).getConfiguration();
    AsyncRequests.newRequest(new TerminateCallback(runVm.getInstanceId())).dispatch(runVm.getPlacement());
  }
catch (  Throwable t) {
    LOG.error(t,t);
  }
}

Fixed version (ground truth):
private static void restoreInstance(final String cluster,final VmInfo runVm){
  try {
    String instanceId=runVm.getInstanceId();
    String reservationId=runVm.getReservationId();
    UserFullName ownerId=Accounts.lookupUserFullNameById(runVm.getOwnerId());
    String placement=cluster;
    byte[] userData=new byte[0];
    if (runVm.getUserData() != null && runVm.getUserData().length() > 1) {
      userData=Base64.decode(runVm.getUserData());
    }
    Integer launchIndex=0;
    try {
      launchIndex=Integer.parseInt(runVm.getLaunchIndex());
    }
 catch (    NumberFormatException e) {
    }
    ImageInfo img=Transactions.one(Images.exampleMachineWithImageId(runVm.getInstanceType().lookupRoot().getId()),Tx.NOOP);
    VmKeyInfo keyInfo=null;
    SshKeyPair key=null;
    if (runVm.getKeyValue() != null || !"String_Node_Str".equals(runVm.getKeyValue())) {
      try {
        SshKeyPair searchKey=EntityWrapper.get(SshKeyPair.class).lookupAndClose(new SshKeyPair(ownerId){
{
            setPublicKey(runVm.getKeyValue());
          }
        }
);
      }
 catch (      Exception e) {
        key=SshKeyPair.NO_KEY;
      }
    }
 else {
      key=SshKeyPair.NO_KEY;
    }
    keyInfo=new VmKeyInfo(key.getDisplayName(),key.getPublicKey(),key.getFingerPrint());
    VmTypeInfo vmType=runVm.getInstanceType();
    List<Network> networks=new ArrayList<Network>();
    for (    String netName : runVm.getGroupNames()) {
      Network notwork=null;
      try {
        notwork=Networks.getInstance().lookup(runVm.getOwnerId() + "String_Node_Str" + netName);
        networks.add(notwork);
        try {
          NetworkToken netToken=Clusters.getInstance().lookup(runVm.getPlacement()).getState().extantAllocation(runVm.getOwnerId(),netName,notwork.getUuid(),runVm.getNetParams().getVlan());
          notwork.addTokenIfAbsent(netToken);
        }
 catch (        NetworkAlreadyExistsException e) {
          LOG.trace(e);
        }
        notwork.extantNetworkIndex(runVm.getPlacement(),runVm.getNetParams().getNetworkIndex());
      }
 catch (      NoSuchElementException e1) {
        try {
          try {
            notwork=NetworkGroupUtil.getUserNetworkRulesGroup(Accounts.lookupAccountFullNameByUserId(runVm.getOwnerId()),netName).getVmNetwork();
          }
 catch (          Exception e) {
            notwork=NetworkGroupUtil.getUserNetworkRulesGroup(Accounts.lookupAccountFullNameByUserId(runVm.getOwnerId()),"String_Node_Str").getVmNetwork();
          }
          networks.add(notwork);
          NetworkToken netToken=Clusters.getInstance().lookup(runVm.getPlacement()).getState().extantAllocation(runVm.getOwnerId(),netName,notwork.getUuid(),runVm.getNetParams().getVlan());
          notwork.addTokenIfAbsent(netToken);
          Networks.getInstance().registerIfAbsent(notwork,Networks.State.ACTIVE);
        }
 catch (        NetworkAlreadyExistsException e) {
          LOG.trace(e);
        }
      }
    }
    VmInstance vm=new VmInstance(reservationId,launchIndex,instanceId,ownerId,placement,userData,keyInfo,vmType,img.getPlatform().toString(),networks,Integer.toString(runVm.getNetParams().getNetworkIndex()));
    vm.clearPending();
    vm.setLaunchTime(runVm.getLaunchTime());
    vm.updatePublicAddress(VmInstance.DEFAULT_IP);
    vm.setKeyInfo(keyInfo);
    VmInstances.getInstance().register(vm);
  }
 catch (  NoSuchElementException e) {
    ClusterConfiguration config=Clusters.getInstance().lookup(runVm.getPlacement()).getConfiguration();
    AsyncRequests.newRequest(new TerminateCallback(runVm.getInstanceId())).dispatch(runVm.getPlacement());
  }
catch (  Throwable t) {
    LOG.error(t,t);
  }
}