From: Mahad Ibrahim <mahad.ibrahim.dev@gmail.com>
To: Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>
Cc: Kees Cook <kees@kernel.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
Mahad Ibrahim <mahad.ibrahim.dev@gmail.com>
Subject: [PATCH 7/7] ALSA: usb-audio: replace strlcat() in longname construction
Date: Fri, 7 Aug 2026 11:41:39 +0000 [thread overview]
Message-ID: <20260807114139.1661-8-mahad.ibrahim.dev@gmail.com> (raw)
In-Reply-To: <20260807114139.1661-1-mahad.ibrahim.dev@gmail.com>
card->longname was assembled from a chain of strlcat() calls covering
the vendor or manufacturer name, the short name, the USB path and the
speed suffix. The return value of one of them was reused as the
offset for usb_make_path().
Keep the current length in len and write each piece at that offset,
with scnprintf() where a format is involved and strscpy() for the
plain speed suffixes. len is taken again after strim(), which
shortens the string in place, and again after usb_make_path(), which
writes into the buffer directly. Both would otherwise leave the
offset pointing at the wrong byte.
As in the hiface conversion, len now counts characters written rather
than requested, so the bounds check before usb_make_path() is always
true; in the truncated case it writes only the NUL terminator that is
already there. The strings produced for every combination of vendor,
manufacturer, short name and link speed are byte for byte the same as
before.
Signed-off-by: Mahad Ibrahim <mahad.ibrahim.dev@gmail.com>
---
sound/usb/card.c | 27 +++++++++++++++++----------
1 file changed, 17 insertions(+), 10 deletions(-)
diff --git a/sound/usb/card.c b/sound/usb/card.c
index 24112e491779..3c15a6862046 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -651,7 +651,7 @@ static void usb_audio_make_longname(struct usb_device *dev,
struct snd_card *card = chip->card;
const struct usb_audio_device_name *preset;
const char *s = NULL;
- int len;
+ int len = 0;
preset = lookup_device_name(chip->usb_id);
@@ -675,32 +675,39 @@ static void usb_audio_make_longname(struct usb_device *dev,
if (*card->longname) {
strim(card->longname);
+ len = strlen(card->longname);
if (*card->longname)
- strlcat(card->longname, " ", sizeof(card->longname));
+ len += scnprintf(card->longname + len,
+ sizeof(card->longname) - len, " ");
}
- strlcat(card->longname, card->shortname, sizeof(card->longname));
-
- len = strlcat(card->longname, " at ", sizeof(card->longname));
+ len += scnprintf(card->longname + len, sizeof(card->longname) - len,
+ "%s at ", card->shortname);
if (len < sizeof(card->longname))
usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
+ len = strlen(card->longname);
switch (snd_usb_get_speed(dev)) {
case USB_SPEED_LOW:
- strlcat(card->longname, ", low speed", sizeof(card->longname));
+ strscpy(card->longname + len, ", low speed",
+ sizeof(card->longname) - len);
break;
case USB_SPEED_FULL:
- strlcat(card->longname, ", full speed", sizeof(card->longname));
+ strscpy(card->longname + len, ", full speed",
+ sizeof(card->longname) - len);
break;
case USB_SPEED_HIGH:
- strlcat(card->longname, ", high speed", sizeof(card->longname));
+ strscpy(card->longname + len, ", high speed",
+ sizeof(card->longname) - len);
break;
case USB_SPEED_SUPER:
- strlcat(card->longname, ", super speed", sizeof(card->longname));
+ strscpy(card->longname + len, ", super speed",
+ sizeof(card->longname) - len);
break;
case USB_SPEED_SUPER_PLUS:
- strlcat(card->longname, ", super speed plus", sizeof(card->longname));
+ strscpy(card->longname + len, ", super speed plus",
+ sizeof(card->longname) - len);
break;
default:
break;
--
2.54.0
next prev parent reply other threads:[~2026-08-07 11:42 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-07 11:41 [PATCH 0/7] ALSA: remove remaining strlcat() users under sound/ Mahad Ibrahim
2026-08-07 11:41 ` [PATCH 1/7] ALSA: ump: replace strlcat() with strscpy() Mahad Ibrahim
2026-08-07 11:41 ` [PATCH 2/7] ALSA: ac97: replace strlcat() with scnprintf() Mahad Ibrahim
2026-08-07 11:41 ` [PATCH 3/7] ALSA: cmipci: replace strlcat() with strscpy() Mahad Ibrahim
2026-08-07 11:41 ` [PATCH 4/7] ALSA: caiaq: " Mahad Ibrahim
2026-08-07 11:41 ` [PATCH 5/7] ALSA: usb-audio: replace strlcat() with append_ctl_name() Mahad Ibrahim
2026-08-07 11:41 ` [PATCH 6/7] ALSA: hiface: replace strlcat() with scnprintf() Mahad Ibrahim
2026-08-07 11:41 ` Mahad Ibrahim [this message]
2026-08-07 12:15 ` [PATCH 0/7] ALSA: remove remaining strlcat() users under sound/ Takashi Iwai
2026-08-07 13:04 ` David Laight
2026-08-07 15:41 ` Mahad Ibrahim
2026-08-07 16:03 ` Takashi Iwai
2026-08-07 21:46 ` Kees Cook
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=20260807114139.1661-8-mahad.ibrahim.dev@gmail.com \
--to=mahad.ibrahim.dev@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=tiwai@suse.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox