Skip to content

Commit b686562

Browse files
hreineckeigaw
authored andcommitted
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 <hare@suse.de>
1 parent 32560ea commit b686562

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

libnvme/nvme.i

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ PyObject *hostid_from_file();
135135
%exception nvme_ctrl::discover {
136136
discover_err = 0;
137137
$action /* $action sets discover_err to non-zero value on failure */
138-
if (discover_err) {
138+
if (discover_err == 1) {
139+
SWIG_exception(SWIG_AttributeError, "No controller connection");
140+
} else if (discover_err) {
139141
SWIG_exception(SWIG_RuntimeError, "Discover failed");
140142
}
141143
}
@@ -748,6 +750,7 @@ struct nvme_ns {
748750

749751
%newobject discover;
750752
struct nvmf_discovery_log *discover(int lsp = 0, int max_retries = 6) {
753+
const char *dev;
751754
struct nvmf_discovery_log *logp;
752755
struct nvme_get_discovery_args args = {
753756
.c = $self,
@@ -758,11 +761,16 @@ struct nvme_ns {
758761
.lsp = lsp,
759762
};
760763

764+
dev = nvme_ctrl_get_name($self);
765+
if (dev) {
766+
discover_err = 1;
767+
return NULL;
768+
}
761769
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
762770
logp = nvmf_get_discovery_wargs(&args);
763771
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
764772

765-
if (logp == NULL) discover_err = 1;
773+
if (logp == NULL) discover_err = 2;
766774
return logp;
767775
}
768776

0 commit comments

Comments
 (0)