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.setfilesystemencoding("xxx"); open("a") => stack overflow
Type: crash Stage:
Components: Interpreter Core Versions: Python 3.1, Python 3.2, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ned.deily, vstinner
Priority: normal Keywords: patch

Created on 2010-03-24 23:05 by vstinner, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
setfilesystemencoding.patch vstinner, 2010-03-24 23:08
Messages (4)
msg101656 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-03-24 23:05
sys.setfilesystemencoding() doesn't check if the argument is a valid encoding name.

If the filesystem encoding is invalid, open("a") goes into an unlimited loop. The default recursion limit is 1000, but the example crash at 930: too bad :-) Each loop allocate ~9000 bytes: Linux creates a 8 MB stack by default, and so ~9000x930 uses all the stack.

Using a lower recursion limit, we can see the loop:
----------
$ ./python -c 'import sys; sys.setrecursionlimit(30); sys.setfilesystemencoding("xxx"); open("x")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 98, in search_function
    level=0)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 83, in search_function
    norm_encoding = normalize_encoding(encoding)
  File "/home/SHARE/SVN/py3k/Lib/encodings/__init__.py", line 55, in normalize_encoding
    if isinstance(encoding, bytes):
RuntimeError: maximum recursion depth exceeded while calling a Python object
----------
msg101657 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-03-24 23:08
Attached patch raises a LookupError is the specified encoding is invalid. It uses _PyCodec_Lookup() function: is it allowed in bltinmodule.c?
msg101658 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2010-03-24 23:47
(duplicate of Issue8076)
msg101662 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-03-25 00:36
Commited: r79393 (py3k), r79394 (3.1).
History
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52473
2010-05-17 15:05:04floxlinkissue8740 superseder
2010-03-25 00:36:34vstinnersetstatus: open -> closed
resolution: fixed
messages: + msg101662
2010-03-24 23:47:22ned.deilysetnosy: + ned.deily
messages: + msg101658
2010-03-24 23:08:07vstinnersetfiles: + setfilesystemencoding.patch
keywords: + patch
messages: + msg101657
2010-03-24 23:05:53vstinnercreate