From 66e010c597f9617676a4878018247310245357df Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Thu, 28 Aug 2025 14:38:53 +0200 Subject: [PATCH] nvme.i: fix crash on ctrl.discover() without a connection Calling 'ctrl.discover()' without having called 'nvme.connect()' first causes the application to crash horriby. So add a check for an existing controller connection before trying to fetch the discovery log page. Signed-off-by: Hannes Reinecke --- libnvme/nvme.i | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libnvme/nvme.i b/libnvme/nvme.i index 4ede50e74..b2059c5c7 100644 --- a/libnvme/nvme.i +++ b/libnvme/nvme.i @@ -135,7 +135,9 @@ PyObject *hostid_from_file(); %exception nvme_ctrl::discover { discover_err = 0; $action /* $action sets discover_err to non-zero value on failure */ - if (discover_err) { + if (discover_err == 1) { + SWIG_exception(SWIG_AttributeError, "No controller connection"); + } else if (discover_err) { SWIG_exception(SWIG_RuntimeError, "Discover failed"); } } @@ -748,6 +750,7 @@ struct nvme_ns { %newobject discover; struct nvmf_discovery_log *discover(int lsp = 0, int max_retries = 6) { + const char *dev; struct nvmf_discovery_log *logp; struct nvme_get_discovery_args args = { .c = $self, @@ -758,11 +761,16 @@ struct nvme_ns { .lsp = lsp, }; + dev = nvme_ctrl_get_name($self); + if (dev) { + discover_err = 1; + return NULL; + } Py_BEGIN_ALLOW_THREADS /* Release Python GIL */ logp = nvmf_get_discovery_wargs(&args); Py_END_ALLOW_THREADS /* Reacquire Python GIL */ - if (logp == NULL) discover_err = 1; + if (logp == NULL) discover_err = 2; return logp; }