From: Nilay Shroff Date: Sun, 17 Mar 2024 11:36:22 +0000 (+0530) Subject: nvme: Don't seg fault if given device is not char/block device X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=fbb331106339de6c92ba520f1757a80802ab95e9;p=users%2Fsagi%2Fnvme-cli.git nvme: Don't seg fault if given device is not char/block device If the given device name is not char/block device then in open_dev_direct() set errno to ENODEV and err to -1 before returning to the dev_fd(). This would then ensure that in case of error, dev_fd() returns the corerct negative error code back to its callers. So now the callers of dev_fd() would handle the error appropriately instead of try accessing the nvme_dev which would be NULL. Signed-off-by: Nilay Shroff Signed-off-by: Daniel Wagner --- diff --git a/nvme.c b/nvme.c index d6a45fcf..dc065742 100644 --- a/nvme.c +++ b/nvme.c @@ -254,7 +254,8 @@ static int open_dev_direct(struct nvme_dev **devp, char *devstr, int flags) } if (!is_chardev(dev) && !is_blkdev(dev)) { nvme_show_error("%s is not a block or character device", devstr); - err = -ENODEV; + errno = ENODEV; + err = -1; goto err_close; } *devp = dev;