GNOME Bugzilla – Bug 638094
Libusb : fix segfault + updates
Last modified: 2018-05-22 13:49:45 UTC
1. Make Context nullable for DeviceHandle.from_vid_pid 2. Remove [CCode (array_length = false)] from get_device_list This this fixes a segfault for : /* valac --pkg libusb-1.0 TestLibUsb.vala */ using LibUSB; namespace TestLibUSb { public static class TestLibUSb { static int main (string[] args) { Context context = null; Device[] devices = null; Context.init (out context); context.get_device_list(out devices); return 0; } } } 3. Replace Glib.TimeVal with Posix.TimeVal for methods wait_for_event, handle_events_timeout and handle_events_locked 4. add TransferFlags
Created attachment 177089 [details] [review] Patch Fix-segfault + updates.
The patch fixes the segfault during the c-code generation but breaks the compilation. I've tried several attributes in the vapi file: /* valac --pkg libusb-1.0 TestLibUsb.vala */ using LibUSB; namespace TestLibUSb { public static class TestLibUSb { static int main (string[] args) { Context context = null; Device[] devices = null; Context.init (out context); context.get_device_list(out devices); return 0; } } } /* public ssize_t get_device_list ([CCode (array_length = false, array_null_terminated = true)] out unowned Device[]? list); public ssize_t get_device_list ([CCode (array_length = false, array_null_terminated = true)] out unowned Device[] list); public ssize_t get_device_list ([CCode (array_length = false)] out unowned Device[]? list); public ssize_t get_device_list ([CCode (array_length = false)] out unowned Device[] list); valac -v -g --pkg libusb-1.0 TestLibUsb.vala --save-temps --dump-tree test.tree ** ERROR:valaccodearraymodule.c:941:vala_ccode_array_module_real_get_array_length_cvalue: assertion failed: (_tmp20_) Aborted public ssize_t get_device_list ([CCode (array_length = false)] out Device[] list); public ssize_t get_device_list ([CCode (array_length = false, array_null_terminated = true)] out Device[] list); public ssize_t get_device_list ([CCode (array_length = false, array_null_terminated = true)] out Device[]? list); public ssize_t get_device_list ([CCode (array_length = false)] out Device[]? list); valac -v -g --pkg libusb-1.0 TestLibUsb.vala --save-temps --dump-tree test.tree Segmentation fault public ssize_t get_device_list (out Device[] list); public ssize_t get_device_list (out unowned Device[] list); public ssize_t get_device_list ([CCode (array_null_terminated = true)] out unowned Device[] list); valac -v -g --pkg libusb-1.0 TestLibUsb.vala --save-temps --dump-tree test.tree cc -g -o 'TestLibUsb' 'TestLibUsb.c' -pthread -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/include/libusb-1.0 -pthread -lgobject-2.0 -lgthread-2.0 -lrt -lglib-2.0 -lusb-1.0 TestLibUsb.c: In function ‘test_lib_usb_test_lib_usb_main’: TestLibUsb.c:100:2: error: too many arguments to function ‘libusb_get_device_list’ /usr/include/libusb-1.0/libusb.h:778:9: note: declared here */
I was able to compile the code by changing the libusb-1.0.vapi : /* valac --pkg libusb-1.0 TestLibUsb.vala */ using LibUSB; namespace TestLibUSb { public static class TestLibUSb { static int main (string[] args) { Context? context = null; Device[]? devices = null; Context.init (out context); context.get_device_list(ref devices); return 0; } } } diff --git a/vapi/libusb-1.0.vapi b/vapi/libusb-1.0.vapi index a9c0311..b665d56 100644 --- a/vapi/libusb-1.0.vapi +++ b/vapi/libusb-1.0.vapi @@ -251,7 +251,7 @@ namespace LibUSB { public class Context { public static int init (out Context context); public void set_debug (int level); - public ssize_t get_device_list (out Device[] list); + public ssize_t get_device_list ([CCode (array_length = false, array_null_terminated = true)] ref Device[] list); public DeviceHandle open_device_with_vid_pid (uint16 vendor_id, uint16 product_id); public int try_lock_events ();
commit 0f9f2987450ebc95b1c6ed4f9225e1b410eaca3d Author: Geert Jordaens <geert.jordaens@telenet.be> Date: Wed Jan 19 12:19:13 2011 -0800 libusb-1.0: Add TransferFlags, use Posix.timeval not GLib.TimeVal
Created attachment 178788 [details] [review] libusb-1.0: Use a wrapper for Context.get_device_list Use a wrapper method to make the binding a bit nicer. The argument needs to be out and not have an array length, which is what is causing valac to crash. This includes a fix for that.
Review of attachment 178788 [details] [review]: ::: codegen/valaccodemethodcallmodule.vala @@ +788,3 @@ if (array_type != null) { for (int dim = 1; dim <= array_type.rank; dim++) { + ccode.add_assignment (get_array_sizes (unary.inner).get (dim - 1), params[dim - 1].no_array_length ? new CCodeIdentifier ("-1") : get_array_sizes (unary).get (dim - 1)); params[dim - 1] doesn't look right. Also, I think it would be better to add the (dummy) array length to the TargetValue instead, i.e., handle no_array_length case at line 406ff. Also, in the case of null-terminated arrays, this should calculate the array length at runtime instead of just using -1.
This VAPI still has some problems. First off, rev 3f33d23c884092ce95ffb0afaa45292acc46f778 introduced + [CCode (cname = "_vala_libusb_device_handle_new")] which leads to a linking error when calling the DeviceHandle constructor here. How is this constructor supposed to work? More over, foreach ( var device in devices ) does not work until we apply - public ssize_t get_device_list ([CCode (array_length = false)] out Device[] list); + public ssize_t get_device_list ([CCode (array_length = false, array_null_terminated = true)] out Device[] list); There's probably more issues, but at least these two should be fixed first.
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/151.