ALSA: core: Use dev_name of card_dev as debugfs directory name

Message ID 20230912110113.3166-1-peter.ujfalusi@linux.intel.com
State New
Headers
Series ALSA: core: Use dev_name of card_dev as debugfs directory name |

Commit Message

Péter Ujfalusi Sept. 12, 2023, 11:01 a.m. UTC
  There is no need to use temporary string for the debugfs directory name as
we can use the device name of the card.

This change will also fixes the following compiler warning/error (W=1):

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

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.

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

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Suggested-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
---
 sound/core/init.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
  

Comments

Takashi Iwai Sept. 12, 2023, 1:31 p.m. UTC | #1
On Tue, 12 Sep 2023 13:01:13 +0200,
Peter Ujfalusi wrote:
> 
> There is no need to use temporary string for the debugfs directory name as
> we can use the device name of the card.
> 
> This change will also fixes the following compiler warning/error (W=1):
> 
> 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
> 
> 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.
> 
> The warnings got brought to light by a recent patch upstream:
> commit 6d4ab2e97dcf ("extrawarn: enable format and stringop overflow warnings in W=1")
> 
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Suggested-by: Takashi Iwai <tiwai@suse.de>
> Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>

Thanks, applied now.


Takashi
  

Patch

diff --git a/sound/core/init.c b/sound/core/init.c
index d61bde1225f2..22c0d217b860 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -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(dev_name(&card->card_dev),
+						sound_debugfs_root);
 #endif
 	return 0;