usb_audio_make_longname() builds card->longname incrementally from the
vendor/manufacturer name, shortname, USB device path, and USB speed using
strscpy(), strlcat(), and manual buffer offset calculations for
usb_make_path().

In preparation for removing the deprecated strlcat() API[1], refactor
usb_audio_make_longname() to use struct seq_buf to track the buffer
position and remaining space across these operations.

Link: https://github.com/KSPP/linux/issues/370 [1]
Cc: [email protected]
Assisted-by: Gemini:3.5-pro [git, make, checkpatch]
Signed-off-by: Bill Wendling <[email protected]>
---
Cc: Russell King <[email protected]>
Cc: Huacai Chen <[email protected]>
Cc: WANG Xuerui <[email protected]>
Cc: Thomas Bogendoerfer <[email protected]>
Cc: "James E.J. Bottomley" <[email protected]>
Cc: Helge Deller <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>
Cc: Ian Abbott <[email protected]>
Cc: H Hartley Sweeten <[email protected]>
Cc: Tony Luck <[email protected]>
Cc: Maarten Lankhorst <[email protected]>
Cc: Maxime Ripard <[email protected]>
Cc: Thomas Zimmermann <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: "Thomas Hellström" <[email protected]>
Cc: Rodrigo Vivi <[email protected]>
Cc: Dmitry Torokhov <[email protected]>
Cc: Matthias Schwarzott <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: Tony Nguyen <[email protected]>
Cc: Przemek Kitszel <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Arend van Spriel <[email protected]>
Cc: Rob Herring <[email protected]>
Cc: Saravana Kannan <[email protected]>
Cc: Krzysztof Kozlowski <[email protected]>
Cc: Sylwester Nawrocki <[email protected]>
Cc: Peter Griffin <[email protected]>
Cc: Alim Akhtar <[email protected]>
Cc: Linus Walleij <[email protected]>
Cc: Anil Gurumurthy <[email protected]>
Cc: Sudarsana Kalluru <[email protected]>
Cc: "Martin K. Petersen" <[email protected]>
Cc: Trond Myklebust <[email protected]>
Cc: Anna Schumaker <[email protected]>
Cc: Mike Marshall <[email protected]>
Cc: Martin Brandenburg <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: Jiri Pirko <[email protected]>
Cc: Simon Horman <[email protected]>
Cc: Chuck Lever <[email protected]>
Cc: Jeff Layton <[email protected]>
Cc: NeilBrown <[email protected]>
Cc: Olga Kornievskaia <[email protected]>
Cc: Dai Ngo <[email protected]>
Cc: Tom Talpey <[email protected]>
Cc: Jaroslav Kysela <[email protected]>
Cc: Takashi Iwai <[email protected]>
Cc: Bill Wendling <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: "Mike Rapoport (Microsoft)" <[email protected]>
Cc: Kanglong Wang <[email protected]>
Cc: Tiezhu Yang <[email protected]>
Cc: Qiang Ma <[email protected]>
Cc: Randy Dunlap <[email protected]>
Cc: Pengpeng Hou <[email protected]>
Cc: Ard Biesheuvel <[email protected]>
Cc: Breno Leitao <[email protected]>
Cc: Thorsten Blum <[email protected]>
Cc: Harshit Mogalapalli <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Ashutosh Desai <[email protected]>
Cc: Imre Deak <[email protected]>
Cc: Dmitry Baryshkov <[email protected]>
Cc: Johan Hovold <[email protected]>
Cc: Johannes Berg <[email protected]>
Cc: Miri Korenblit <[email protected]>
Cc: Alexander Stein <[email protected]>
Cc: Cryolitia PukNgae <[email protected]>
Cc: Jiaming Zhang <[email protected]>
Cc: Will Porter <[email protected]>
Cc: Cen Zhang <[email protected]>
Cc: "Cássio Gabriel" <[email protected]>
Cc: Rong Zhang <[email protected]>
Cc: Arun Raghavan <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
---
 sound/usb/card.c | 58 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 39 insertions(+), 19 deletions(-)

diff --git a/sound/usb/card.c b/sound/usb/card.c
index 9307da95efbe..bdca8085fca6 100644
--- a/sound/usb/card.c
+++ b/sound/usb/card.c
@@ -25,6 +25,7 @@
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/seq_buf.h>
 #include <linux/ctype.h>
 #include <linux/usb.h>
 #include <linux/moduleparam.h>
@@ -651,7 +652,9 @@ 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;
+       struct seq_buf sb;
+       char *buf;
+       size_t size;
 
        preset = lookup_device_name(chip->usb_id);
 
@@ -667,44 +670,61 @@ static void usb_audio_make_longname(struct usb_device 
*dev,
                s = preset->vendor_name;
        else if (quirk && quirk->vendor_name)
                s = quirk->vendor_name;
-       *card->longname = 0;
-       if (s && *s)
-               strscpy(card->longname, s);
-       else if (dev->manufacturer && *dev->manufacturer)
-               strscpy(card->longname, dev->manufacturer);
 
-       if (*card->longname) {
-               strim(card->longname);
-               if (*card->longname)
-                       strlcat(card->longname, " ", sizeof(card->longname));
+       seq_buf_init(&sb, card->longname, sizeof(card->longname));
+
+       if (s && *s)
+               seq_buf_puts(&sb, s);
+       else if (dev->manufacturer && *dev->manufacturer)
+               seq_buf_puts(&sb, dev->manufacturer);
+
+       if (seq_buf_used(&sb)) {
+               char *trimmed;
+
+               seq_buf_str(&sb);
+               trimmed = strim(card->longname);
+               if (trimmed != card->longname)
+                       memmove(card->longname, trimmed, strlen(trimmed) + 1);
+               sb.len = strlen(card->longname);
+               if (sb.len)
+                       seq_buf_putc(&sb, ' ');
        }
 
-       strlcat(card->longname, card->shortname, sizeof(card->longname));
+       seq_buf_puts(&sb, card->shortname);
 
-       len = strlcat(card->longname, " at ", sizeof(card->longname));
+       seq_buf_puts(&sb, " at ");
 
-       if (len < sizeof(card->longname))
-               usb_make_path(dev, card->longname + len, sizeof(card->longname) 
- len);
+       size = seq_buf_get_buf(&sb, &buf);
+       if (size > 0) {
+               int path_len = usb_make_path(dev, buf, size);
+
+               if (path_len >= 0)
+                       seq_buf_commit(&sb, path_len);
+               else
+                       seq_buf_set_overflow(&sb);
+       }
 
        switch (snd_usb_get_speed(dev)) {
        case USB_SPEED_LOW:
-               strlcat(card->longname, ", low speed", sizeof(card->longname));
+               seq_buf_puts(&sb, ", low speed");
                break;
        case USB_SPEED_FULL:
-               strlcat(card->longname, ", full speed", sizeof(card->longname));
+               seq_buf_puts(&sb, ", full speed");
                break;
        case USB_SPEED_HIGH:
-               strlcat(card->longname, ", high speed", sizeof(card->longname));
+               seq_buf_puts(&sb, ", high speed");
                break;
        case USB_SPEED_SUPER:
-               strlcat(card->longname, ", super speed", 
sizeof(card->longname));
+               seq_buf_puts(&sb, ", super speed");
                break;
        case USB_SPEED_SUPER_PLUS:
-               strlcat(card->longname, ", super speed plus", 
sizeof(card->longname));
+               seq_buf_puts(&sb, ", super speed plus");
                break;
        default:
                break;
        }
+
+       seq_buf_str(&sb);
 }
 
 static void snd_usb_init_quirk_flags(int idx, struct snd_usb_audio *chip)
-- 
2.55.0.1032.g73a4cd73de-goog

Reply via email to