From c279d921b2ecac840943c0781f813ad88c5da942 Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 21 Dec 2022 14:39:04 -0800 Subject: [PATCH 1/3] BUG: Fix unintialized strlen when PyUnicode_AsUTF8AndSize fails --- pandas/_libs/src/ujson/python/objToJSON.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index a6f18e0aec4d9..4d7af13d03e6c 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -341,6 +341,7 @@ static char *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, Set errorMsg(to tell encoder to stop), and let Python exception propagate. */ JSONObjectEncoder *enc = (JSONObjectEncoder *)tc->encoder; + *_outLen = 0; enc->errorMsg = "Encoding failed."; } return encoded; From aed9852386846d8d76f1d2e8e9a0321e8b8cf1db Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Wed, 21 Dec 2022 19:19:44 -0800 Subject: [PATCH 2/3] Check error message before allocating buffer --- pandas/_libs/src/ujson/lib/ultrajsonenc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/src/ujson/lib/ultrajsonenc.c b/pandas/_libs/src/ujson/lib/ultrajsonenc.c index a74d505b0d0ec..169c5b6889077 100644 --- a/pandas/_libs/src/ujson/lib/ultrajsonenc.c +++ b/pandas/_libs/src/ujson/lib/ultrajsonenc.c @@ -1080,11 +1080,11 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, case JT_UTF8: { value = enc->getStringValue(obj, &tc, &szlen); - Buffer_Reserve(enc, RESERVE_STRING(szlen)); if (enc->errorMsg) { enc->endTypeContext(obj, &tc); return; } + Buffer_Reserve(enc, RESERVE_STRING(szlen)); Buffer_AppendCharUnchecked(enc, '\"'); if (enc->forceASCII) { From 78f5987e451f7436fdf100cb6d295c490b61a44e Mon Sep 17 00:00:00 2001 From: Thomas Li <47963215+lithomas1@users.noreply.github.com> Date: Thu, 22 Dec 2022 08:58:36 -0800 Subject: [PATCH 3/3] remove setting strlen to 0 --- pandas/_libs/src/ujson/python/objToJSON.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/_libs/src/ujson/python/objToJSON.c b/pandas/_libs/src/ujson/python/objToJSON.c index 4d7af13d03e6c..a6f18e0aec4d9 100644 --- a/pandas/_libs/src/ujson/python/objToJSON.c +++ b/pandas/_libs/src/ujson/python/objToJSON.c @@ -341,7 +341,6 @@ static char *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, Set errorMsg(to tell encoder to stop), and let Python exception propagate. */ JSONObjectEncoder *enc = (JSONObjectEncoder *)tc->encoder; - *_outLen = 0; enc->errorMsg = "Encoding failed."; } return encoded;