This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sys.stdin segfaults on invalid stdin
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: nnorwitz Nosy List: jepler, misa, nnorwitz, sonderblade
Priority: high Keywords:

Created on 2004-12-13 23:41 by misa, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg23684 - (view) Author: Mihai Ibanescu (misa) Date: 2004-12-13 23:41
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
msg23685 - (view) Author: Jeff Epler (jepler) Date: 2004-12-19 03:25
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.
msg23686 - (view) Author: Björn Lindqvist (sonderblade) Date: 2005-01-12 02:16
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.
msg23687 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2005-12-19 06:15
Logged In: YES 
user_id=33168

This was fixed a while ago for 2.4.3 and 2.5.
See bug 1353504
History
Date User Action Args
2022-04-11 14:56:08adminsetgithub: 41317
2004-12-13 23:41:25misacreate