Skip to content

Commit 6e320e3

Browse files
committed
BUG: Fix small memory access errors in ujson objToJSON.c
These were found (and validated fixed) using valgrind. It is likely that none of these changes should create any issues (except maybe on a debug build of python?). But silencing valgrind warnings is good on its own, to ease possible future debugging using valgrind.
1 parent 81093ba commit 6e320e3

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

pandas/_libs/src/ujson/python/objToJSON.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,9 +1458,9 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
14581458

14591459
if (is_datetimelike) {
14601460
if (nanosecVal == get_nat()) {
1461-
len = 5; // TODO: shouldn't require extra space for terminator
1462-
cLabel = PyObject_Malloc(len);
1463-
strncpy(cLabel, "null", len);
1461+
len = 4;
1462+
cLabel = PyObject_Malloc(len + 1);
1463+
strncpy(cLabel, "null", len + 1);
14641464
} else {
14651465
if (enc->datetimeIso) {
14661466
if ((type_num == NPY_TIMEDELTA) || (PyDelta_Check(item))) {
@@ -1486,23 +1486,22 @@ char **NpyArr_encodeLabels(PyArrayObject *labels, PyObjectEncoder *enc,
14861486
}
14871487
}
14881488
} else { // Fallback to string representation
1489-
PyObject *str = PyObject_Str(item);
1490-
if (str == NULL) {
1491-
Py_DECREF(item);
1489+
// Replace item with the string to keep it alive.
1490+
Py_SETREF(item, PyObject_Str(item));
1491+
if (item == NULL) {
14921492
NpyArr_freeLabels(ret, num);
14931493
ret = 0;
14941494
break;
14951495
}
14961496

1497-
cLabel = (char *)PyUnicode_AsUTF8(str);
1498-
Py_DECREF(str);
1497+
cLabel = (char *)PyUnicode_AsUTF8(item);
14991498
len = strlen(cLabel);
15001499
}
15011500

1502-
Py_DECREF(item);
15031501
// Add 1 to include NULL terminator
15041502
ret[i] = PyObject_Malloc(len + 1);
15051503
memcpy(ret[i], cLabel, len + 1);
1504+
Py_DECREF(item);
15061505

15071506
if (is_datetimelike) {
15081507
PyObject_Free(cLabel);

pandas/tests/series/test_dtypes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,6 @@ def test_astype_generic_timestamp_no_frequency(self, dtype, request):
387387
data = [1]
388388
s = Series(data)
389389

390-
if np.dtype(dtype).name not in ["timedelta64", "datetime64"]:
391-
mark = pytest.mark.xfail(reason="GH#33890 Is assigned ns unit")
392-
request.node.add_marker(mark)
393-
394390
msg = (
395391
fr"The '{dtype.__name__}' dtype has no unit\. "
396392
fr"Please pass in '{dtype.__name__}\[ns\]' instead."

0 commit comments

Comments
 (0)