The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=6b7b8575e97980166416886935e8cf376e50d88d
commit 6b7b8575e97980166416886935e8cf376e50d88d Author: John Baldwin <j...@freebsd.org> AuthorDate: 2025-07-07 16:37:28 +0000 Commit: John Baldwin <j...@freebsd.org> CommitDate: 2025-07-07 16:37:28 +0000 mfiutil: Use sbuf(3) to construct drive names in mfi_drive_name This replaces fragile uses of snprintf(). Note that I did not keep the truncation check for the space separator in the middle of the drive name since the same check was not applied to the second part, so the name was truncated anyway. Just truncate the entire result if it doesn't fit. Differential Revision: https://reviews.freebsd.org/D50883 --- usr.sbin/mfiutil/Makefile | 2 +- usr.sbin/mfiutil/mfi_drive.c | 34 ++++++++++++---------------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/usr.sbin/mfiutil/Makefile b/usr.sbin/mfiutil/Makefile index da1c9249f8cd..85b66d4b6f49 100644 --- a/usr.sbin/mfiutil/Makefile +++ b/usr.sbin/mfiutil/Makefile @@ -9,7 +9,7 @@ MLINKS= mfiutil.8 mrsasutil.8 CFLAGS.gcc+= -fno-builtin-strftime -LIBADD= util +LIBADD= sbuf util # Here be dragons .ifdef DEBUG diff --git a/usr.sbin/mfiutil/mfi_drive.c b/usr.sbin/mfiutil/mfi_drive.c index e8e945c566c4..c7c5aeb02f14 100644 --- a/usr.sbin/mfiutil/mfi_drive.c +++ b/usr.sbin/mfiutil/mfi_drive.c @@ -31,6 +31,7 @@ #include <sys/types.h> #include <sys/errno.h> +#include <sys/sbuf.h> #include <ctype.h> #include <err.h> #include <fcntl.h> @@ -56,9 +57,9 @@ const char * mfi_drive_name(struct mfi_pd_info *pinfo, uint16_t device_id, uint32_t def) { struct mfi_pd_info info; + struct sbuf sb; static char buf[16]; - char *p; - int error, fd, len; + int fd; if ((def & MFI_DNAME_HONOR_OPTS) != 0 && (mfi_opts & (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID)) != 0) @@ -89,40 +90,29 @@ mfi_drive_name(struct mfi_pd_info *pinfo, uint16_t device_id, uint32_t def) pinfo = &info; } - p = buf; - len = sizeof(buf); + sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); if (def & MFI_DNAME_DEVICE_ID) { if (device_id == 0xffff) - error = snprintf(p, len, "MISSING"); + sbuf_printf(&sb, "MISSING"); else - error = snprintf(p, len, "%2u", device_id); - if (error >= 0) { - p += error; - len -= error; - } + sbuf_printf(&sb, "%2u", device_id); } if ((def & (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID)) == - (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID) && len >= 2) { - *p++ = ' '; - len--; - *p = '\0'; - len--; + (MFI_DNAME_ES|MFI_DNAME_DEVICE_ID)) { + sbuf_cat(&sb, " "); } if (def & MFI_DNAME_ES) { if (pinfo->encl_device_id == 0xffff) - error = snprintf(p, len, "S%u", + sbuf_printf(&sb, "S%u", pinfo->slot_number); else if (pinfo->encl_device_id == pinfo->ref.v.device_id) - error = snprintf(p, len, "E%u", + sbuf_printf(&sb, "E%u", pinfo->encl_index); else - error = snprintf(p, len, "E%u:S%u", + sbuf_printf(&sb, "E%u:S%u", pinfo->encl_index, pinfo->slot_number); - if (error >= 0) { - p += error; - len -= error; - } } + sbuf_finish(&sb); return (buf); }