From: Colin Watson Date: Fri, 5 Dec 2025 11:03:48 +0000 Subject: Don't leave NewTopic partially-initialized on error If `NewTopic` was initialized with a non-dict `config`, then the constructor raised an exception but left the object in a partially-initialized state with `replica_assignment` and `config` elements that hadn't had their reference counts increased, leading to mysterious segfaults in the test suite. Be more careful to only set these elements once we're ready to do so. Fixes: #2146 Forwarded: https://github.com/confluentinc/confluent-kafka-python/pull/2151 Bug: https://github.com/confluentinc/confluent-kafka-python/issues/2146 Bug-Debian: https://bugs.debian.org/1117901 Last-Update: 2025-12-05 --- src/confluent_kafka/src/AdminTypes.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/confluent_kafka/src/AdminTypes.c b/src/confluent_kafka/src/AdminTypes.c index 61adeb2..5baa17d 100644 --- a/src/confluent_kafka/src/AdminTypes.c +++ b/src/confluent_kafka/src/AdminTypes.c @@ -67,6 +67,7 @@ static int NewTopic_init (PyObject *self0, PyObject *args, PyObject *kwargs) { NewTopic *self = (NewTopic *)self0; const char *topic; + PyObject *replica_assignment = NULL, *config = NULL; static char *kws[] = { "topic", "num_partitions", "replication_factor", @@ -82,22 +83,24 @@ static int NewTopic_init (PyObject *self0, PyObject *args, if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iiOO", kws, &topic, &self->num_partitions, &self->replication_factor, - &self->replica_assignment, - &self->config)) + &replica_assignment, + &config)) return -1; - if (self->config) { - if (!PyDict_Check(self->config)) { + if (config) { + if (!PyDict_Check(config)) { PyErr_SetString(PyExc_TypeError, "config must be a dict of strings"); return -1; } - Py_INCREF(self->config); + Py_INCREF(config); + self->config = config; } - Py_XINCREF(self->replica_assignment); + Py_XINCREF(replica_assignment); + self->replica_assignment = replica_assignment; self->topic = strdup(topic);