Program: 106515
Project: 106515
Buggy source:
protected void createVbd(Connect conn,VirtualMachineTO vmSpec,String vmName,LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
  List<DiskDef> disks=new ArrayList<DiskDef>();
  for (  VolumeTO volume : vmSpec.getDisks()) {
    KVMPhysicalDisk physicalDisk=null;
    KVMStoragePool pool=null;
    if (volume.getType() == Volume.Type.ISO && volume.getPath() != null) {
      String volPath=volume.getPath();
      int index=volPath.lastIndexOf("String_Node_Str");
      String volDir=volPath.substring(0,index);
      String volName=volPath.substring(index + 1);
      KVMStoragePool secondaryStorage=_storagePoolMgr.getStoragePoolByURI(volDir);
      physicalDisk=secondaryStorage.getPhysicalDisk(volName);
    }
 else     if (volume.getType() != Volume.Type.ISO) {
      pool=_storagePoolMgr.getStoragePool(volume.getPoolUuid());
      physicalDisk=pool.getPhysicalDisk(volume.getPath());
    }
    String volPath=null;
    if (physicalDisk != null) {
      volPath=physicalDisk.getPath();
    }
    DiskDef.diskBus diskBusType=getGuestDiskModel(vmSpec.getOs());
    DiskDef disk=new DiskDef();
    if (volume.getType() == Volume.Type.ISO) {
      if (volPath == null) {
        disk.defISODisk(null);
      }
 else {
        disk.defISODisk(volPath);
      }
    }
 else {
      int devId=(int)volume.getDeviceId();
      if (pool.getType() == StoragePoolType.CLVM) {
        disk.defBlockBasedDisk(physicalDisk.getPath(),devId,diskBusType);
      }
 else {
        if (volume.getType() == Volume.Type.DATADISK) {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,DiskDef.diskBus.VIRTIO,DiskDef.diskFmtType.QCOW2);
        }
 else {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,diskBusType,DiskDef.diskFmtType.QCOW2);
        }
      }
      disks.add(devId,disk);
      continue;
    }
    vm.getDevices().addDevice(disk);
  }
  for (  DiskDef disk : disks) {
    vm.getDevices().addDevice(disk);
  }
  if (vmSpec.getType() != VirtualMachine.Type.User) {
    if (_sysvmISOPath != null) {
      DiskDef iso=new DiskDef();
      iso.defISODisk(_sysvmISOPath);
      vm.getDevices().addDevice(iso);
    }
    createPatchVbd(conn,vmName,vm,vmSpec);
  }
}

Fixed version (ground truth):
protected void createVbd(Connect conn,VirtualMachineTO vmSpec,String vmName,LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException {
  List<VolumeTO> disks=Arrays.asList(vmSpec.getDisks());
  Collections.sort(disks,new Comparator<VolumeTO>(){
    @Override public int compare(    VolumeTO arg0,    VolumeTO arg1){
      return arg0.getDeviceId() > arg1.getDeviceId() ? 1 : -1;
    }
  }
);
  for (  VolumeTO volume : disks) {
    KVMPhysicalDisk physicalDisk=null;
    KVMStoragePool pool=null;
    if (volume.getType() == Volume.Type.ISO && volume.getPath() != null) {
      String volPath=volume.getPath();
      int index=volPath.lastIndexOf("String_Node_Str");
      String volDir=volPath.substring(0,index);
      String volName=volPath.substring(index + 1);
      KVMStoragePool secondaryStorage=_storagePoolMgr.getStoragePoolByURI(volDir);
      physicalDisk=secondaryStorage.getPhysicalDisk(volName);
    }
 else     if (volume.getType() != Volume.Type.ISO) {
      pool=_storagePoolMgr.getStoragePool(volume.getPoolUuid());
      physicalDisk=pool.getPhysicalDisk(volume.getPath());
    }
    String volPath=null;
    if (physicalDisk != null) {
      volPath=physicalDisk.getPath();
    }
    DiskDef.diskBus diskBusType=getGuestDiskModel(vmSpec.getOs());
    DiskDef disk=new DiskDef();
    if (volume.getType() == Volume.Type.ISO) {
      if (volPath == null) {
        disk.defISODisk(null);
      }
 else {
        disk.defISODisk(volPath);
      }
    }
 else {
      int devId=(int)volume.getDeviceId();
      if (pool.getType() == StoragePoolType.CLVM) {
        disk.defBlockBasedDisk(physicalDisk.getPath(),devId,diskBusType);
      }
 else {
        if (volume.getType() == Volume.Type.DATADISK) {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,DiskDef.diskBus.VIRTIO,DiskDef.diskFmtType.QCOW2);
        }
 else {
          disk.defFileBasedDisk(physicalDisk.getPath(),devId,diskBusType,DiskDef.diskFmtType.QCOW2);
        }
      }
    }
    vm.getDevices().addDevice(disk);
  }
  if (vmSpec.getType() != VirtualMachine.Type.User) {
    if (_sysvmISOPath != null) {
      DiskDef iso=new DiskDef();
      iso.defISODisk(_sysvmISOPath);
      vm.getDevices().addDevice(iso);
    }
    createPatchVbd(conn,vmName,vm,vmSpec);
  }
}