]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132983: Call Py_XDECREF rather than PyObject_GC_Del in failed __new__ (GH-133962)
authorPetr Viktorin <encukou@gmail.com>
Tue, 13 May 2025 09:11:52 +0000 (11:11 +0200)
committerGitHub <noreply@github.com>
Tue, 13 May 2025 09:11:52 +0000 (11:11 +0200)
Call Py_XDECREF rather than PyObject_GC_Del in failed __new__

This will call tp_dealloc and clear all members.

Modules/_zstd/compressor.c
Modules/_zstd/decompressor.c
Modules/_zstd/zstddict.c

index 97693fd043efb20532a0bea77eeac31b77acbd17..38baee2be1e95beed138adb2480a7fb0dd212382 100644 (file)
@@ -338,6 +338,7 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
     }
 
     self->use_multithread = 0;
+    self->dict = NULL;
 
     /* Compression context */
     self->cctx = ZSTD_createCCtx();
@@ -372,7 +373,6 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
     }
 
     /* Load Zstandard dictionary to compression context */
-    self->dict = NULL;
     if (zstd_dict != Py_None) {
         if (_zstd_load_c_dict(self, zstd_dict) < 0) {
             goto error;
@@ -387,9 +387,7 @@ _zstd_ZstdCompressor_new_impl(PyTypeObject *type, PyObject *level,
     return (PyObject*)self;
 
 error:
-    if (self != NULL) {
-        PyObject_GC_Del(self);
-    }
+    Py_XDECREF(self);
     return NULL;
 }
 
@@ -401,7 +399,9 @@ ZstdCompressor_dealloc(PyObject *ob)
     PyObject_GC_UnTrack(self);
 
     /* Free compression context */
-    ZSTD_freeCCtx(self->cctx);
+    if (self->cctx) {
+        ZSTD_freeCCtx(self->cctx);
+    }
 
     /* Py_XDECREF the dict after free the compression context */
     Py_CLEAR(self->dict);
index 852b796a872eefd69fb516309a0a6fc72bd02b80..58f9c9f804e549631fbce76e7ecfda4b660b5a7b 100644 (file)
@@ -554,6 +554,7 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
     self->in_end = -1;
     self->unused_data = NULL;
     self->eof = 0;
+    self->dict = NULL;
 
     /* needs_input flag */
     self->needs_input = 1;
@@ -570,7 +571,6 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
     }
 
     /* Load Zstandard dictionary to decompression context */
-    self->dict = NULL;
     if (zstd_dict != Py_None) {
         if (_zstd_load_d_dict(self, zstd_dict) < 0) {
             goto error;
@@ -592,9 +592,7 @@ _zstd_ZstdDecompressor_new_impl(PyTypeObject *type, PyObject *zstd_dict,
     return (PyObject*)self;
 
 error:
-    if (self != NULL) {
-        PyObject_GC_Del(self);
-    }
+    Py_XDECREF(self);
     return NULL;
 }
 
@@ -606,7 +604,9 @@ ZstdDecompressor_dealloc(PyObject *ob)
     PyObject_GC_UnTrack(self);
 
     /* Free decompression context */
-    ZSTD_freeDCtx(self->dctx);
+    if (self->dctx) {
+        ZSTD_freeDCtx(self->dctx);
+    }
 
     /* Py_CLEAR the dict after free decompression context */
     Py_CLEAR(self->dict);
index 264946e8da2a997a21a2d9249ba1aa66176fd85e..7df187a6fa69d72ebb3b83a961f352fcd833a748 100644 (file)
@@ -52,6 +52,7 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, PyObject *dict_content,
 
     self->dict_content = NULL;
     self->d_dict = NULL;
+    self->dict_id = 0;
 
     /* ZSTD_CDict dict */
     self->c_dicts = PyDict_New();
@@ -92,9 +93,7 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, PyObject *dict_content,
     return (PyObject*)self;
 
 error:
-    if (self != NULL) {
-        PyObject_GC_Del(self);
-    }
+    Py_XDECREF(self);
     return NULL;
 }
 
@@ -106,7 +105,9 @@ ZstdDict_dealloc(PyObject *ob)
     PyObject_GC_UnTrack(self);
 
     /* Free ZSTD_DDict instance */
-    ZSTD_freeDDict(self->d_dict);
+    if (self->d_dict) {
+        ZSTD_freeDDict(self->d_dict);
+    }
 
     /* Release dict_content after Free ZSTD_CDict/ZSTD_DDict instances */
     Py_CLEAR(self->dict_content);