From 3f30ed4b167ead10ce850e72009b86b489335a82 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 29 May 2025 06:53:44 -0700 Subject: [PATCH 1/2] gh-134885: zstd: Use Py_XSETREF --- .../2025-05-29-06-53-40.gh-issue-134885.-_L22o.rst | 2 ++ Modules/_zstd/_zstdmodule.c | 11 ++++------- 2 files changed, 6 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-05-29-06-53-40.gh-issue-134885.-_L22o.rst diff --git a/Misc/NEWS.d/next/Library/2025-05-29-06-53-40.gh-issue-134885.-_L22o.rst b/Misc/NEWS.d/next/Library/2025-05-29-06-53-40.gh-issue-134885.-_L22o.rst new file mode 100644 index 00000000000000..4b05d42c109d06 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-29-06-53-40.gh-issue-134885.-_L22o.rst @@ -0,0 +1,2 @@ +Fix possible crash in the :mod:`compression.zstd` module related to setting +parameter types. Patch by Jelle Zijlstra. diff --git a/Modules/_zstd/_zstdmodule.c b/Modules/_zstd/_zstdmodule.c index 5ad697d2b83dd6..dce149b9a4abc1 100644 --- a/Modules/_zstd/_zstdmodule.c +++ b/Modules/_zstd/_zstdmodule.c @@ -514,13 +514,10 @@ _zstd_set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type, return NULL; } - Py_XDECREF(mod_state->CParameter_type); - Py_INCREF(c_parameter_type); - mod_state->CParameter_type = (PyTypeObject*)c_parameter_type; - - Py_XDECREF(mod_state->DParameter_type); - Py_INCREF(d_parameter_type); - mod_state->DParameter_type = (PyTypeObject*)d_parameter_type; + Py_XSETREF( + mod_state->CParameter_type, Py_NewRef((PyTypeObject*)c_parameter_type)); + Py_XSETREF( + mod_state->DParameter_type, Py_NewRef((PyTypeObject*)d_parameter_type)); Py_RETURN_NONE; } From 631916ac4fa22f2d1e250033ddc2d9ea1fd94c50 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 29 May 2025 07:05:59 -0700 Subject: [PATCH 2/2] . --- Lib/test/test_ctypes/test_incomplete.py | 1 - Modules/_zstd/_zstdmodule.c | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_ctypes/test_incomplete.py b/Lib/test/test_ctypes/test_incomplete.py index 2f344611995b2c..3189fcd1bd1330 100644 --- a/Lib/test/test_ctypes/test_incomplete.py +++ b/Lib/test/test_ctypes/test_incomplete.py @@ -1,6 +1,5 @@ import ctypes import unittest -import warnings from ctypes import Structure, POINTER, pointer, c_char_p # String-based "incomplete pointers" were implemented in ctypes 0.6.3 (2003, when diff --git a/Modules/_zstd/_zstdmodule.c b/Modules/_zstd/_zstdmodule.c index dce149b9a4abc1..986b3579479f0f 100644 --- a/Modules/_zstd/_zstdmodule.c +++ b/Modules/_zstd/_zstdmodule.c @@ -515,9 +515,9 @@ _zstd_set_parameter_types_impl(PyObject *module, PyObject *c_parameter_type, } Py_XSETREF( - mod_state->CParameter_type, Py_NewRef((PyTypeObject*)c_parameter_type)); + mod_state->CParameter_type, (PyTypeObject*)Py_NewRef(c_parameter_type)); Py_XSETREF( - mod_state->DParameter_type, Py_NewRef((PyTypeObject*)d_parameter_type)); + mod_state->DParameter_type, (PyTypeObject*)Py_NewRef(d_parameter_type)); Py_RETURN_NONE; }