Date: Mon, 7 Jul 2025 17:23:00 GMT From: John Baldwin <jhb@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Subject: git: fe1cd734a176 - main - mfiutil: Change mfi_autolearn_(period|mode) to write to a FILE Message-ID: <202507071723.567HN09V080594@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=fe1cd734a17603f9b237c5bb08a9e29f2b4898ee commit fe1cd734a17603f9b237c5bb08a9e29f2b4898ee Author: John Baldwin <jhb@FreeBSD.org> AuthorDate: 2025-07-07 16:36:47 +0000 Commit: John Baldwin <jhb@FreeBSD.org> CommitDate: 2025-07-07 16:36:47 +0000 mfiutil: Change mfi_autolearn_(period|mode) to write to a FILE This avoids using fragile logic with snprintf() to build strings. For the calling code in mfi_show.c, I chose to pass stdout directly instead of using fmemopen() to write to the temporary buffer since that is simpler and avoids having to deal with output truncation. Differential Revision: https://reviews.freebsd.org/D50881 --- usr.sbin/mfiutil/mfi_bbu.c | 40 +++++++++++----------------------------- usr.sbin/mfiutil/mfi_show.c | 10 ++++++---- usr.sbin/mfiutil/mfiutil.h | 4 ++-- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/usr.sbin/mfiutil/mfi_bbu.c b/usr.sbin/mfiutil/mfi_bbu.c index e97227d47c70..aa685e438453 100644 --- a/usr.sbin/mfiutil/mfi_bbu.c +++ b/usr.sbin/mfiutil/mfi_bbu.c @@ -40,41 +40,23 @@ /* The autolearn period is given in seconds. */ void -mfi_autolearn_period(uint32_t period, char *buf, size_t sz) +mfi_autolearn_period(FILE *fp, uint32_t period) { unsigned int d, h; - char *tmp; d = period / (24 * 3600); h = (period % (24 * 3600)) / 3600; - tmp = buf; if (d != 0) { - int fmt_len; - fmt_len = snprintf(buf, sz, "%u day%s", d, d == 1 ? "" : "s"); - if (fmt_len < 0) { - *buf = 0; - return; - } - if ((size_t)fmt_len >= sz) { - return; - } - tmp += fmt_len; - sz -= tmp - buf; - if (h != 0) { - fmt_len = snprintf(tmp, sz, ", "); - if (fmt_len < 0 || (size_t)fmt_len >= sz) { - return; - } - tmp += fmt_len; - sz -= 2; - } + fprintf(fp, "%u day%s", d, d == 1 ? "" : "s"); + if (h != 0) + fprintf(fp, ", "); } if (h != 0) - snprintf(tmp, sz, "%u hour%s", h, h == 1 ? "" : "s"); + fprintf(fp, "%u hour%s", h, h == 1 ? "" : "s"); if (d == 0 && h == 0) - snprintf(tmp, sz, "less than 1 hour"); + fprintf(fp, "less than 1 hour"); } /* The time to the next relearn is given in seconds since 1/1/2000. */ @@ -96,21 +78,21 @@ mfi_next_learn_time(uint32_t next_learn_time, char *buf, size_t sz) } void -mfi_autolearn_mode(uint8_t mode, char *buf, size_t sz) +mfi_autolearn_mode(FILE *fp, uint8_t mode) { switch (mode) { case 0: - snprintf(buf, sz, "enabled"); + fprintf(fp, "enabled"); break; case 1: - snprintf(buf, sz, "disabled"); + fprintf(fp, "disabled"); break; case 2: - snprintf(buf, sz, "warn via event"); + fprintf(fp, "warn via event"); break; default: - snprintf(buf, sz, "mode 0x%02x", mode); + fprintf(fp, "mode 0x%02x", mode); break; } } diff --git a/usr.sbin/mfiutil/mfi_show.c b/usr.sbin/mfiutil/mfi_show.c index bf85c8b82d69..2d413f2a46b4 100644 --- a/usr.sbin/mfiutil/mfi_show.c +++ b/usr.sbin/mfiutil/mfi_show.c @@ -218,8 +218,9 @@ show_battery(int ac, char **av __unused) printf(" Current Voltage: %d mV\n", stat.voltage); printf(" Temperature: %d C\n", stat.temperature); if (show_props) { - mfi_autolearn_period(props.auto_learn_period, buf, sizeof(buf)); - printf(" Autolearn period: %s\n", buf); + printf(" Autolearn period: "); + mfi_autolearn_period(stdout, props.auto_learn_period); + printf("\n"); if (props.auto_learn_mode != 0) snprintf(buf, sizeof(buf), "never"); else @@ -229,8 +230,9 @@ show_battery(int ac, char **av __unused) printf(" Learn delay interval: %u hour%s\n", props.learn_delay_interval, props.learn_delay_interval != 1 ? "s" : ""); - mfi_autolearn_mode(props.auto_learn_mode, buf, sizeof(buf)); - printf(" Autolearn mode: %s\n", buf); + printf(" Autolearn mode: "); + mfi_autolearn_mode(stdout, props.auto_learn_mode); + printf("\n"); if (props.bbu_mode != 0) printf(" BBU Mode: %d\n", props.bbu_mode); } diff --git a/usr.sbin/mfiutil/mfiutil.h b/usr.sbin/mfiutil/mfiutil.h index 34b423098862..86b03998163c 100644 --- a/usr.sbin/mfiutil/mfiutil.h +++ b/usr.sbin/mfiutil/mfiutil.h @@ -175,9 +175,9 @@ int mfi_bbu_get_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp); int mfi_bbu_set_props(int fd, struct mfi_bbu_properties *props, uint8_t *statusp); -void mfi_autolearn_period(uint32_t, char *, size_t); +void mfi_autolearn_period(FILE *, uint32_t); void mfi_next_learn_time(uint32_t, char *, size_t); -void mfi_autolearn_mode(uint8_t, char *, size_t); +void mfi_autolearn_mode(FILE *, uint8_t); int get_mfi_unit(const char *dev); char *get_mfi_type(const char *dev);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202507071723.567HN09V080594>