Friday, November 13, 2015

What Happens when we don't read the manual

This is RTFD kind of a pun which happened with me. Shooting self in the foot is more like it. I was trying to play around with ctypes and was trying to figure out how to get the last error code. I didn't read the python docs man page till this point and kept doing

#----------------python3 so don't be amazed when we do conversions-----------------------
import ctypes
import ctypes.util
libc = ctypes.util.find_library("c")
libc.prinf("Hello world") # this is not going to print nothing
errno = libc.errno
print( "Error code is " + str(errno))

#------------------------------

Guess what I get , a SIGSEGV .. !!!!! WOW and I now become more bold and try to get deeper with gdb. So here is how I prove myself to be a member of all idiotic morons society's prime member

 gdb python3
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1

Reading symbols from python3...Reading symbols from /usr/lib/debug//usr/bin/python3.4...done. #download the python symbols for this
done.
(gdb) run
Starting program: /usr/bin/python3
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> import ctypes.util
>>> libc = ctypes.util.find_library("c")
>>> libc.printf("Hello World")
>>> libc = ctypes.CDLL(libc)
>>> libc.printf("Hello World")
1
H>>> libc.errno
<_FuncPtr object at 0x7ffff5e58688>
>>> libc.errno()

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7fd16a0 in ?? ()
(gdb) bt 5
#0  0x00007ffff7fd16a0 in ?? () <== null function
#1  0x00007ffff5f49adc in ffi_call_unix64 () from /usr/lib/x86_64-linux-gnu/libffi.so.6
#2  0x00007ffff5f4940c in ffi_call () from /usr/lib/x86_64-linux-gnu/libffi.so.6
#3  0x00007ffff6159fed in _call_function_pointer (argcount=0, resmem=0x7fffffffdaf0, restype=<optimized out>, atypes=<optimized out>, avalues=0x7fffffffdae0,
    pProc=0x7ffff7fd16a0, flags=4353) at /build/python3.4-eNHHUi/python3.4-3.4.3/Modules/_ctypes/callproc.c:811
#4  _ctypes_callproc (pProc=pProc@entry=0x7ffff7fd16a0, argtuple=argtuple@entry=(), flags=4353, argtypes=argtypes@entry=0x0,
    restype=<_ctypes.PyCSimpleType at remote 0xa99178>, checker=0x0) at /build/python3.4-eNHHUi/python3.4-3.4.3/Modules/_ctypes/callproc.c:1149
(More stack frames follow...)
(gdb)




finally reading through the documentation the appropriate way of reading the error number is :



Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> import ctypes.util
>>> libc = ctypes.util.find_library("c")
>>> libc = ctypes.CDLL(libc)
>>> libc.printf("Hello World")
>>> ctypes.get_errno()




No comments:

Post a Comment