Re: dl + getenv (without arguments) = segfault (python2.p2, python2.3)

Discussion in 'Python' started by Skip Montanaro, Aug 7, 2003.

  1. Xavier> Do note that my previous e-mail was not implying my possible
    Xavier> ignorance to the 'dl' module's functionality, but instead
    Xavier> inquiring wether there is or may be a solution in the future for
    Xavier> Python to handle such an erronous outcome. And plus, segfaults
    Xavier> are ugly and make me cry.

    I understand. Given the nature of the problem here, I suspect the segfault
    occurred in getenv() (I can't check to be sure, as I don't have the dl
    module). It's kind of hard for Python to gracefully recover when it doesn't
    control the program counter.

    Skip
    Skip Montanaro, Aug 7, 2003
    #1
    1. Advertising

  2. Re: dl + getenv (without arguments) = segfault (python2.p2,python2.3)

    Carl Banks <> writes:

    > Skip Montanaro wrote:
    >>
    >> Xavier> Do note that my previous e-mail was not implying my possible
    >> Xavier> ignorance to the 'dl' module's functionality, but instead
    >> Xavier> inquiring wether there is or may be a solution in the future for
    >> Xavier> Python to handle such an erronous outcome. And plus, segfaults
    >> Xavier> are ugly and make me cry.
    >>
    >> I understand. Given the nature of the problem here, I suspect the segfault
    >> occurred in getenv() (I can't check to be sure, as I don't have the dl
    >> module). It's kind of hard for Python to gracefully recover when it doesn't
    >> control the program counter.

    >
    > Theoretically, it could trap SIGSEGV and have the handler return
    > control to Python. (You'd probably have to wrap the dl call with
    > setjmp, and use longjmp in the handler.) I wouldn't count on the
    > Python process being in great shape afterwards, though.


    ctypes (but only when run under windows) handles this with win32
    structured exception handling (c_char_p is the ctypes way to specify
    'char *'):

    c:\test>python
    Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from ctypes import *
    >>> cdll.msvcrt.getenv.restype = c_char_p
    >>> cdll.msvcrt.getenv("windir")

    'C:\\WINDOWS'
    >>> cdll.msvcrt.getenv(32)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    WindowsError: exception: access violation
    >>>


    ctypes is cross platform, but it doesn't trap SIGSEGV as Carl
    suggested. I wonder if someone comes up with a patch <wink>.

    Having said that, it is possible to specify the arguments that getenv
    accepts, and so at least make it somewhat harder to crash it:

    >>> cdll.msvcrt.getenv.argtypes = [c_char_p]
    >>> cdll.msvcrt.getenv(32)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: while constructing argument 1:
    string expected instead of int instance
    >>> cdll.msvcrt.getenv("PATH")

    'C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;.....'
    >>>


    ctypes lives at <http://starship.python.net/crew/theller/ctypes/>

    Thomas
    Thomas Heller, Aug 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    setuid() and getenv()?

    vertigo, Jul 16, 2004, in forum: Perl
    Replies:
    1
    Views:
    690
    Joe Smith
    Jul 17, 2004
  2. Xavier
    Replies:
    0
    Views:
    417
    Xavier
    Aug 6, 2003
  3. lordy

    To getenv or not getenv

    lordy, Jul 28, 2006, in forum: Java
    Replies:
    2
    Views:
    4,805
    lordy
    Jul 28, 2006
  4. Magdoll
    Replies:
    2
    Views:
    236
    Magdoll
    Nov 17, 2008
  5. Andrey Vul
    Replies:
    8
    Views:
    685
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page