Skip to content

Commit 8e4314d

Browse files
committed
BUG: patch in weird nested decoding issue, courtesy of @Komnomnomnom
1 parent 2697b49 commit 8e4314d

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pandas/io/tests/test_json/test_pandas.py

+23
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,29 @@ def test_date_format(self):
314314
result = read_json(json,typ='series',parse_dates=True)
315315
assert_series_equal(result,ts)
316316

317+
def test_weird_nested_json(self):
318+
319+
# this used to core dump the parser
320+
s = r'''{
321+
"status": "success",
322+
"data": {
323+
"posts": [
324+
{
325+
"id": 1,
326+
"title": "A blog post",
327+
"body": "Some useful content"
328+
},
329+
{
330+
"id": 2,
331+
"title": "Another blog post",
332+
"body": "More content"
333+
}
334+
]
335+
}
336+
}'''
337+
338+
read_json(s)
339+
317340
@network
318341
@slow
319342
def test_url(self):

pandas/src/ujson/python/JSONtoObj.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct __PyObjectDecoder
1010
JSONObjectDecoder dec;
1111

1212
void* npyarr; // Numpy context buffer
13+
void* npyarr_addr; // Ref to npyarr ptr to track DECREF calls
1314
npy_intp curdim; // Current array dimension
1415

1516
PyArray_Descr* dtype;
@@ -67,9 +68,7 @@ void Npy_releaseContext(NpyArrContext* npyarr)
6768
}
6869
if (npyarr->dec)
6970
{
70-
// Don't set to null, used to make sure we don't Py_DECREF npyarr
71-
// in releaseObject
72-
// npyarr->dec->npyarr = NULL;
71+
npyarr->dec->npyarr = NULL;
7372
npyarr->dec->curdim = 0;
7473
}
7574
Py_XDECREF(npyarr->labels[0]);
@@ -88,6 +87,7 @@ JSOBJ Object_npyNewArray(void* _decoder)
8887
{
8988
// start of array - initialise the context buffer
9089
npyarr = decoder->npyarr = PyObject_Malloc(sizeof(NpyArrContext));
90+
decoder->npyarr_addr = npyarr;
9191

9292
if (!npyarr)
9393
{
@@ -515,7 +515,7 @@ JSOBJ Object_newDouble(double value)
515515
static void Object_releaseObject(JSOBJ obj, void* _decoder)
516516
{
517517
PyObjectDecoder* decoder = (PyObjectDecoder*) _decoder;
518-
if (obj != decoder->npyarr)
518+
if (obj != decoder->npyarr_addr)
519519
{
520520
Py_XDECREF( ((PyObject *)obj));
521521
}
@@ -555,11 +555,13 @@ PyObject* JSONToObj(PyObject* self, PyObject *args, PyObject *kwargs)
555555
pyDecoder.dec = dec;
556556
pyDecoder.curdim = 0;
557557
pyDecoder.npyarr = NULL;
558+
pyDecoder.npyarr_addr = NULL;
558559

559560
decoder = (JSONObjectDecoder*) &pyDecoder;
560561

561562
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|iiO&", kwlist, &sarg, &numpy, &labelled, PyArray_DescrConverter2, &dtype))
562563
{
564+
Npy_releaseContext(pyDecoder.npyarr);
563565
return NULL;
564566
}
565567

0 commit comments

Comments
 (0)