ALSA: hda: generic: Check potential mixer name string truncation
authorTakashi Iwai <tiwai@suse.de>
Fri, 15 Sep 2023 08:28:02 +0000 (10:28 +0200)
committerTakashi Iwai <tiwai@suse.de>
Fri, 15 Sep 2023 11:22:16 +0000 (13:22 +0200)
add_control_with_pfx() constructs a mixer name element with the fixed
size, and it got compile warnings with -Wformat-truncation.

Although the size overflow is very unlikely, let's have a sanity check
of the string size and returns the error if it really doesn't fit
instead of silent truncation.

Link: https://lore.kernel.org/r/20230915082802.28684-14-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/pci/hda/hda_generic.c

index dbf7aa88e0e31a48a751e91761cfa644f98ab1c1..bf685d01259d30070aaf3ac7f3ed3204bc30c5bd 100644 (file)
@@ -998,7 +998,11 @@ static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
                                const char *sfx, int cidx, unsigned long val)
 {
        char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
-       snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
+       int len;
+
+       len = snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
+       if (snd_BUG_ON(len >= sizeof(name)))
+               return -EINVAL;
        if (!add_control(spec, type, name, cidx, val))
                return -ENOMEM;
        return 0;