Program: 105143
Project: 105143
Buggy source:
protected synchronized String attachOrDetachDisk(final Connect conn,final boolean attach,final String vmName,final KVMPhysicalDisk attachingDisk,final int devId,final String serial) throws LibvirtException, InternalErrorException {
  List<DiskDef> disks=null;
  Domain dm=null;
  DiskDef diskdef=null;
  final KVMStoragePool attachingPool=attachingDisk.getPool();
  try {
    if (!attach) {
      dm=conn.domainLookupByName(vmName);
      final LibvirtDomainXMLParser parser=new LibvirtDomainXMLParser();
      final String xml=dm.getXMLDesc(0);
      parser.parseDomainXML(xml);
      disks=parser.getDisks();
      if (attachingPool.getType() == StoragePoolType.RBD) {
        if (resource.getHypervisorType() == Hypervisor.HypervisorType.LXC) {
          final String device=resource.mapRbdDevice(attachingDisk);
          if (device != null) {
            s_logger.debug("String_Node_Str" + device);
            attachingDisk.setPath(device);
          }
        }
      }
      for (      final DiskDef disk : disks) {
        final String file=disk.getDiskPath();
        if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
          diskdef=disk;
          break;
        }
      }
      if (diskdef == null) {
        throw new InternalErrorException("String_Node_Str" + attachingDisk.getPath() + "String_Node_Str");
      }
    }
 else {
      diskdef=new DiskDef();
      diskdef.setSerial(serial);
      if (attachingPool.getType() == StoragePoolType.RBD) {
        if (resource.getHypervisorType() == Hypervisor.HypervisorType.LXC) {
          final String device=resource.mapRbdDevice(attachingDisk);
          if (device != null) {
            s_logger.debug("String_Node_Str" + device);
            diskdef.defBlockBasedDisk(device,devId,DiskDef.DiskBus.VIRTIO);
          }
 else {
            throw new InternalErrorException("String_Node_Str" + attachingDisk.getPath() + "String_Node_Str");
          }
        }
 else {
          diskdef.defNetworkBasedDisk(attachingDisk.getPath(),attachingPool.getSourceHost(),attachingPool.getSourcePort(),attachingPool.getAuthUserName(),attachingPool.getUuid(),devId,DiskDef.DiskBus.VIRTIO,DiskProtocol.RBD,DiskDef.DiskFmtType.RAW);
        }
      }
 else       if (attachingPool.getType() == StoragePoolType.Gluster) {
        final String mountpoint=attachingPool.getLocalPath();
        final String path=attachingDisk.getPath();
        final String glusterVolume=attachingPool.getSourceDir().replace("String_Node_Str","String_Node_Str");
        diskdef.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint,"String_Node_Str"),attachingPool.getSourceHost(),attachingPool.getSourcePort(),null,null,devId,DiskDef.DiskBus.VIRTIO,DiskProtocol.GLUSTER,DiskDef.DiskFmtType.QCOW2);
      }
 else       if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
        diskdef.defFileBasedDisk(attachingDisk.getPath(),devId,DiskDef.DiskBus.VIRTIO,DiskDef.DiskFmtType.QCOW2);
      }
 else       if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
        diskdef.defBlockBasedDisk(attachingDisk.getPath(),devId,DiskDef.DiskBus.VIRTIO);
      }
    }
    final String xml=diskdef.toString();
    return attachOrDetachDevice(conn,attach,vmName,xml);
  }
  finally {
    if (dm != null) {
      dm.free();
    }
  }
}

Fixed version (ground truth):
protected synchronized String attachOrDetachDisk(final Connect conn,final boolean attach,final String vmName,final KVMPhysicalDisk attachingDisk,final int devId,final String serial,final Long bytesReadRate,final Long bytesWriteRate,final Long iopsReadRate,final Long iopsWriteRate) throws LibvirtException, InternalErrorException {
  List<DiskDef> disks=null;
  Domain dm=null;
  DiskDef diskdef=null;
  final KVMStoragePool attachingPool=attachingDisk.getPool();
  try {
    if (!attach) {
      dm=conn.domainLookupByName(vmName);
      final LibvirtDomainXMLParser parser=new LibvirtDomainXMLParser();
      final String xml=dm.getXMLDesc(0);
      parser.parseDomainXML(xml);
      disks=parser.getDisks();
      if (attachingPool.getType() == StoragePoolType.RBD) {
        if (resource.getHypervisorType() == Hypervisor.HypervisorType.LXC) {
          final String device=resource.mapRbdDevice(attachingDisk);
          if (device != null) {
            s_logger.debug("String_Node_Str" + device);
            attachingDisk.setPath(device);
          }
        }
      }
      for (      final DiskDef disk : disks) {
        final String file=disk.getDiskPath();
        if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
          diskdef=disk;
          break;
        }
      }
      if (diskdef == null) {
        throw new InternalErrorException("String_Node_Str" + attachingDisk.getPath() + "String_Node_Str");
      }
    }
 else {
      diskdef=new DiskDef();
      diskdef.setSerial(serial);
      if (attachingPool.getType() == StoragePoolType.RBD) {
        if (resource.getHypervisorType() == Hypervisor.HypervisorType.LXC) {
          final String device=resource.mapRbdDevice(attachingDisk);
          if (device != null) {
            s_logger.debug("String_Node_Str" + device);
            diskdef.defBlockBasedDisk(device,devId,DiskDef.DiskBus.VIRTIO);
          }
 else {
            throw new InternalErrorException("String_Node_Str" + attachingDisk.getPath() + "String_Node_Str");
          }
        }
 else {
          diskdef.defNetworkBasedDisk(attachingDisk.getPath(),attachingPool.getSourceHost(),attachingPool.getSourcePort(),attachingPool.getAuthUserName(),attachingPool.getUuid(),devId,DiskDef.DiskBus.VIRTIO,DiskProtocol.RBD,DiskDef.DiskFmtType.RAW);
        }
      }
 else       if (attachingPool.getType() == StoragePoolType.Gluster) {
        final String mountpoint=attachingPool.getLocalPath();
        final String path=attachingDisk.getPath();
        final String glusterVolume=attachingPool.getSourceDir().replace("String_Node_Str","String_Node_Str");
        diskdef.defNetworkBasedDisk(glusterVolume + path.replace(mountpoint,"String_Node_Str"),attachingPool.getSourceHost(),attachingPool.getSourcePort(),null,null,devId,DiskDef.DiskBus.VIRTIO,DiskProtocol.GLUSTER,DiskDef.DiskFmtType.QCOW2);
      }
 else       if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
        diskdef.defFileBasedDisk(attachingDisk.getPath(),devId,DiskDef.DiskBus.VIRTIO,DiskDef.DiskFmtType.QCOW2);
      }
 else       if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
        diskdef.defBlockBasedDisk(attachingDisk.getPath(),devId,DiskDef.DiskBus.VIRTIO);
      }
      if ((bytesReadRate != null) && (bytesReadRate > 0)) {
        diskdef.setBytesReadRate(bytesReadRate);
      }
      if ((bytesWriteRate != null) && (bytesWriteRate > 0)) {
        diskdef.setBytesWriteRate(bytesWriteRate);
      }
      if ((iopsReadRate != null) && (iopsReadRate > 0)) {
        diskdef.setIopsReadRate(iopsReadRate);
      }
      if ((iopsWriteRate != null) && (iopsWriteRate > 0)) {
        diskdef.setIopsWriteRate(iopsWriteRate);
      }
    }
    final String xml=diskdef.toString();
    return attachOrDetachDevice(conn,attach,vmName,xml);
  }
  finally {
    if (dm != null) {
      dm.free();
    }
  }
}