[libvirt] [PATCH] ruby-libvirt: Don't crash in leases_wrap() by passing NULLs to rb_str_new2()

Dan Williams dcbw at redhat.com
Thu Jan 7 17:12:16 UTC 2016


Not all lease values are mandatory, and when they aren't supplied
by the libvirt driver they get set to NULL.  That makes
rb_str_new2() bail out.

Signed-off-by: Dan Williams <dcbw at redhat.com>
---
For example using the qemu driver we don't get 'iaid', and that
makes ruby unhappy:

[{"iface"=>"virbr1", "expirytime"=>1452189569, "type"=>0, "mac"=>"52:54:00:05:2b:6f",
"ipaddr"=>"192.168.121.49", "prefix"=>24, "hostname"=>"openshiftdev",
"clientid"=>"ff:00:05:2b:6f:00:01:00:01:1e:21:55:ed:52:54:00:05:2b:6f"}]

 ext/libvirt/network.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/ext/libvirt/network.c b/ext/libvirt/network.c
index 7c77d73..c250d7d 100644
--- a/ext/libvirt/network.c
+++ b/ext/libvirt/network.c
@@ -269,14 +269,20 @@ static VALUE leases_wrap(VALUE arg)
         rb_hash_aset(hash, rb_str_new2("expirytime"),
                      LL2NUM(lease->expirytime));
         rb_hash_aset(hash, rb_str_new2("type"), INT2NUM(lease->type));
-        rb_hash_aset(hash, rb_str_new2("mac"), rb_str_new2(lease->mac));
-        rb_hash_aset(hash, rb_str_new2("iaid"), rb_str_new2(lease->iaid));
+        if (lease->mac)
+            rb_hash_aset(hash, rb_str_new2("mac"), rb_str_new2(lease->mac));
+        if (lease->iaid)
+            rb_hash_aset(hash, rb_str_new2("iaid"), rb_str_new2(lease->iaid));
         rb_hash_aset(hash, rb_str_new2("ipaddr"), rb_str_new2(lease->ipaddr));
         rb_hash_aset(hash, rb_str_new2("prefix"), UINT2NUM(lease->prefix));
-        rb_hash_aset(hash, rb_str_new2("hostname"),
-                     rb_str_new2(lease->hostname));
-        rb_hash_aset(hash, rb_str_new2("clientid"),
-                     rb_str_new2(lease->clientid));
+        if (lease->hostname) {
+            rb_hash_aset(hash, rb_str_new2("hostname"),
+                         rb_str_new2(lease->hostname));
+        }
+        if (lease->clientid) {
+            rb_hash_aset(hash, rb_str_new2("clientid"),
+                         rb_str_new2(lease->clientid));
+        }
 
         rb_ary_store(result, i, hash);
     }
-- 
2.4.3




More information about the libvir-list mailing list