Skip to content

Commit e575190

Browse files
authored
gh-132983: Call Py_XDECREF rather than PyObject_GC_Del in failed __new__ (GH-133962)
Call Py_XDECREF rather than PyObject_GC_Del in failed __new__ This will call tp_dealloc and clear all members.
1 parent c09cec5 commit e575190

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

Modules/_zstd/compressor.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
338338
}
339339

340340
self->use_multithread = 0;
341+
self->dict = NULL;
341342

342343
/* Compression context */
343344
self->cctx = ZSTD_createCCtx();
@@ -372,7 +373,6 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
372373
}
373374

374375
/* Load Zstandard dictionary to compression context */
375-
self->dict = NULL;
376376
if (zstd_dict != Py_None) {
377377
if (_zstd_load_c_dict(self, zstd_dict) < 0) {
378378
goto error;
@@ -387,9 +387,7 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
387387
return (PyObject*)self;
388388

389389
error:
390-
if (self != NULL) {
391-
PyObject_GC_Del(self);
392-
}
390+
Py_XDECREF(self);
393391
return NULL;
394392
}
395393

@@ -401,7 +399,9 @@ ZstdCompressor_dealloc(PyObject *ob)
401399
PyObject_GC_UnTrack(self);
402400

403401
/* Free compression context */
404-
ZSTD_freeCCtx(self->cctx);
402+
if (self->cctx) {
403+
ZSTD_freeCCtx(self->cctx);
404+
}
405405

406406
/* Py_XDECREF the dict after free the compression context */
407407
Py_CLEAR(self->dict);

Modules/_zstd/decompressor.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
554554
self->in_end = -1;
555555
self->unused_data = NULL;
556556
self->eof = 0;
557+
self->dict = NULL;
557558

558559
/* needs_input flag */
559560
self->needs_input = 1;
@@ -570,7 +571,6 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
570571
}
571572

572573
/* Load Zstandard dictionary to decompression context */
573-
self->dict = NULL;
574574
if (zstd_dict != Py_None) {
575575
if (_zstd_load_d_dict(self, zstd_dict) < 0) {
576576
goto error;
@@ -592,9 +592,7 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
592592
return (PyObject*)self;
593593

594594
error:
595-
if (self != NULL) {
596-
PyObject_GC_Del(self);
597-
}
595+
Py_XDECREF(self);
598596
return NULL;
599597
}
600598

@@ -606,7 +604,9 @@ ZstdDecompressor_dealloc(PyObject *ob)
606604
PyObject_GC_UnTrack(self);
607605

608606
/* Free decompression context */
609-
ZSTD_freeDCtx(self->dctx);
607+
if (self->dctx) {
608+
ZSTD_freeDCtx(self->dctx);
609+
}
610610

611611
/* Py_CLEAR the dict after free decompression context */
612612
Py_CLEAR(self->dict);

Modules/_zstd/zstddict.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, PyObject *dict_content,
5252

5353
self->dict_content = NULL;
5454
self->d_dict = NULL;
55+
self->dict_id = 0;
5556

5657
/* ZSTD_CDict dict */
5758
self->c_dicts = PyDict_New();
@@ -92,9 +93,7 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, PyObject *dict_content,
9293
return (PyObject*)self;
9394

9495
error:
95-
if (self != NULL) {
96-
PyObject_GC_Del(self);
97-
}
96+
Py_XDECREF(self);
9897
return NULL;
9998
}
10099

@@ -106,7 +105,9 @@ ZstdDict_dealloc(PyObject *ob)
106105
PyObject_GC_UnTrack(self);
107106

108107
/* Free ZSTD_DDict instance */
109-
ZSTD_freeDDict(self->d_dict);
108+
if (self->d_dict) {
109+
ZSTD_freeDDict(self->d_dict);
110+
}
110111

111112
/* Release dict_content after Free ZSTD_CDict/ZSTD_DDict instances */
112113
Py_CLEAR(self->dict_content);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy