diff --git a/src/umockdev-ioctl.vala b/src/umockdev-ioctl.vala index d0e65e632417a624b48aa5ba7e70a21dc3adb1ce..baa384f754bcb3ad86968fe1aaa2311a62245f3e 100644 --- a/src/umockdev-ioctl.vala +++ b/src/umockdev-ioctl.vala @@ -216,6 +216,87 @@ public class IoctlData : GLib.Object { Posix.memcpy(&data[offset], new_data, new_data.length); } + /** + * umockdev_ioctl_get_int: + * @self: A #UMockdevIoctlData + * + * Return data as integer. This avoids problems with unaligned memory. + */ + public int get_int() { + int value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable" + Posix.memcpy(&value, data, sizeof(int)); + return value; + } + + /** + * umockdev_ioctl_set_int: + * @self: A #UMockdevIoctlData + * @value: Value to set + * + * Set data to given value. This avoids problems with unaligned memory. + */ + public void set_int(int value) { + Posix.memcpy(data, &value, sizeof(int)); + } + + /** + * umockdev_ioctl_set_uint32: + * @self: A #UMockdevIoctlData + * @value: Value to set + * + * Set data to given value. This avoids problems with unaligned memory. + */ + public void set_uint32(uint32 value) { + Posix.memcpy(data, &value, sizeof(uint32)); + } + + /** + * umockdev_ioctl_get_ulong: + * @self: A #UMockdevIoctlData + * + * Return data as ulong. This avoids problems with unaligned memory. + */ + public ulong get_ulong() { + ulong value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable" + Posix.memcpy(&value, data, sizeof(ulong)); + return value; + } + + /** + * umockdev_ioctl_set_ulong: + * @self: A #UMockdevIoctlData + * @value: Value to set + * + * Set data to given value. This avoids problems with unaligned memory. + */ + public void set_ulong(ulong value) { + Posix.memcpy(data, &value, sizeof(ulong)); + } + + /** + * umockdev_ioctl_get_long: + * @self: A #UMockdevIoctlData + * + * Return data as long. This avoids problems with unaligned memory. + */ + public long get_long() { + long value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable" + Posix.memcpy(&value, data, sizeof(long)); + return value; + } + + /** + * umockdev_ioctl_get_pointer: + * @self: A #UMockdevIoctlData + * + * Return data as pointer. This avoids problems with unaligned memory. + */ + public void* get_pointer() { + void* value = null; // unnecessary initialization, avoids "Use of possibly unassigned local variable" + Posix.memcpy(&value, data, sizeof(void*)); + return value; + } + /** * umockdev_ioctl_retrieve: * @self: A #UMockdevIoctlData @@ -934,7 +1015,7 @@ internal class IoctlTreeHandler : IoctlBase { } else { Posix.errno = Posix.ENOTTY; } - last = tree.execute(last, request, *(void**) client.arg.data, ref ret); + last = tree.execute(last, request, client.arg.get_pointer(), ref ret); my_errno = Posix.errno; Posix.errno = 0; if (last != null) @@ -955,7 +1036,7 @@ internal class IoctlTreeHandler : IoctlBase { * This should only happen for REAPURB, but it does not hurt to * just always check. */ - if (*(void**) data.data == (void*) last_submit_urb.data) { + if (data.get_pointer() == (void*) last_submit_urb.data) { data.set_ptr(0, last_submit_urb); last_submit_urb = null; @@ -1062,7 +1143,7 @@ internal class IoctlTreeRecorder : IoctlBase { } /* Record */ - node = new IoctlTree.Tree.from_bin(request, *(void**) client.arg.data, ret); + node = new IoctlTree.Tree.from_bin(request, client.arg.get_pointer(), ret); if (node != null) { tree.insert((owned) node); } diff --git a/src/umockdev-pcap.vala b/src/umockdev-pcap.vala index 9f9cdd4c6b3446c886dd57fd4bc217d18016cfc9..30180d95690215609f5544548f5b4ab62de621b8 100644 --- a/src/umockdev-pcap.vala +++ b/src/umockdev-pcap.vala @@ -93,7 +93,7 @@ internal class IoctlUsbPcapHandler : IoctlBase { switch (request) { case USBDEVFS_GET_CAPABILITIES: - *(uint32*) data.data = capabilities; + data.set_uint32(capabilities); client.complete(0, 0); return true; @@ -108,7 +108,7 @@ internal class IoctlUsbPcapHandler : IoctlBase { case USBDEVFS_DISCARDURB: for (int i = 0; i < urbs.length; i++) { - if (urbs.index(i).urb_data.client_addr == *((ulong*)client.arg.data)) { + if (urbs.index(i).urb_data.client_addr == client.arg.get_ulong()) { /* Found the urb, add to discard array, remove it and return success */ discarded.prepend_val(urbs.index(i)); urbs.remove_index(i); diff --git a/tests/test-umockdev-vala.vala b/tests/test-umockdev-vala.vala index 524c77776971f219821951ec8a7ff3a739c72613..92e8f09923f3da807c844e0aac23239918c4daa5 100644 --- a/tests/test-umockdev-vala.vala +++ b/tests/test-umockdev-vala.vala @@ -1000,15 +1000,13 @@ static bool ioctl_custom_handle_ioctl_cb(UMockdev.IoctlBase handler, UMockdev.IoctlClient client) { if (client.request == 1) { - client.complete(*(long*)client.arg.data, 0); + client.complete(client.arg.get_long(), 0); } else if (client.request == 2) { client.complete(-1, Posix.ENOMEM); } else if (client.request == 3 ) { try { var data = client.arg.resolve(0, sizeof(int)); - - *(int*) data.data = (int) 0xc00fffee; - + data.set_int((int) 0xc00fffee); client.complete(0, 0); } catch (Error e) { error ("cannot resolve client arg: %s", e.message);