From ab83c4b14328ff3152096df3f5802c6c641c077a Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Fri, 5 Dec 2025 11:03:48 +0000 Subject: [PATCH] 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 --- src/confluent_kafka/src/AdminTypes.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/confluent_kafka/src/AdminTypes.c b/src/confluent_kafka/src/AdminTypes.c index da1faa353..7f2848e09 100644 --- a/src/confluent_kafka/src/AdminTypes.c +++ b/src/confluent_kafka/src/AdminTypes.c @@ -77,6 +77,7 @@ static void NewTopic_dealloc(NewTopic *self) { 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", @@ -91,22 +92,23 @@ static int NewTopic_init(PyObject *self0, PyObject *args, PyObject *kwargs) { if (!PyArg_ParseTupleAndKeywords( args, kwargs, "s|iiOO", kws, &topic, &self->num_partitions, - &self->replication_factor, &self->replica_assignment, - &self->config)) + &self->replication_factor, &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);