[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [libvirt-java] [PATCH 1/2] Make Device.listCapabilities return only valid array elements



The libvirt function virNodeDeviceListCaps might return fewer elements
than requested.  Take this into account and properly decode the UTF-8
strings returned.

Additionally, the given strings are freed before returning the resulting
string array.
---
 src/main/java/org/libvirt/Device.java      | 10 +++++++---
 src/main/java/org/libvirt/jna/Libvirt.java |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/libvirt/Device.java b/src/main/java/org/libvirt/Device.java
index fe49ce9..d55321e 100644
--- a/src/main/java/org/libvirt/Device.java
+++ b/src/main/java/org/libvirt/Device.java
@@ -3,6 +3,8 @@ package org.libvirt;
 import org.libvirt.jna.DevicePointer;
 import static org.libvirt.Library.libvirt;
 
+import com.sun.jna.Pointer;
+
 /**
  * A device which is attached to a node
  */
@@ -135,13 +137,15 @@ public class Device {
      */
     public String[] listCapabilities() throws LibvirtException {
         int maxCaps = getNumberOfCapabilities();
-        String[] names = new String[maxCaps];
 
         if (maxCaps > 0) {
-            libvirt.virNodeDeviceListCaps(VDP, names, maxCaps);
+            Pointer[] ptrs = new Pointer[maxCaps];
+            int got = libvirt.virNodeDeviceListCaps(VDP, ptrs, maxCaps);
             processError();
+            return Library.toStringArray(ptrs, got);
+        } else {
+            return Library.NO_STRINGS;
         }
-        return names;
     }
 
     /**
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index b026b04..a2bf42e 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -281,7 +281,7 @@ public interface Libvirt extends Library {
     String virNodeDeviceGetName(DevicePointer virDevicePointer);
     String virNodeDeviceGetParent(DevicePointer virDevicePointer);
     int virNodeDeviceNumOfCaps(DevicePointer virDevicePointer);
-    int virNodeDeviceListCaps(DevicePointer virDevicePointer, String[] names, int maxNames);
+    int virNodeDeviceListCaps(DevicePointer virDevicePointer, Pointer[] names, int maxNames);
     String virNodeDeviceGetXMLDesc(DevicePointer virDevicePointer);
     int virNodeDeviceFree(DevicePointer virDevicePointer);
     int virNodeDeviceDettach(DevicePointer virDevicePointer);
-- 
1.8.5.2.msysgit.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]