[ python-Bugs-1084766 ] sys.stdin segfaults on invalid stdin

SourceForge.net noreply at sourceforge.net
Mon Dec 19 07:15:10 CET 2005


Bugs item #1084766, was opened at 2004-12-13 15:41
Message generated for change (Comment added) made by nnorwitz
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1084766&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.3
>Status: Closed
>Resolution: Duplicate
Priority: 6
Submitted By: Mihai Ibanescu (misa)
>Assigned to: Neal Norwitz (nnorwitz)
Summary: sys.stdin segfaults on invalid stdin

Initial Comment:
Bug reported as:
https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=140715

If stdin is invalid (e.g., redirected from a directory
by mistake),
python interpreter segfaults on any attempt to access it.

In a case where one actually tries to read sys.stdin, I
would expect
something like

Traceback (most recent call last):
  File "<string>", line 1, in ?
IOError: [Errno 21] Is a directory

----------------------------------------------------------------------

>Comment By: Neal Norwitz (nnorwitz)
Date: 2005-12-18 22:15

Message:
Logged In: YES 
user_id=33168

This was fixed a while ago for 2.4.3 and 2.5.
See bug 1353504

----------------------------------------------------------------------

Comment By: Björn Lindqvist (sonderblade)
Date: 2005-01-11 18:16

Message:
Logged In: YES 
user_id=51702

A trivial solution is to move the lines

_PyExc_Init();
_PyImport_FixupExtensions("exceptions", "exceptions");

above the block that begins with "sysmod = _PySys_Init();"
Then when the interpreter is run with "python < ." it outputs:

Fatal Python error: Py_Initialize: can't initialize sys
Avbruten (SIGABRT)

I don't know if that is a good solution.

----------------------------------------------------------------------

Comment By: Jeff Epler (jepler)
Date: 2004-12-18 19:25

Message:
Logged In: YES 
user_id=2772

Duplicated here on a stale CVS version of 2.4.  Here's some
information from gdb:
Starting program: /usr/src/cvs/python/dist/src/python -S -c
'' < /
[Thread debugging using libthread_db enabled]
[New Thread -151074304 (LWP 6154)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -151074304 (LWP 6154)]
0x08074f90 in PyDict_SetItem (op=0xf6fae79c, key=0xf6fb6f20,
value=0x0)
    at Objects/dictobject.c:549
549             Py_INCREF(value);
(gdb) where
#0  0x08074f90 in PyDict_SetItem (op=0xf6fae79c,
key=0xf6fb6f20, value=0x0)
    at Objects/dictobject.c:549
#1  0x08075e13 in PyDict_SetItemString (v=0xf6fae79c,
key=0x8109d60 "stdin", 
    item=0x0) at Objects/dictobject.c:1988
#2  0x080d6988 in _PySys_Init () at Python/sysmodule.c:961
#3  0x080d3e17 in Py_InitializeEx (install_sigs=1) at
Python/pythonrun.c:190
#4  0x080d5815 in Py_Initialize () at Python/pythonrun.c:283
#5  0x08055159 in Py_Main (argc=4, argv=0xfefac1b4) at
Modules/main.c:418
#6  0x08054e0f in main (argc=4, argv=0xfefac1b4) at
Modules/python.c:23

The problem seems to be that PyFile_FromFile() returns NULL
without tripping the PyErr_Occurred() test on my line 946 of
sysmodule.c, because the call to fill_file_fields() returns
NULL too.

It looks like fill_file_fields is supposed to return NULL
with an error set, but when dircheck() is called that early,
PyExc_IOError is NULL, so no error is set.

Maybe _PyExc_Init() must be moved earlier in startup.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1084766&group_id=5470


More information about the Python-bugs-list mailing list