From 36a5d36adcc979b1806f61a9053c5710cd9bd8e4 Mon Sep 17 00:00:00 2001 From: cenhuilin Date: Fri, 19 Sep 2025 09:07:04 +0800 Subject: [PATCH] nvme: Don't seg fault if given device is not char/block device (cherry picked from commit 0bb537fbd6d7e91824d84ad87b02bee19f156b44) --- ...iven-device-is-not-char-block-device.patch | 36 +++++++++++++++++++ nvme-cli.spec | 7 +++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 0001-nvme-Don-t-seg-fault-if-given-device-is-not-char-block-device.patch diff --git a/0001-nvme-Don-t-seg-fault-if-given-device-is-not-char-block-device.patch b/0001-nvme-Don-t-seg-fault-if-given-device-is-not-char-block-device.patch new file mode 100644 index 0000000..6655bbd --- /dev/null +++ b/0001-nvme-Don-t-seg-fault-if-given-device-is-not-char-block-device.patch @@ -0,0 +1,36 @@ +From fbb331106339de6c92ba520f1757a80802ab95e9 Mon Sep 17 00:00:00 2001 +From: Nilay Shroff +Date: Fri, 19 Sep 2025 09:01:13 +0800 +Subject: [PATCH] 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 +--- + nvme.c | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/nvme.c b/nvme.c +index e9aac8f..367ead6 100644 +--- a/nvme.c ++++ b/nvme.c +@@ -281,7 +281,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; +-- +2.43.0 + diff --git a/nvme-cli.spec b/nvme-cli.spec index 78e874d..6350274 100644 --- a/nvme-cli.spec +++ b/nvme-cli.spec @@ -1,12 +1,14 @@ Name: nvme-cli Version: 2.8 -Release: 1 +Release: 2 Summary: NVMe management command line interface License: GPLv2+ URL: https://github.com/linux-nvme/nvme-cli Source0: https://github.com/linux-nvme/%{name}/archive/v%{version}.tar.gz +Patch0001: 0001-nvme-Don-t-seg-fault-if-given-device-is-not-char-block-device.patch + Requires: libnvme BuildRequires: gcc-g++ @@ -60,6 +62,9 @@ rm -f %{buildroot}/usr/lib/dracut/dracut.conf.d/70-nvmf-autoconnect.conf %{_mandir}/man1/nvme*.gz %changelog +* Fri Sep 19 2025 cenhuilin - 2.8-2 +- nvme: Don't seg fault if given device is not char/block device + * Thu Mar 21 2024 panchenbo - 2.8-1 - Update to version 2.8 -- Gitee