Get minus value in python3 when calling C funtion that return char*


I tried to call C function in so file inside Python3 script.

The C function prototype is:

const char* _decrypt(const char* str); // always return NULL or malloc'ed buffer

I loaded the so in Python3, and call the function as below:

slib = ctypes.cdll.LoadLibrary('')
ret = slib._decrypt(ctypes.c_char_p(ciphertext)) # ciphertext is valid
ctypes.c_char_p(ret).value.decode('utf-8') # this may cause SEGV fault

Occationally, ret is minus, such as -1744677792. By occationally, I mean sometimes I got minus value, sometime I got valid positive return value..

Since C function always return either NULL or valid malloc'ed buffer.

How is it possible to get minus value inside Python?


The issue is caused by address incorrectly returned to Python3. By applying

ret = ret & 0xffffffff

I can get the right address.

But the so is based on 64 bits address, and I tried to use 0xffffffffffffffff to mask the returned value, it does not work this way, that's it will also cause SEGV fault.

Is this because Python3 use 32bits address by default?

asked on Stack Overflow Nov 23, 2019 by FaceBro • edited Nov 23, 2019 by FaceBro

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0