[New-bugs-announce] [issue31505] assertion failure in _json.make_encoder() in case of a bad encoder() argument

Oren Milman report at bugs.python.org
Mon Sep 18 04:57:43 EDT 2017


New submission from Oren Milman:

The following code causes an assertion failure:

import _json
def _bad_encoder(*args):
    return None

enc = _json.make_encoder(None, None, _bad_encoder, None,
                         'foo', 'bar', None, None, None)

enc(obj='spam', _current_indent_level=4)


This is because encoder_new() (in Modules/_json.c) assumes that the received
encoder() argument is a function that returns a string, and stores it in the
new PyEncoderObject.
When encoder_encode_string() is called (by encoder_listencode_obj()), it
returns whatever the stored encoder() returned, assuming it returned a string.
Then, encoder_listencode_obj() passes this value to _steal_accumulate(), which
passes it to _PyAccu_Accumulate(), which asserts it is a string.


Similarly, the following code also causes an assertion failure (only the obj
argument is different):

import _json
def _bad_encoder(*args):
    return None

enc = _json.make_encoder(None, None, _bad_encoder, None,
                         'foo', 'bar', None, None, None)

enc(obj={'spam': 42}, _current_indent_level=4)


In this case, encoder_listencode_dict() passes whatever encoder_encode_string()
returned, to _PyAccu_Accumulate(), which asserts it is a string.

----------
components: Extension Modules
messages: 302428
nosy: Oren Milman
priority: normal
severity: normal
status: open
title: assertion failure in _json.make_encoder() in case of a bad encoder() argument
type: crash
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31505>
_______________________________________


More information about the New-bugs-announce mailing list