From 595e3cbb3db46124abcbf605bbe8d5784a69213e Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 16 Oct 2008 21:09:28 +0000 Subject: check for error conditions in _json #3623 --- Lib/json/tests/test_scanstring.py | 7 +++++++ Modules/_json.c | 8 +++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Lib/json/tests/test_scanstring.py b/Lib/json/tests/test_scanstring.py index 87051bb..6b600db 100644 --- a/Lib/json/tests/test_scanstring.py +++ b/Lib/json/tests/test_scanstring.py @@ -2,6 +2,7 @@ import sys import decimal from unittest import TestCase +import json import json.decoder class TestScanString(TestCase): @@ -100,3 +101,9 @@ class TestScanString(TestCase): self.assertEquals( scanstring('["Bad value", truth]', 2, None, True), (u'Bad value', 12)) + + def test_issue3623(self): + self.assertRaises(ValueError, json.decoder.scanstring, b"xxx", 1, + "xxx") + self.assertRaises(UnicodeDecodeError, + json.encoder.encode_basestring_ascii, b"xx\xff") diff --git a/Modules/_json.c b/Modules/_json.c index 88510a7..832b1ff 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -179,11 +179,13 @@ raise_errmsg(char *msg, PyObject *s, Py_ssize_t end) errmsg_fn = PyObject_GetAttrString(decoder, "errmsg"); if (errmsg_fn == NULL) return; - Py_XDECREF(decoder); + Py_DECREF(decoder); } pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end); - PyErr_SetObject(PyExc_ValueError, pymsg); - Py_DECREF(pymsg); + if (pymsg) { + PyErr_SetObject(PyExc_ValueError, pymsg); + Py_DECREF(pymsg); + } /* def linecol(doc, pos): -- cgit v0.12