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

[libvirt] [libvirt-java] [PATCH 13/65] 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.

Signed-off-by: Claudio Bley <cbley av-test de>
---
 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 78dd9bc..bf3291d 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -282,7 +282,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.7.9.5


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