Skip to contentSkip to conversation

imsm: introduce helpers to manage file descriptors

PATCH Applied Mariusz Tkaczyk 2 participants 2 messages Oct 19, 2021 – Nov 2, 2021 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 imsm: introduce helpers to manage file descriptors

To avoid direct comparisions define dedicated inlines.
This patch propagates them in super-intel.c. They are declared globally for future usage outside IMSM.

Additionally, it adds fd check in save_backup_imsm() to remove code vulnerability and simplifies targets array implementation.

It also propagates pr_vrb() macro instead if (verbose) condidtion.

---
Changes:
- Fixed bug reported by Jes
- changed close_fd to do_close in __free_imsm_disk()

 mdadm.h       |  25 ++++++++
 super-intel.c | 167 +++++++++++++++++++++++---------------------------
 2 files changed, 100 insertions(+), 92 deletions(-)
mdadm.h +25 -0
index 8f8841d8..4f6da7e2 100644
@@ -782,6 +782,31 @@ static inline char *map_dev(int major, int minor, int create)
 	return map_dev_preferred(major, minor, create, NULL);
 }
 
+/**
+ * is_fd_valid() - check file descriptor.
+ * @fd: file descriptor.
+ *
+ * The function checks if @fd is nonnegative integer and shall be used only
+ * to verify open() result.
+ */
+static inline int is_fd_valid(int fd)
+{
+	return (fd > -1);
+}
+
+/**
+ * close_fd() - verify, close and unset file descriptor.
+ * @fd: pointer to file descriptor.
+ *
+ * The function closes and invalidates file descriptor if appropriative. It
+ * ignores incorrect file descriptor quitely to simplify error handling.
+ */
+static inline void close_fd(int *fd)
+{
+	if (is_fd_valid(*fd) && close(*fd) == 0)
+		*fd = -1;
+}
+
 struct active_array;
 struct metadata_update;
 
super-intel.c +75 -92

415 lines not on this page · raw message

Signature · 1 line
2.26.2
Applied
#1 lore raw
Quoted 10 lines · Mariusz Tkaczyk

To avoid direct comparisions define dedicated inlines.
This patch propagates them in super-intel.c. They are declared globally for future usage outside IMSM.

Additionally, it adds fd check in save_backup_imsm() to remove code vulnerability and simplifies targets array implementation.

It also propagates pr_vrb() macro instead if (verbose) condidtion.

Quoted 8 lines

---
Changes:
- Fixed bug reported by Jes
- changed close_fd to do_close in __free_imsm_disk()

 mdadm.h       |  25 ++++++++
 super-intel.c | 167 +++++++++++++++++++++++---------------------------
 2 files changed, 100 insertions(+), 92 deletions(-)

Applied!

Thanks,
Jes