CFFI security against wrong #ifdef

39 views
Skip to first unread message

anatoly techtonik

unread,
Jan 29, 2015, 1:47:00 PM1/29/15
to pytho...@googlegroups.com
My library (PDCurses) can be compiled with three defines:

#ifdef CHTYPE_LONG
# if _LP64
typedef unsigned int chtype;
# else
typedef unsigned long chtype;  /* 16-bit attr + 16-bit char */
# endif
#else
typedef unsigned short chtype; /* 8-bit attr + 8-bit char */
#endif

CFFI doesn't support them, so I need to choose one. And even if I choose something for parsing with CFFI, there is no guarantee that library was compiled with exactly same defines. Does CFFI protect against that?

Ryan

unread,
Jan 29, 2015, 3:30:18 PM1/29/15
to pytho...@googlegroups.com, anatoly techtonik
I'm not sure if this would work, but you could see if this works (off the top of my head):

from cffi import FFI
ffi = FFI()

ffi.cdef('int gettp();')
d = ffi.verify('''
#include <somelib.h>
#include <stdlib.h>

int gettp() {
switch (sizeof(chtype)) {
case sizeof(unsigned int): return 0;
case sizeof(unsigned short): return 1;
case sizeof(unsigned long): return 2;
default: return 3;
}
}''')

sizes = [
'unsigned int',
'unsigned short',
'unsigned long'
]

try:
t = types[d.gettp()]
except IndexError:
# handle bad type here

ffi.cdef(''''
chtype some_lib_func();
'''.replace('chtype', t))
--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.
Check out my website: http://kirbyfan64.github.io/

anatoly techtonik

unread,
Jan 31, 2015, 3:24:26 AM1/31/15
to Ryan, pytho...@googlegroups.com
The question is if CFFI is safe for use with library that is named the
same, but was compiled with different defines?
Reply all
Reply to author
Forward
0 new messages