Skip to content

Commit 6518f46

Browse files
committed
WIP
1 parent 9ebb6ec commit 6518f46

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

src/umockdev-ioctl.vala

+84-3
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,87 @@ public class IoctlData : GLib.Object {
216216
Posix.memcpy(&data[offset], new_data, new_data.length);
217217
}
218218

219+
/**
220+
* umockdev_ioctl_get_int:
221+
* @self: A #UMockdevIoctlData
222+
*
223+
* Return data as integer. This avoids problems with unaligned memory.
224+
*/
225+
public int get_int() {
226+
int value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
227+
Posix.memcpy(&value, data, sizeof(int));
228+
return value;
229+
}
230+
231+
/**
232+
* umockdev_ioctl_set_int:
233+
* @self: A #UMockdevIoctlData
234+
* @value: Value to set
235+
*
236+
* Set data to given value. This avoids problems with unaligned memory.
237+
*/
238+
public void set_int(int value) {
239+
Posix.memcpy(data, &value, sizeof(int));
240+
}
241+
242+
/**
243+
* umockdev_ioctl_set_uint32:
244+
* @self: A #UMockdevIoctlData
245+
* @value: Value to set
246+
*
247+
* Set data to given value. This avoids problems with unaligned memory.
248+
*/
249+
public void set_uint32(uint32 value) {
250+
Posix.memcpy(data, &value, sizeof(uint32));
251+
}
252+
253+
/**
254+
* umockdev_ioctl_get_ulong:
255+
* @self: A #UMockdevIoctlData
256+
*
257+
* Return data as ulong. This avoids problems with unaligned memory.
258+
*/
259+
public ulong get_ulong() {
260+
ulong value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
261+
Posix.memcpy(&value, data, sizeof(ulong));
262+
return value;
263+
}
264+
265+
/**
266+
* umockdev_ioctl_set_ulong:
267+
* @self: A #UMockdevIoctlData
268+
* @value: Value to set
269+
*
270+
* Set data to given value. This avoids problems with unaligned memory.
271+
*/
272+
public void set_ulong(ulong value) {
273+
Posix.memcpy(data, &value, sizeof(ulong));
274+
}
275+
276+
/**
277+
* umockdev_ioctl_get_long:
278+
* @self: A #UMockdevIoctlData
279+
*
280+
* Return data as long. This avoids problems with unaligned memory.
281+
*/
282+
public long get_long() {
283+
long value = 0; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
284+
Posix.memcpy(&value, data, sizeof(long));
285+
return value;
286+
}
287+
288+
/**
289+
* umockdev_ioctl_get_pointer:
290+
* @self: A #UMockdevIoctlData
291+
*
292+
* Return data as pointer. This avoids problems with unaligned memory.
293+
*/
294+
public void* get_pointer() {
295+
void* value = null; // unnecessary initialization, avoids "Use of possibly unassigned local variable"
296+
Posix.memcpy(&value, data, sizeof(void*));
297+
return value;
298+
}
299+
219300
/**
220301
* umockdev_ioctl_retrieve:
221302
* @self: A #UMockdevIoctlData
@@ -934,7 +1015,7 @@ internal class IoctlTreeHandler : IoctlBase {
9341015
} else {
9351016
Posix.errno = Posix.ENOTTY;
9361017
}
937-
last = tree.execute(last, request, *(void**) client.arg.data, ref ret);
1018+
last = tree.execute(last, request, client.arg.get_pointer(), ref ret);
9381019
my_errno = Posix.errno;
9391020
Posix.errno = 0;
9401021
if (last != null)
@@ -955,7 +1036,7 @@ internal class IoctlTreeHandler : IoctlBase {
9551036
* This should only happen for REAPURB, but it does not hurt to
9561037
* just always check.
9571038
*/
958-
if (*(void**) data.data == (void*) last_submit_urb.data) {
1039+
if (data.get_pointer() == (void*) last_submit_urb.data) {
9591040
data.set_ptr(0, last_submit_urb);
9601041

9611042
last_submit_urb = null;
@@ -1062,7 +1143,7 @@ internal class IoctlTreeRecorder : IoctlBase {
10621143
}
10631144

10641145
/* Record */
1065-
node = new IoctlTree.Tree.from_bin(request, *(void**) client.arg.data, ret);
1146+
node = new IoctlTree.Tree.from_bin(request, client.arg.get_pointer(), ret);
10661147
if (node != null) {
10671148
tree.insert((owned) node);
10681149
}

src/umockdev-pcap.vala

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {
9393

9494
switch (request) {
9595
case USBDEVFS_GET_CAPABILITIES:
96-
*(uint32*) data.data = capabilities;
96+
data.set_uint32(capabilities);
9797

9898
client.complete(0, 0);
9999
return true;
@@ -108,7 +108,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {
108108

109109
case USBDEVFS_DISCARDURB:
110110
for (int i = 0; i < urbs.length; i++) {
111-
if (urbs.index(i).urb_data.client_addr == *((ulong*)client.arg.data)) {
111+
if (urbs.index(i).urb_data.client_addr == client.arg.get_ulong()) {
112112
/* Found the urb, add to discard array, remove it and return success */
113113
discarded.prepend_val(urbs.index(i));
114114
urbs.remove_index(i);

tests/test-umockdev-vala.vala

+2-4
Original file line numberDiff line numberDiff line change
@@ -1000,15 +1000,13 @@ static bool
10001000
ioctl_custom_handle_ioctl_cb(UMockdev.IoctlBase handler, UMockdev.IoctlClient client)
10011001
{
10021002
if (client.request == 1) {
1003-
client.complete(*(long*)client.arg.data, 0);
1003+
client.complete(client.arg.get_long(), 0);
10041004
} else if (client.request == 2) {
10051005
client.complete(-1, Posix.ENOMEM);
10061006
} else if (client.request == 3 ) {
10071007
try {
10081008
var data = client.arg.resolve(0, sizeof(int));
1009-
1010-
*(int*) data.data = (int) 0xc00fffee;
1011-
1009+
data.set_int((int) 0xc00fffee);
10121010
client.complete(0, 0);
10131011
} catch (Error e) {
10141012
error ("cannot resolve client arg: %s", e.message);

0 commit comments

Comments
 (0)