Skip to content

Commit 93dec23

Browse files
committed
Put locks around relevant functions and use PyDict_GetItemRef
1 parent 43d3dea commit 93dec23

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Modules/_zstd/compressor.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ _get_CDict(ZstdDict *self, int compressionLevel)
155155
{
156156
assert(PyMutex_IsLocked(&self->lock));
157157
PyObject *level = NULL;
158-
PyObject *capsule;
158+
PyObject *capsule = NULL;
159159
ZSTD_CDict *cdict;
160+
int ret;
160161

161162

162163
/* int level object */
@@ -166,11 +167,12 @@ _get_CDict(ZstdDict *self, int compressionLevel)
166167
}
167168

168169
/* Get PyCapsule object from self->c_dicts */
169-
capsule = PyDict_GetItemWithError(self->c_dicts, level);
170+
ret = PyDict_GetItemRef(self->c_dicts, level, &capsule);
171+
if (ret < 0) {
172+
Py_XDECREF(capsule);
173+
goto error;
174+
}
170175
if (capsule == NULL) {
171-
if (PyErr_Occurred()) {
172-
goto error;
173-
}
174176
/* Create ZSTD_CDict instance */
175177
char *dict_buffer = PyBytes_AS_STRING(self->dict_content);
176178
Py_ssize_t dict_len = Py_SIZE(self->dict_content);
@@ -197,9 +199,8 @@ _get_CDict(ZstdDict *self, int compressionLevel)
197199
goto error;
198200
}
199201

200-
/* Add PyCapsule object to self->c_dicts if not already inserted */
201-
int ret = PyDict_SetDefaultRef(self->c_dicts, level, capsule, NULL);
202-
Py_DECREF(capsule);
202+
/* Add PyCapsule object to self->c_dicts */
203+
ret = PyDict_SetItem(self->c_dicts, level, capsule);
203204
if (ret < 0) {
204205
goto error;
205206
}
@@ -214,6 +215,7 @@ _get_CDict(ZstdDict *self, int compressionLevel)
214215
cdict = NULL;
215216
success:
216217
Py_XDECREF(level);
218+
Py_XDECREF(capsule);
217219
return cdict;
218220
}
219221

@@ -422,6 +424,7 @@ static PyObject *
422424
compress_lock_held(ZstdCompressor *self, Py_buffer *data,
423425
ZSTD_EndDirective end_directive)
424426
{
427+
assert(PyMutex_IsLocked(&self->lock));
425428
ZSTD_inBuffer in;
426429
ZSTD_outBuffer out;
427430
_BlocksOutputBuffer buffer = {.list = NULL};
@@ -504,6 +507,7 @@ mt_continue_should_break(ZSTD_inBuffer *in, ZSTD_outBuffer *out)
504507
static PyObject *
505508
compress_mt_continue_lock_held(ZstdCompressor *self, Py_buffer *data)
506509
{
510+
assert(PyMutex_IsLocked(&self->lock));
507511
ZSTD_inBuffer in;
508512
ZSTD_outBuffer out;
509513
_BlocksOutputBuffer buffer = {.list = NULL};

Modules/_zstd/decompressor.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ decompress_lock_held(ZstdDecompressor *self, ZSTD_inBuffer *in,
343343
static void
344344
decompressor_reset_session_lock_held(ZstdDecompressor *self)
345345
{
346+
assert(PyMutex_IsLocked(&self->lock));
346347

347348
/* Reset variables */
348349
self->in_begin = 0;
@@ -362,6 +363,7 @@ static PyObject *
362363
stream_decompress_lock_held(ZstdDecompressor *self, Py_buffer *data,
363364
Py_ssize_t max_length)
364365
{
366+
assert(PyMutex_IsLocked(&self->lock));
365367
ZSTD_inBuffer in;
366368
PyObject *ret = NULL;
367369
int use_input_buffer;

0 commit comments

Comments
 (0)