classification
Title: Segfaults in _json while encoding objects
Type: crash Stage: resolved
Components: Extension Modules Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: ezio.melotti, python-dev
Priority: normal Keywords: needs review, patch

Created on 2011-05-10 16:17 by ezio.melotti, last changed 2011-05-10 22:25 by ezio.melotti. This issue is now closed.

Files
File name Uploaded Description Edit
issue12051.diff ezio.melotti, 2011-05-10 16:17 review
issue12051-2.diff ezio.melotti, 2011-05-10 19:11 review
Messages (3)
msg135716 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-05-10 16:17
Subclasses of JSONEncoder that use check_circular=False can segfault json:
>>> import json
>>> class EndlessJSONEncoder(json.JSONEncoder):
...     def default(self, o):
...         return [o]
... 
>>> EndlessJSONEncoder(check_circular=False).encode(5j)
Segmentation fault

The attached patch fixes it raising a "RuntimeError: maximum recursion depth exceeded".

There might be other ways to get a segfault, because there are other recursive calls involving more functions (i.e. a calls b, and b calls a) that are not covered by the patch.
msg135728 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-05-10 19:11
New patch fixes two more segfaults while encoding highly-nested objects.
Updated the issue title accordingly.
msg135751 - (view) Author: Roundup Robot (python-dev) Date: 2011-05-10 22:24
New changeset a21f5af476cb by Ezio Melotti in branch '2.7':
#12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
http://hg.python.org/cpython/rev/a21f5af476cb

New changeset 9557e4eeb291 by Ezio Melotti in branch '3.1':
#12051: Fix segfault in json.dumps() while encoding highly-nested objects using the C accelerations.
http://hg.python.org/cpython/rev/9557e4eeb291

New changeset 3847b279d665 by Ezio Melotti in branch '3.2':
#12051: merge with 3.1.
http://hg.python.org/cpython/rev/3847b279d665

New changeset 821d7885c7ec by Ezio Melotti in branch 'default':
#12051: merge with 3.2.
http://hg.python.org/cpython/rev/821d7885c7ec
History
Date User Action Args
2011-05-10 22:25:49ezio.melottisetstatus: open -> closed
resolution: fixed
stage: commit review -> resolved
2011-05-10 22:24:03python-devsetnosy: + python-dev
messages: + msg135751
2011-05-10 19:11:07ezio.melottisetfiles: + issue12051-2.diff

messages: + msg135728
title: Segfault in JSONEncoder subclasses with check_circular=False -> Segfaults in _json while encoding objects
2011-05-10 16:17:19ezio.melotticreate