Skip to content

Commit

Permalink
Fix a crash wish unencodable encoding in the encoder.
Browse files Browse the repository at this point in the history
JSONEncoder.encode() crashed in Python 3 when encoded bytes keys if the
encoding was not encodable to utf-8 (contained surrogates).
  • Loading branch information
serhiy-storchaka committed May 21, 2017
1 parent e84076f commit 9cef9f5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion simplejson/_speedups.c
Expand Up @@ -642,10 +642,13 @@ encoder_stringify_key(PyEncoderObject *s, PyObject *key)
}
else if (PyString_Check(key)) {
#if PY_MAJOR_VERSION >= 3
const char *encoding = JSON_ASCII_AS_STRING(s->encoding);
if (encoding == NULL)
return NULL;
return PyUnicode_Decode(
PyString_AS_STRING(key),
PyString_GET_SIZE(key),
JSON_ASCII_AS_STRING(s->encoding),
encoding,
NULL);
#else /* PY_MAJOR_VERSION >= 3 */
Py_INCREF(key);
Expand Down Expand Up @@ -2599,6 +2602,8 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
s->encoding = JSON_ParseEncoding(encoding);
if (s->encoding == NULL)
goto bail;
if (JSON_ASCII_AS_STRING(s->encoding) == NULL)
goto bail;
Py_INCREF(indent);
s->indent = indent;
Py_INCREF(key_separator);
Expand Down
10 changes: 9 additions & 1 deletion simplejson/tests/test_speedups.py
@@ -1,10 +1,12 @@
from __future__ import with_statement

import sys
import unittest
from unittest import TestCase

import simplejson
from simplejson import encoder, decoder, scanner
from simplejson.compat import PY3, long_type
from simplejson.compat import PY3, long_type, b


def has_speedups():
Expand Down Expand Up @@ -80,3 +82,9 @@ def test_int_as_string_bitcount_overflow(self):
def test():
encoder.JSONEncoder(int_as_string_bitcount=long_count).encode(0)
self.assertRaises((TypeError, OverflowError), test)

if PY3:
@skip_if_speedups_missing
def test_bad_encoding(self):
with self.assertRaises(UnicodeEncodeError):
encoder.JSONEncoder(encoding='\udcff').encode({b('key'): 123})

0 comments on commit 9cef9f5

Please sign in to comment.