From nobody Mon Jul 7 17:23:00 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4bbWKK22SWz60kZn; Mon, 07 Jul 2025 17:23:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bbWKK1Wytz3dQj; Mon, 07 Jul 2025 17:23:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751908981; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=r236bkBBXQUJ2801xKVFWANPKGfcRHX7GzwE68BQE3M=; b=M3BmzKKReVlOXQto2X61BudBGgmAJ9s4LV8QJVAKs6ELYK1YVFngvZ8r8WG3CyCG/L7oaE TmizblPoztE3iOqAYHypNWTv9Pt0Mxw8TvLKLZvR4cNJD/O9YXmfZ6Z9nhLsMegzvQ+gRf cgHMP3itmC3BZc5R8ADYm0jFm60LbUlTtkXIR+J+isaIUjRCBQN5FOYa69PeU4uUa9jwi8 ioB9jeSpEEBZuCfTB1MdYCL9Bj1bJs55Rkj9xX1pvauTSeAWuDAFP5DbGAy2ZfFpL9FP9+ +n6cOI1uzUA98X4ufrHsymnxAW4Tpf8i33uF+wdMS99eQdORZMKxMKjD/nyeSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751908981; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=r236bkBBXQUJ2801xKVFWANPKGfcRHX7GzwE68BQE3M=; b=hXWqpF0ftCkgcPHXtHgnpvGHHyFMIH6Cq1llrvfcys9Q+8pupnmDBPE1WMNn2dI1lH4LMj WFfRqB5+NDN68MClFXrmP0sGR8OEWc2WRQ6QLZL4QWKGwgGto8nPx4yFoVJV7VymOwJN+F fdYsKGme3sydnrfOV6jJ9RTpo0djRhHq/lmCPVpnpmM9tPUMaAqfd3ZTV4mfpkWyFRCj0w eUzH2mFpaWjpr2TnJJPjc22ReDlHS6adt8VjsZaG6Jo6aUJu3PseoqL7sGpl/mCCToYJGT 3biZauJSOYZ6ysx5p9p56764ToaPHTSPCcx/pVR777IzvXvmYnEU8nM9PePmUQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1751908981; a=rsa-sha256; cv=none; b=CD4dq28vAd886M69TUmBLfMVLImK+wqDQw5U2PzOKghytZrB6uo3gW7WnVqkW6Ua55Rsxc c+B7r2M7SyoL9fc+KzSm4zTdWQmmlxomkBlZ0SHWF+NXykUt5Cg9g0OcXFNQAR1wKTb7il Du+W1XKKLF1P9ZEeCMYEMiyNj2UDrQjXmSJ8sccXYmLb/Gu8plVSWEanB1PmK+87l9qxUq 0wIfRQf6mjK+J7ktuKRBn2tdn9cYwu1wrn48vltSBwKXnMfIgmQdoVTZQy5cz6iPoJ/N5q rKLd3MGx7jitXeplaEcgFowxO70KEZVFUdZM2yrQjXyVZDWt0Crm/FbUxkEqeA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bbWKK1717z10Zy; Mon, 07 Jul 2025 17:23:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 567HN0Dw080597; Mon, 7 Jul 2025 17:23:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 567HN09V080594; Mon, 7 Jul 2025 17:23:00 GMT (envelope-from git) Date: Mon, 7 Jul 2025 17:23:00 GMT Message-Id: <202507071723.567HN09V080594@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: fe1cd734a176 - main - mfiutil: Change mfi_autolearn_(period|mode) to write to a FILE List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: fe1cd734a17603f9b237c5bb08a9e29f2b4898ee Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=fe1cd734a17603f9b237c5bb08a9e29f2b4898ee commit fe1cd734a17603f9b237c5bb08a9e29f2b4898ee Author: John Baldwin AuthorDate: 2025-07-07 16:36:47 +0000 Commit: John Baldwin 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);