Skip to contentSkip to conversation

ALSA: core: Increase the name array size for debugfs directory name

PATCH In review Peter Ujfalusi 3 participants 4 messages Sep 12, 2023 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: core: Increase the name array size for debugfs directory name

The idx is guarantied to be less than SNDRV_CARDS (max 256 or 8) by the code in snd_card_init(), however the compiler does not see that. Compiling with W=1 results:

sound/core/init.c: In function ‘snd_card_init’:
sound/core/init.c:367:28: error: ‘%d’ directive writing between 1 and 10 bytes into a region of size 4 [-Werror=format-overflow=]
367 | sprintf(name, "card%d", idx);
| ^~
sound/core/init.c:367:23: note: directive argument in the range [0, 2147483646]

  367 |         sprintf(name, "card%d", idx);
      |                       ^~~~~~~~
sound/core/init.c:367:9: note: ‘sprintf’ output between 6 and 15 bytes into a destination of size 8
  367 |         sprintf(name, "card%d", idx);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors

While the code is correct, we need to silence the compiler somehow. It could be done by limiting the range in sprintf like sprintf(name, "card%d", idx % SNDRV_CARDS); sprintf(name, "card%hhd", idx);
etc

These are too workaroundish. Increase the name array to 15 instead which looks better and only adds 7 bytes on stack.

The warnings got brought to light by a recent patch upstream:
commit 6d4ab2e97dcf ("extrawarn: enable format and stringop overflow warnings in W=1")

---
Hi,

The mentioned commit causes other build failures with W=1 at least in sound/usb/mixer_scarlett_gen2.c
sound/usb/mixer.c
sound/soc/codecs/hdac_hdmi.c
sound/hda/intel-sdw-acpi.c

Some of them are also false and we need to find a workaround, but I think the scarlett case might be valid.

Regards,
Peter

 sound/core/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/init.c +1 -1
index d61bde1225f2..d8a13a76d241 100644
@@ -279,7 +279,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
 {
 	int err;
 #ifdef CONFIG_SND_DEBUG
-	char name[8];
+	char name[15];
 #endif
 
 	if (extra_size > 0)
Signature · 1 line
2.42.0
#1 lore raw
Quoted 8 lines · Peter Ujfalusi

While the code is correct, we need to silence the compiler somehow. It could be done by limiting the range in sprintf like sprintf(name, "card%d", idx % SNDRV_CARDS); sprintf(name, "card%hhd", idx);
etc

These are too workaroundish. Increase the name array to 15 instead which looks better and only adds 7 bytes on stack.

It looks like we use the same string for kobject_set_name(), so maybe this would work as well:

sound/core/init.c +2 -5
@@ -278,9 +278,6 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
                         size_t extra_size)
 {
        int err;
-#ifdef CONFIG_SND_DEBUG
-       char name[8];
-#endif
 
        if (extra_size > 0)
                card->private_data = (char *)card + sizeof(struct snd_card);
@@ -364,8 +361,8 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
        }
 
 #ifdef CONFIG_SND_DEBUG
-       sprintf(name, "card%d", idx);
-       card->debugfs_root = debugfs_create_dir(name, sound_debugfs_root);
+       card->debugfs_root = debugfs_create_dir(kobject_name(&card->card_dev.kobj),
+                                               sound_debugfs_root);
 #endif
        return 0;
 

Arnd

#2 lore raw
Quoted 28 lines · Arnd Bergmann
Quoted 8 lines · Peter Ujfalusi

While the code is correct, we need to silence the compiler somehow. It could be done by limiting the range in sprintf like sprintf(name, "card%d", idx % SNDRV_CARDS); sprintf(name, "card%hhd", idx);
etc

These are too workaroundish. Increase the name array to 15 instead which looks better and only adds 7 bytes on stack.

It looks like we use the same string for kobject_set_name(), so maybe this would work as well:

sound/core/init.c +0 -3
@@ -278,9 +278,6 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
                         size_t extra_size)
 {
        int err;
-#ifdef CONFIG_SND_DEBUG
-       char name[8];
-#endif
 
        if (extra_size > 0)
                card->private_data = (char *)card + sizeof(struct snd_card);

sound/core/init.c static int snd_card_init(struct snd_card *card, struct device *parent, · line 365

Quoted 7 lines
sound/core/init.c +2 -2
@@ -364,8 +361,8 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
        }
 
 #ifdef CONFIG_SND_DEBUG
-       sprintf(name, "card%d", idx);
-       card->debugfs_root = debugfs_create_dir(name, sound_debugfs_root);
+       card->debugfs_root = debugfs_create_dir(kobject_name(&card->card_dev.kobj),
+                                               sound_debugfs_root);

The idea looks neat, but I suppose it's better with dev_name(&card->card_dev) instead?

thanks,

Takashi

#3 lore raw
Quoted 32 lines · Takashi Iwai
Quoted 30 lines · Arnd Bergmann
Quoted 8 lines · Peter Ujfalusi

While the code is correct, we need to silence the compiler somehow. It could be done by limiting the range in sprintf like sprintf(name, "card%d", idx % SNDRV_CARDS); sprintf(name, "card%hhd", idx);
etc

These are too workaroundish. Increase the name array to 15 instead which looks better and only adds 7 bytes on stack.

It looks like we use the same string for kobject_set_name(), so maybe this would work as well:

sound/core/init.c +0 -3
@@ -278,9 +278,6 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
                         size_t extra_size)
 {
        int err;
-#ifdef CONFIG_SND_DEBUG
-       char name[8];
-#endif
 
        if (extra_size > 0)
                card->private_data = (char *)card + sizeof(struct snd_card);
@@ -364,8 +361,8 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
        }
 
Quoted 8 lines

sound/core/init.c static int snd_card_init(struct snd_card *card, struct device *parent, · line 365

Quoted 5 lines
sound/core/init.c +2 -2
 #ifdef CONFIG_SND_DEBUG
-       sprintf(name, "card%d", idx);
-       card->debugfs_root = debugfs_create_dir(name, sound_debugfs_root);
+       card->debugfs_root = debugfs_create_dir(kobject_name(&card->card_dev.kobj),
+                                               sound_debugfs_root);

The idea looks neat, but I suppose it's better with dev_name(&card->card_dev) instead?

Yes, this looks better, I will send a new patch in a minute.

Quoted 4 lines

thanks,

Takashi

Signature · 1 line
Péter