Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dereference void* in ctypes?

Consider the following code:

import ctypes

IPC_PRIVATE, MAP_SIZE, IPC_CREAT, IPC_EXCL = 0, 65536, 512, 1024

shmget = ctypes.cdll.LoadLibrary("libc.so.6").shmget
shmat = ctypes.cdll.LoadLibrary("libc.so.6").shmat

shm_id = shmget(IPC_PRIVATE, MAP_SIZE, IPC_CREAT | IPC_EXCL | 0600)
trace_bits = shmat(shm_id, 0, 0)
s = ctypes.string_at(ctypes.c_void_p(trace_bits), 1)
print(s[0])

When I try to run it, it gives me a "segmentation fault" after a successful run of shmat. What am I doing wrong?

like image 312
d33tah Avatar asked Mar 12 '26 04:03

d33tah


1 Answers

By default, all ctypes functions which are wrapped like this have restype == c_int. So you need to set it correctly before you call it. The same is for argtypes.

like image 91
Albert Avatar answered Mar 13 '26 16:03

Albert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!