Program: 105435
Project: 105435
Buggy source:
protected synchronized String attachOrDetachDisk(Connect conn,boolean attach,String vmName,KVMPhysicalDisk attachingDisk,int devId,Long bytesReadRate,Long bytesWriteRate,Long iopsReadRate,Long iopsWriteRate,String cacheMode) throws LibvirtException, InternalErrorException {
  List<DiskDef> disks=null;
  Domain dm=null;
  DiskDef diskdef=null;
  KVMStoragePool attachingPool=attachingDisk.getPool();
  try {
    if (!attach) {
      dm=conn.domainLookupByName(vmName);
      LibvirtDomainXMLParser parser=new LibvirtDomainXMLParser();
      String xml=dm.getXMLDesc(0);
      parser.parseDomainXML(xml);
      disks=parser.getDisks();
      for (      DiskDef disk : disks) {
        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();
      if (attachingPool.getType() == StoragePoolType.RBD) {
        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) {
        diskdef.defNetworkBasedDisk(attachingDisk.getPath(),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);
      if (cacheMode != null) {
        diskdef.setCacheMode(DiskDef.diskCacheMode.valueOf(cacheMode));
      }
    }
    String xml=diskdef.toString();
    return attachOrDetachDevice(conn,attach,vmName,xml);
  }
  finally {
    if (dm != null) {
      dm.free();
    }
  }
}

Fixed version (ground truth):
protected synchronized String attachOrDetachDisk(Connect conn,boolean attach,String vmName,KVMPhysicalDisk attachingDisk,int devId,Long bytesReadRate,Long bytesWriteRate,Long iopsReadRate,Long iopsWriteRate,String cacheMode) throws LibvirtException, InternalErrorException {
  List<DiskDef> disks=null;
  Domain dm=null;
  DiskDef diskdef=null;
  KVMStoragePool attachingPool=attachingDisk.getPool();
  try {
    if (!attach) {
      dm=conn.domainLookupByName(vmName);
      LibvirtDomainXMLParser parser=new LibvirtDomainXMLParser();
      String xml=dm.getXMLDesc(0);
      parser.parseDomainXML(xml);
      disks=parser.getDisks();
      for (      DiskDef disk : disks) {
        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();
      if (attachingPool.getType() == StoragePoolType.RBD) {
        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) {
        diskdef.defNetworkBasedDisk(attachingDisk.getPath(),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);
      if (cacheMode != null) {
        diskdef.setCacheMode(DiskDef.diskCacheMode.valueOf(cacheMode.toUpperCase()));
      }
    }
    String xml=diskdef.toString();
    return attachOrDetachDevice(conn,attach,vmName,xml);
  }
  finally {
    if (dm != null) {
      dm.free();
    }
  }
}