Skip to content

BUG: Fix small memory access errors in ujson objToJSON.c #33929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions pandas/_libs/src/ujson/python/objToJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -1458,9 +1458,9 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,

if (is_datetimelike) {
if (nanosecVal == get_nat()) {
len = 5; // TODO: shouldn't require extra space for terminator
cLabel = PyObject_Malloc(len);
strncpy(cLabel, "null", len);
len = 4;
cLabel = PyObject_Malloc(len + 1);
strncpy(cLabel, "null", len + 1);
} else {
if (enc->datetimeIso) {
if ((type_num == NPY_TIMEDELTA) || (PyDelta_Check(item))) {
Expand All @@ -1486,23 +1486,22 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
}
}
} else { // Fallback to string representation
PyObject *str = PyObject_Str(item);
if (str == NULL) {
Py_DECREF(item);
// Replace item with the string to keep it alive.
Py_SETREF(item, PyObject_Str(item));
if (item == NULL) {
NpyArr_freeLabels(ret, num);
ret = 0;
break;
}

cLabel = (char *)PyUnicode_AsUTF8(str);
Py_DECREF(str);
cLabel = (char *)PyUnicode_AsUTF8(item);
len = strlen(cLabel);
}

Py_DECREF(item);
// Add 1 to include NULL terminator
ret[i] = PyObject_Malloc(len + 1);
memcpy(ret[i], cLabel, len + 1);
Py_DECREF(item);

if (is_datetimelike) {
PyObject_Free(cLabel);
Expand Down