Program: 106156
Project: 106156
Buggy source:
@Override public Answer copyTemplateToPrimaryStorage(CopyCommand cmd){
  DataTO srcData=cmd.getSrcTO();
  DataTO destData=cmd.getDestTO();
  TemplateObjectTO template=(TemplateObjectTO)srcData;
  DataStoreTO imageStore=template.getDataStore();
  PrimaryDataStoreTO primaryStore=(PrimaryDataStoreTO)destData.getDataStore();
  if (!(imageStore instanceof NfsTO)) {
    return new CopyCmdAnswer("String_Node_Str");
  }
  NfsTO nfsImageStore=(NfsTO)imageStore;
  String tmplturl=nfsImageStore.getUrl() + File.separator + template.getPath();
  int index=tmplturl.lastIndexOf("String_Node_Str");
  String mountpoint=tmplturl.substring(0,index);
  String tmpltname=null;
  if (index < tmplturl.length() - 1) {
    tmpltname=tmplturl.substring(index + 1);
  }
  KVMPhysicalDisk tmplVol=null;
  KVMStoragePool secondaryPool=null;
  try {
    secondaryPool=storagePoolMgr.getStoragePoolByURI(mountpoint);
    if (tmpltname == null) {
      secondaryPool.refresh();
      List<KVMPhysicalDisk> disks=secondaryPool.listPhysicalDisks();
      if (disks == null || disks.isEmpty()) {
        return new PrimaryStorageDownloadAnswer("String_Node_Str" + secondaryPool.getUuid());
      }
      for (      KVMPhysicalDisk disk : disks) {
        if (disk.getName().endsWith("String_Node_Str")) {
          tmplVol=disk;
          break;
        }
      }
      if (tmplVol == null) {
        return new PrimaryStorageDownloadAnswer("String_Node_Str" + secondaryPool.getUuid());
      }
    }
 else {
      tmplVol=secondaryPool.getPhysicalDisk(tmpltname);
    }
    KVMStoragePool primaryPool=storagePoolMgr.getStoragePool(primaryStore.getPoolType(),primaryStore.getUuid());
    KVMPhysicalDisk primaryVol=storagePoolMgr.copyPhysicalDisk(tmplVol,UUID.randomUUID().toString(),primaryPool);
    DataTO data=null;
    if (destData.getObjectType() == DataObjectType.TEMPLATE) {
      TemplateObjectTO newTemplate=new TemplateObjectTO();
      newTemplate.setPath(primaryVol.getName());
      if (primaryPool.getType() == StoragePoolType.RBD) {
        newTemplate.setFormat(ImageFormat.RAW);
      }
 else {
        newTemplate.setFormat(ImageFormat.QCOW2);
      }
      data=newTemplate;
    }
 else     if (destData.getObjectType() == DataObjectType.VOLUME) {
      VolumeObjectTO volumeObjectTO=new VolumeObjectTO();
      volumeObjectTO.setPath(primaryVol.getName());
      if (primaryVol.getFormat() == PhysicalDiskFormat.RAW)       volumeObjectTO.setFormat(ImageFormat.RAW);
 else       if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
        volumeObjectTO.setFormat(ImageFormat.QCOW2);
      }
      data=volumeObjectTO;
    }
    return new CopyCmdAnswer(data);
  }
 catch (  CloudRuntimeException e) {
    return new CopyCmdAnswer(e.toString());
  }
 finally {
    if (secondaryPool != null) {
      secondaryPool.delete();
    }
  }
}

Fixed version (ground truth):
@Override public Answer copyTemplateToPrimaryStorage(CopyCommand cmd){
  DataTO srcData=cmd.getSrcTO();
  DataTO destData=cmd.getDestTO();
  TemplateObjectTO template=(TemplateObjectTO)srcData;
  DataStoreTO imageStore=template.getDataStore();
  PrimaryDataStoreTO primaryStore=(PrimaryDataStoreTO)destData.getDataStore();
  if (!(imageStore instanceof NfsTO)) {
    return new CopyCmdAnswer("String_Node_Str");
  }
  NfsTO nfsImageStore=(NfsTO)imageStore;
  String tmplturl=nfsImageStore.getUrl() + File.separator + template.getPath();
  int index=tmplturl.lastIndexOf("String_Node_Str");
  String mountpoint=tmplturl.substring(0,index);
  String tmpltname=null;
  if (index < tmplturl.length() - 1) {
    tmpltname=tmplturl.substring(index + 1);
  }
  KVMPhysicalDisk tmplVol=null;
  KVMStoragePool secondaryPool=null;
  try {
    secondaryPool=storagePoolMgr.getStoragePoolByURI(mountpoint);
    if (tmpltname == null) {
      secondaryPool.refresh();
      List<KVMPhysicalDisk> disks=secondaryPool.listPhysicalDisks();
      if (disks == null || disks.isEmpty()) {
        return new PrimaryStorageDownloadAnswer("String_Node_Str" + secondaryPool.getUuid());
      }
      for (      KVMPhysicalDisk disk : disks) {
        if (disk.getName().endsWith("String_Node_Str")) {
          tmplVol=disk;
          break;
        }
      }
      if (tmplVol == null) {
        return new PrimaryStorageDownloadAnswer("String_Node_Str" + secondaryPool.getUuid());
      }
    }
 else {
      tmplVol=secondaryPool.getPhysicalDisk(tmpltname);
    }
    KVMStoragePool primaryPool=storagePoolMgr.getStoragePool(primaryStore.getPoolType(),primaryStore.getUuid());
    KVMPhysicalDisk primaryVol=storagePoolMgr.copyPhysicalDisk(tmplVol,UUID.randomUUID().toString(),primaryPool,cmd.getWaitInMillSeconds());
    DataTO data=null;
    if (destData.getObjectType() == DataObjectType.TEMPLATE) {
      TemplateObjectTO newTemplate=new TemplateObjectTO();
      newTemplate.setPath(primaryVol.getName());
      if (primaryPool.getType() == StoragePoolType.RBD) {
        newTemplate.setFormat(ImageFormat.RAW);
      }
 else {
        newTemplate.setFormat(ImageFormat.QCOW2);
      }
      data=newTemplate;
    }
 else     if (destData.getObjectType() == DataObjectType.VOLUME) {
      VolumeObjectTO volumeObjectTO=new VolumeObjectTO();
      volumeObjectTO.setPath(primaryVol.getName());
      if (primaryVol.getFormat() == PhysicalDiskFormat.RAW)       volumeObjectTO.setFormat(ImageFormat.RAW);
 else       if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
        volumeObjectTO.setFormat(ImageFormat.QCOW2);
      }
      data=volumeObjectTO;
    }
    return new CopyCmdAnswer(data);
  }
 catch (  CloudRuntimeException e) {
    return new CopyCmdAnswer(e.toString());
  }
 finally {
    if (secondaryPool != null) {
      secondaryPool.delete();
    }
  }
}