From: Sun Jian <sun.jian.kdev@gmail.com>
To: Vaibhav Agarwal <vaibhav.sr@gmail.com>, Johan Hovold <johan@kernel.org>
Cc: Mark Greer <mgreer@animalcreek.com>,
Alex Elder <elder@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
David Laight <david.laight.linux@gmail.com>,
greybus-dev@lists.linaro.org, linux-staging@lists.linux.dev,
linux-kernel@vger.kernel.org, Sun Jian <sun.jian.kdev@gmail.com>
Subject: [PATCH v3] staging: greybus: audio: avoid snprintf truncation warnings
Date: Tue, 30 Dec 2025 09:29:08 +0800 [thread overview]
Message-ID: <20251230012908.214959-1-sun.jian.kdev@gmail.com> (raw)
In-Reply-To: <20251229112649.137391-1-sun.jian.kdev@gmail.com>
W=1 reports possible truncation when formatting widget/control names
with snprintf() and a %s argument. Use a small helper and hide the %s
pointer from the compiler's truncation analysis via OPTIMIZER_HIDE_VAR(),
while keeping the existing snprintf() formatting.
No functional change intended.
Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com>
Changes in v3:
- Replace the earlier scnprintf()/strlcat() approach with a helper
keeping snprintf().
- Hide the %s argument from compiler truncation analysis using
OPTIMIZER_HIDE_VAR().
- Add a small local length limit macro with a short comment.
---
drivers/staging/greybus/audio_topology.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/drivers/staging/greybus/audio_topology.c b/drivers/staging/greybus/audio_topology.c
index 76146f91cddc..35775067897c 100644
--- a/drivers/staging/greybus/audio_topology.c
+++ b/drivers/staging/greybus/audio_topology.c
@@ -1009,6 +1009,19 @@ static const struct snd_soc_dapm_widget gbaudio_widgets[] = {
SND_SOC_DAPM_POST_PMD),
};
+/* Limit %s length to avoid -Wformat-truncation with snprintf() */
+#define GB_NAME_TMP_LEN 32
+
+static void gbaudio_prefix_dev_id(char *name, size_t name_len,
+ unsigned int dev_id)
+{
+ char temp_name[GB_NAME_TMP_LEN], *cp = temp_name;
+
+ strscpy(temp_name, name, sizeof(temp_name));
+ OPTIMIZER_HIDE_VAR(cp);
+ snprintf(name, name_len, "GB %u %s", dev_id, cp);
+}
+
static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
struct snd_soc_dapm_widget *dw,
struct gb_audio_widget *w, int *w_size)
@@ -1018,7 +1031,6 @@ static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
struct gb_audio_control *curr;
struct gbaudio_control *control, *_control;
size_t size;
- char temp_name[NAME_SIZE];
ret = gbaudio_validate_kcontrol_count(w);
if (ret) {
@@ -1086,8 +1098,7 @@ static int gbaudio_tplg_create_widget(struct gbaudio_module_info *module,
}
/* Prefix dev_id to widget control_name */
- strscpy(temp_name, w->name, sizeof(temp_name));
- snprintf(w->name, sizeof(w->name), "GB %d %s", module->dev_id, temp_name);
+ gbaudio_prefix_dev_id(w->name, sizeof(w->name), module->dev_id);
switch (w->type) {
case snd_soc_dapm_spk:
@@ -1143,7 +1154,6 @@ static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
struct gb_audio_control *curr;
struct gbaudio_control *control, *_control;
size_t size;
- char temp_name[NAME_SIZE];
size = sizeof(struct snd_kcontrol_new) * module->num_controls;
dapm_kctls = devm_kzalloc(module->dev, size, GFP_KERNEL);
@@ -1168,9 +1178,7 @@ static int gbaudio_tplg_process_kcontrols(struct gbaudio_module_info *module,
}
control->id = curr->id;
/* Prefix dev_id to widget_name */
- strscpy(temp_name, curr->name, sizeof(temp_name));
- snprintf(curr->name, sizeof(curr->name), "GB %d %s", module->dev_id,
- temp_name);
+ gbaudio_prefix_dev_id(curr->name, sizeof(curr->name), module->dev_id);
control->name = curr->name;
if (curr->info.type == GB_AUDIO_CTL_ELEM_TYPE_ENUMERATED) {
struct gb_audio_enumerated *gbenum =
--
2.43.0
next prev parent reply other threads:[~2025-12-30 1:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-29 11:26 [PATCH] staging: greybus: audio: avoid snprintf truncation warnings Sun Jian
2025-12-29 14:17 ` David Laight
2025-12-29 16:13 ` [PATCH v2] " Sun Jian
2025-12-29 16:52 ` Greg Kroah-Hartman
2025-12-30 1:29 ` Sun Jian [this message]
2025-12-30 7:40 ` [PATCH v3] " Greg Kroah-Hartman
2025-12-30 22:20 ` David Laight
2026-01-05 11:55 ` Dan Carpenter
2026-01-05 15:37 ` sun jian
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251230012908.214959-1-sun.jian.kdev@gmail.com \
--to=sun.jian.kdev@gmail.com \
--cc=david.laight.linux@gmail.com \
--cc=elder@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=greybus-dev@lists.linaro.org \
--cc=johan@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-staging@lists.linux.dev \
--cc=mgreer@animalcreek.com \
--cc=vaibhav.sr@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.