Skip to contentSkip to conversation

ALSA: Fix -Wformat-truncation warnings

PATCH In review Takashi Iwai 2 participants 16 messages Sep 15, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover ALSA: Fix -Wformat-truncation warnings

Hi,

the recent change introduced -Wformat-truncation for the build with W=1, and this leads to many warnings. Most of them are false-positive, and they can be suppressed by replacing snprintf() with scnprintf() for explicit truncation. A couple of other code deserve for a slight improvement.

Takashi

===

Takashi Iwai (13):
ALSA: seq: ump: Fix -Wformat-truncation warning
ALSA: seq: midi: Fix -Wformat-truncation warning
ALSA: usb-audio: scarlett_gen2: Fix -Wformat-truncation warning
ALSA: caiaq: Fix -Wformat-truncation warning
ALSA: sscape: Fix -Wformat-truncation warning
ALSA: cs4236: Fix -Wformat-truncation warning
ALSA: es1688: Fix -Wformat-truncation warning
ALSA: opti9x: Fix -Wformat-truncation warning
ALSA: xen: Fix -Wformat-truncation warning
ALSA: firewire: Fix -Wformat-truncation warning for longname string
ALSA: firewire: Fix -Wformat-truncation warning for MIDI stream names
ALSA: cmipci: Fix -Wformat-truncation warning
ALSA: hda: generic: Check potential mixer name string truncation

 sound/core/seq/seq_midi.c                 |  4 ++--
 sound/core/seq/seq_ump_client.c           |  2 +-
 sound/firewire/bebob/bebob_midi.c         |  6 +++---
 sound/firewire/dice/dice-midi.c           |  4 ++--
 sound/firewire/digi00x/digi00x-midi.c     | 14 +++++++-------
 sound/firewire/fireface/ff-midi.c         |  4 ++--
 sound/firewire/fireworks/fireworks.c      | 10 +++++-----
 sound/firewire/fireworks/fireworks_midi.c |  4 ++--
 sound/firewire/motu/motu-midi.c           |  4 ++--
 sound/firewire/oxfw/oxfw-midi.c           |  6 +++---
 sound/firewire/oxfw/oxfw.c                | 10 +++++-----
 sound/firewire/tascam/tascam-midi.c       | 12 ++++++------
 sound/isa/cs423x/cs4236.c                 | 14 +++++++-------
 sound/isa/es1688/es1688.c                 |  6 +++---
 sound/isa/opti9xx/miro.c                  |  8 ++++----
 sound/isa/opti9xx/opti92x-ad1848.c        | 16 ++++++++--------
 sound/isa/sscape.c                        |  2 +-
 sound/pci/cmipci.c                        |  8 +++++---
 sound/pci/hda/hda_generic.c               |  6 +++++-
 sound/usb/caiaq/device.c                  |  2 +-
 sound/usb/mixer_scarlett_gen2.c           |  4 ++--
 sound/xen/xen_snd_front_cfg.c             |  2 +-
 22 files changed, 77 insertions(+), 71 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH 1/13 ALSA: seq: ump: Fix -Wformat-truncation warning

The filling of a port name string got a warning with W=1 due to the potentially too long group name. Add the string precision to limit the size.

Fixes 81fd444aa371 ("ALSA: seq: Bind UMP device")
---
 sound/core/seq/seq_ump_client.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/seq/seq_ump_client.c +1 -1
index a60e3f069a80..2db371d79930 100644
@@ -207,7 +207,7 @@ static void fill_port_info(struct snd_seq_port_info *port,
 		SNDRV_SEQ_PORT_TYPE_PORT;
 	port->midi_channels = 16;
 	if (*group->name)
-		snprintf(port->name, sizeof(port->name), "Group %d (%s)",
+		snprintf(port->name, sizeof(port->name), "Group %d (%.53s)",
 			 group->group + 1, group->name);
 	else
 		sprintf(port->name, "Group %d", group->group + 1);
Signature · 1 line
2.35.3
#2 lore raw

PATCH 2/13 ALSA: seq: midi: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The compile warnings with -Wformat-truncation appearing at snd_seq_midisynth_probe() in seq_midi.c are false-positive; those must fit within the given string size.

For suppressing the warning, replace snprintf() with scnprintf(). As stated in the above, truncation doesn't matter.

---
 sound/core/seq/seq_midi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
sound/core/seq/seq_midi.c +2 -2
index 44302d98950e..18320a248aa7 100644
@@ -349,9 +349,9 @@ snd_seq_midisynth_probe(struct device *_dev)
 		if (! port->name[0]) {
 			if (info->name[0]) {
 				if (ports > 1)
-					snprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
+					scnprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
 				else
-					snprintf(port->name, sizeof(port->name), "%s", info->name);
+					scnprintf(port->name, sizeof(port->name), "%s", info->name);
 			} else {
 				/* last resort */
 				if (ports > 1)
Signature · 1 line
2.35.3
#3 lore raw

PATCH 3/13 ALSA: usb-audio: scarlett_gen2: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The recent enablement of -Wformat-truncation leads to a false-positive warning for mixer_scarlett_gen2.c.

For suppressing the warning, replace snprintf() with scnprintf(). As stated in the above, truncation doesn't matter.

---
 sound/usb/mixer_scarlett_gen2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
sound/usb/mixer_scarlett_gen2.c +2 -2
index 9d11bb08667e..5c6f50f38840 100644
@@ -3218,8 +3218,8 @@ static int scarlett2_add_line_in_ctls(struct usb_mixer_interface *mixer)
 			int from = i * info->inputs_per_phantom + 1;
 			int to = (i + 1) * info->inputs_per_phantom;
 
-			snprintf(s, sizeof(s), fmt2, from, to,
-				 "Phantom Power", "Switch");
+			scnprintf(s, sizeof(s), fmt2, from, to,
+				  "Phantom Power", "Switch");
 			err = scarlett2_add_new_ctl(
 				mixer, &scarlett2_phantom_ctl,
 				i, 1, s, &private->phantom_ctls[i]);
Signature · 1 line
2.35.3
#4 lore raw

PATCH 4/13 ALSA: caiaq: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The filling of card->longname can be gracefully truncated, as it's only informative. Use scnprintf() and suppress the superfluous compile warning with -Wformat-truncation.

---
 sound/usb/caiaq/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/usb/caiaq/device.c +1 -1
index 49f63f878e6f..b5cbf1f195c4 100644
@@ -485,7 +485,7 @@ static int init_card(struct snd_usb_caiaqdev *cdev)
 	}
 
 	usb_make_path(usb_dev, usbpath, sizeof(usbpath));
-	snprintf(card->longname, sizeof(card->longname), "%s %s (%s)",
+	scnprintf(card->longname, sizeof(card->longname), "%s %s (%s)",
 		       cdev->vendor_name, cdev->product_name, usbpath);
 
 	setup_card(cdev);
Signature · 1 line
2.35.3
#5 lore raw

PATCH 5/13 ALSA: sscape: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The warning with -Wformat-truncation at sscape_upload_microcode() is false-positive; the version number can be only a single digit, hence fitting with the given string size.

For suppressing the warning, replace snprintf() with scnprintf(). As stated in the above, truncation doesn't matter.

---
 sound/isa/sscape.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/isa/sscape.c +1 -1
index 0bc0025f7c19..cc56fafd27b1 100644
@@ -557,7 +557,7 @@ static int sscape_upload_microcode(struct snd_card *card, int version)
 	char name[14];
 	int err;
 
-	snprintf(name, sizeof(name), "sndscape.co%d", version);
+	scnprintf(name, sizeof(name), "sndscape.co%d", version);
 
 	err = request_firmware(&init_fw, name, card->dev);
 	if (err < 0) {
Signature · 1 line
2.35.3
#6 lore raw

PATCH 6/13 ALSA: cs4236: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The filling of card->longname can be gracefully truncated, as it's only informative. Use scnprintf() and suppress the superfluous compile warning with -Wformat-truncation.

---
 sound/isa/cs423x/cs4236.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
sound/isa/cs423x/cs4236.c +7 -7
index 10112e1bb25d..7226cbf2d7de 100644
@@ -367,14 +367,14 @@ static int snd_cs423x_probe(struct snd_card *card, int dev)
 	strscpy(card->driver, chip->pcm->name, sizeof(card->driver));
 	strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname));
 	if (dma2[dev] < 0)
-		snprintf(card->longname, sizeof(card->longname),
-			 "%s at 0x%lx, irq %i, dma %i",
-			 chip->pcm->name, chip->port, irq[dev], dma1[dev]);
+		scnprintf(card->longname, sizeof(card->longname),
+			  "%s at 0x%lx, irq %i, dma %i",
+			  chip->pcm->name, chip->port, irq[dev], dma1[dev]);
 	else
-		snprintf(card->longname, sizeof(card->longname),
-			 "%s at 0x%lx, irq %i, dma %i&%d",
-			 chip->pcm->name, chip->port, irq[dev], dma1[dev],
-			 dma2[dev]);
+		scnprintf(card->longname, sizeof(card->longname),
+			  "%s at 0x%lx, irq %i, dma %i&%d",
+			  chip->pcm->name, chip->port, irq[dev], dma1[dev],
+			  dma2[dev]);
 
 	err = snd_wss_timer(chip, 0);
 	if (err < 0)
Signature · 1 line
2.35.3
#7 lore raw

PATCH 7/13 ALSA: es1688: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The filling of card->longname can be gracefully truncated, as it's only informative. Use scnprintf() and suppress the superfluous compile warning with -Wformat-truncation.

---
 sound/isa/es1688/es1688.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
sound/isa/es1688/es1688.c +3 -3
index f935b56eeec7..97728bf45474 100644
@@ -130,9 +130,9 @@ static int snd_es1688_probe(struct snd_card *card, unsigned int n)
 
 	strscpy(card->driver, "ES1688", sizeof(card->driver));
 	strscpy(card->shortname, chip->pcm->name, sizeof(card->shortname));
-	snprintf(card->longname, sizeof(card->longname),
-		"%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port,
-		 chip->irq, chip->dma8);
+	scnprintf(card->longname, sizeof(card->longname),
+		  "%s at 0x%lx, irq %i, dma %i", chip->pcm->name, chip->port,
+		  chip->irq, chip->dma8);
 
 	if (fm_port[n] == SNDRV_AUTO_PORT)
 		fm_port[n] = port[n];	/* share the same port */
Signature · 1 line
2.35.3
#8 lore raw

PATCH 8/13 ALSA: opti9x: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The filling of card->longname can be gracefully truncated, as it's only informative. Use scnprintf() and suppress the superfluous compile warning with -Wformat-truncation.

---
 sound/isa/opti9xx/miro.c           |  8 ++++----
 sound/isa/opti9xx/opti92x-ad1848.c | 16 ++++++++--------
 2 files changed, 12 insertions(+), 12 deletions(-)
sound/isa/opti9xx/miro.c +4 -4
index 59242baed576..59792f2fada1 100644
@@ -1344,10 +1344,10 @@ static int snd_miro_probe(struct snd_card *card)
 	}
 
 	strcpy(card->driver, "miro");
-	snprintf(card->longname, sizeof(card->longname),
-		 "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
-		 card->shortname, miro->name, codec->pcm->name,
-		 miro->wss_base + 4, miro->irq, miro->dma1, miro->dma2);
+	scnprintf(card->longname, sizeof(card->longname),
+		  "%s: OPTi%s, %s at 0x%lx, irq %d, dma %d&%d",
+		  card->shortname, miro->name, codec->pcm->name,
+		  miro->wss_base + 4, miro->irq, miro->dma1, miro->dma2);
 
 	if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
 		rmidi = NULL;
sound/isa/opti9xx/opti92x-ad1848.c +8 -8
index 4beeb32fe2a7..c33f67dd5133 100644
@@ -859,15 +859,15 @@ static int snd_opti9xx_probe(struct snd_card *card)
 	strcpy(card->driver, chip->name);
 	sprintf(card->shortname, "OPTi %s", card->driver);
 #if defined(CS4231) || defined(OPTi93X)
-	snprintf(card->longname, sizeof(card->longname),
-		 "%s, %s at 0x%lx, irq %d, dma %d&%d",
-		 card->shortname, codec->pcm->name,
-		 chip->wss_base + 4, irq, dma1, xdma2);
+	scnprintf(card->longname, sizeof(card->longname),
+		  "%s, %s at 0x%lx, irq %d, dma %d&%d",
+		  card->shortname, codec->pcm->name,
+		  chip->wss_base + 4, irq, dma1, xdma2);
 #else
-	snprintf(card->longname, sizeof(card->longname),
-		 "%s, %s at 0x%lx, irq %d, dma %d",
-		 card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
-		 dma1);
+	scnprintf(card->longname, sizeof(card->longname),
+		  "%s, %s at 0x%lx, irq %d, dma %d",
+		  card->shortname, codec->pcm->name, chip->wss_base + 4, irq,
+		  dma1);
 #endif	/* CS4231 || OPTi93X */
 
 	if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
Signature · 1 line
2.35.3
#9 lore raw

PATCH 9/13 ALSA: xen: Fix -Wformat-truncation warning

In reply to Takashi Iwai

The compile warning with -Wformat-truncation at xen_snd_front_cfg_card() is false-positive; the loop can be only for SNDRV_PCM_DEVICES which is at most 32.

For suppressing the warning, replace snprintf() with scnprintf(). As stated in the above, truncation doesn't matter.

---
 sound/xen/xen_snd_front_cfg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/xen/xen_snd_front_cfg.c +1 -1
index 63b0398c3276..55ecf766ca67 100644
@@ -483,7 +483,7 @@ int xen_snd_front_cfg_card(struct xen_snd_front_info *front_info,
 	*stream_cnt = 0;
 	num_devices = 0;
 	do {
-		snprintf(node, sizeof(node), "%d", num_devices);
+		scnprintf(node, sizeof(node), "%d", num_devices);
 		if (!xenbus_exists(XBT_NIL, xb_dev->nodename, node))
 			break;
 
Signature · 1 line
2.35.3
#10 lore raw

PATCH 10/13 ALSA: firewire: Fix -Wformat-truncation warning for longname string

In reply to Takashi Iwai

The filling of card->longname can be gracefully truncated, as it's only informative. Use scnprintf() and suppress the superfluous compile warning with -Wformat-truncation.

---
 sound/firewire/fireworks/fireworks.c | 10 +++++-----
 sound/firewire/oxfw/oxfw.c           | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)
sound/firewire/fireworks/fireworks.c +5 -5
index dd4298876ac0..e3ed4e094ccd 100644
@@ -93,11 +93,11 @@ get_hardware_info(struct snd_efw *efw)
 	strcpy(efw->card->driver, "Fireworks");
 	strcpy(efw->card->shortname, hwinfo->model_name);
 	strcpy(efw->card->mixername, hwinfo->model_name);
-	snprintf(efw->card->longname, sizeof(efw->card->longname),
-		 "%s %s v%s, GUID %08x%08x at %s, S%d",
-		 hwinfo->vendor_name, hwinfo->model_name, version,
-		 hwinfo->guid_hi, hwinfo->guid_lo,
-		 dev_name(&efw->unit->device), 100 << fw_dev->max_speed);
+	scnprintf(efw->card->longname, sizeof(efw->card->longname),
+		  "%s %s v%s, GUID %08x%08x at %s, S%d",
+		  hwinfo->vendor_name, hwinfo->model_name, version,
+		  hwinfo->guid_hi, hwinfo->guid_lo,
+		  dev_name(&efw->unit->device), 100 << fw_dev->max_speed);
 
 	if (hwinfo->flags & BIT(FLAG_RESP_ADDR_CHANGABLE))
 		efw->resp_addr_changable = true;
sound/firewire/oxfw/oxfw.c +5 -5
index 63d40f1a914f..241a697ce26b 100644
@@ -108,11 +108,11 @@ static int name_card(struct snd_oxfw *oxfw, const struct ieee1394_device_id *ent
 	strcpy(oxfw->card->mixername, m);
 	strcpy(oxfw->card->shortname, m);
 
-	snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
-		 "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
-		 v, m, firmware >> 20, firmware & 0xffff,
-		 fw_dev->config_rom[3], fw_dev->config_rom[4],
-		 dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
+	scnprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
+		  "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
+		  v, m, firmware >> 20, firmware & 0xffff,
+		  fw_dev->config_rom[3], fw_dev->config_rom[4],
+		  dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
 end:
 	return err;
 }
Signature · 1 line
2.35.3
maintainer Reviewed-by Tested-by on v1 10/13
#11 lore raw

Re: [PATCH 10/13] ALSA: firewire: Fix -Wformat-truncation warning for longname string

Hi,

Quoted 9 lines · Takashi Iwai

The filling of card->longname can be gracefully truncated, as it's only informative. Use scnprintf() and suppress the superfluous compile warning with -Wformat-truncation.

---
 sound/firewire/fireworks/fireworks.c | 10 +++++-----
 sound/firewire/oxfw/oxfw.c           | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)
Quoted 44 lines
sound/firewire/fireworks/fireworks.c +5 -5
index dd4298876ac0..e3ed4e094ccd 100644
@@ -93,11 +93,11 @@ get_hardware_info(struct snd_efw *efw)
 	strcpy(efw->card->driver, "Fireworks");
 	strcpy(efw->card->shortname, hwinfo->model_name);
 	strcpy(efw->card->mixername, hwinfo->model_name);
-	snprintf(efw->card->longname, sizeof(efw->card->longname),
-		 "%s %s v%s, GUID %08x%08x at %s, S%d",
-		 hwinfo->vendor_name, hwinfo->model_name, version,
-		 hwinfo->guid_hi, hwinfo->guid_lo,
-		 dev_name(&efw->unit->device), 100 << fw_dev->max_speed);
+	scnprintf(efw->card->longname, sizeof(efw->card->longname),
+		  "%s %s v%s, GUID %08x%08x at %s, S%d",
+		  hwinfo->vendor_name, hwinfo->model_name, version,
+		  hwinfo->guid_hi, hwinfo->guid_lo,
+		  dev_name(&efw->unit->device), 100 << fw_dev->max_speed);
 
 	if (hwinfo->flags & BIT(FLAG_RESP_ADDR_CHANGABLE))
 		efw->resp_addr_changable = true;
sound/firewire/oxfw/oxfw.c +5 -5
index 63d40f1a914f..241a697ce26b 100644
@@ -108,11 +108,11 @@ static int name_card(struct snd_oxfw *oxfw, const struct ieee1394_device_id *ent
 	strcpy(oxfw->card->mixername, m);
 	strcpy(oxfw->card->shortname, m);
 
-	snprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
-		 "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
-		 v, m, firmware >> 20, firmware & 0xffff,
-		 fw_dev->config_rom[3], fw_dev->config_rom[4],
-		 dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
+	scnprintf(oxfw->card->longname, sizeof(oxfw->card->longname),
+		  "%s %s (OXFW%x %04x), GUID %08x%08x at %s, S%d",
+		  v, m, firmware >> 20, firmware & 0xffff,
+		  fw_dev->config_rom[3], fw_dev->config_rom[4],
+		  dev_name(&oxfw->unit->device), 100 << fw_dev->max_speed);
 end:
 	return err;
 }
Signature · 1 line
2.35.3

Thanks

Takashi Sakamoto

#12 lore raw

PATCH 11/13 ALSA: firewire: Fix -Wformat-truncation warning for MIDI stream names

In reply to Takashi Iwai

The compile warnings at filling MIDI stream name strings are all false-positive; the number of streams can't go so high.

For suppressing the warning, replace snprintf() with scnprintf(). As stated in the above, truncation doesn't matter.

---
 sound/firewire/bebob/bebob_midi.c         |  6 +++---
 sound/firewire/dice/dice-midi.c           |  4 ++--
 sound/firewire/digi00x/digi00x-midi.c     | 14 +++++++-------
 sound/firewire/fireface/ff-midi.c         |  4 ++--
 sound/firewire/fireworks/fireworks_midi.c |  4 ++--
 sound/firewire/motu/motu-midi.c           |  4 ++--
 sound/firewire/oxfw/oxfw-midi.c           |  6 +++---
 sound/firewire/tascam/tascam-midi.c       | 12 ++++++------
 8 files changed, 27 insertions(+), 27 deletions(-)
sound/firewire/bebob/bebob_midi.c +3 -3
index 6f597d03e7c1..b1425bf98c3b 100644
@@ -84,9 +84,9 @@ static void set_midi_substream_names(struct snd_bebob *bebob,
 	struct snd_rawmidi_substream *subs;
 
 	list_for_each_entry(subs, &str->substreams, list) {
-		snprintf(subs->name, sizeof(subs->name),
-			 "%s MIDI %d",
-			 bebob->card->shortname, subs->number + 1);
+		scnprintf(subs->name, sizeof(subs->name),
+			  "%s MIDI %d",
+			  bebob->card->shortname, subs->number + 1);
 	}
 }
 
sound/firewire/dice/dice-midi.c +2 -2
index 4c2998034313..78988e44b8bc 100644
@@ -88,8 +88,8 @@ static void set_midi_substream_names(struct snd_dice *dice,
 	struct snd_rawmidi_substream *subs;
 
 	list_for_each_entry(subs, &str->substreams, list) {
-		snprintf(subs->name, sizeof(subs->name),
-			 "%s MIDI %d", dice->card->shortname, subs->number + 1);
+		scnprintf(subs->name, sizeof(subs->name),
+			  "%s MIDI %d", dice->card->shortname, subs->number + 1);
 	}
 }
 
sound/firewire/digi00x/digi00x-midi.c +7 -7
index 68eb8c39afa6..8f4bace16050 100644
@@ -100,14 +100,14 @@ static void set_substream_names(struct snd_dg00x *dg00x,
 
 		list_for_each_entry(subs, &str->substreams, list) {
 			if (!is_console) {
-				snprintf(subs->name, sizeof(subs->name),
-					 "%s MIDI %d",
-					 dg00x->card->shortname,
-					 subs->number + 1);
+				scnprintf(subs->name, sizeof(subs->name),
+					  "%s MIDI %d",
+					  dg00x->card->shortname,
+					  subs->number + 1);
 			} else {
-				snprintf(subs->name, sizeof(subs->name),
-					 "%s control",
-					 dg00x->card->shortname);
+				scnprintf(subs->name, sizeof(subs->name),
+					  "%s control",
+					  dg00x->card->shortname);
 			}
 		}
 	}
sound/firewire/fireface/ff-midi.c +2 -2
index 25821d186b87..da3054fdcc7d 100644
@@ -79,8 +79,8 @@ static void set_midi_substream_names(struct snd_rawmidi_str *stream,
 	struct snd_rawmidi_substream *substream;
 
 	list_for_each_entry(substream, &stream->substreams, list) {
-		snprintf(substream->name, sizeof(substream->name),
-			 "%s MIDI %d", name, substream->number + 1);
+		scnprintf(substream->name, sizeof(substream->name),
+			  "%s MIDI %d", name, substream->number + 1);
 	}
 }
 
sound/firewire/fireworks/fireworks_midi.c +2 -2
index 84621e356848..350bf4d299c2 100644
@@ -84,8 +84,8 @@ static void set_midi_substream_names(struct snd_efw *efw,
 	struct snd_rawmidi_substream *subs;
 
 	list_for_each_entry(subs, &str->substreams, list) {
-		snprintf(subs->name, sizeof(subs->name),
-			 "%s MIDI %d", efw->card->shortname, subs->number + 1);
+		scnprintf(subs->name, sizeof(subs->name),
+			  "%s MIDI %d", efw->card->shortname, subs->number + 1);
 	}
 }
 
sound/firewire/motu/motu-midi.c +2 -2
index 2365f7dfde26..eebc7e790ee2 100644
@@ -88,8 +88,8 @@ static void set_midi_substream_names(struct snd_motu *motu,
 	struct snd_rawmidi_substream *subs;
 
 	list_for_each_entry(subs, &str->substreams, list) {
-		snprintf(subs->name, sizeof(subs->name),
-			 "%s MIDI %d", motu->card->shortname, subs->number + 1);
+		scnprintf(subs->name, sizeof(subs->name),
+			  "%s MIDI %d", motu->card->shortname, subs->number + 1);
 	}
 }
 
sound/firewire/oxfw/oxfw-midi.c +3 -3
index 775cba3f1f02..c215fa6f7a03 100644
@@ -129,9 +129,9 @@ static void set_midi_substream_names(struct snd_oxfw *oxfw,
 	struct snd_rawmidi_substream *subs;
 
 	list_for_each_entry(subs, &str->substreams, list) {
-		snprintf(subs->name, sizeof(subs->name),
-			 "%s MIDI %d",
-			 oxfw->card->shortname, subs->number + 1);
+		scnprintf(subs->name, sizeof(subs->name),
+			  "%s MIDI %d",
+			  oxfw->card->shortname, subs->number + 1);
 	}
 }
 
sound/firewire/tascam/tascam-midi.c +6 -6
index 02eed2dce435..c57fac4f1968 100644
@@ -108,9 +108,9 @@ int snd_tscm_create_midi_devices(struct snd_tscm *tscm)
 		/* TODO: support virtual MIDI ports. */
 		if (subs->number < tscm->spec->midi_capture_ports) {
 			/* Hardware MIDI ports. */
-			snprintf(subs->name, sizeof(subs->name),
-				 "%s MIDI %d",
-				 tscm->card->shortname, subs->number + 1);
+			scnprintf(subs->name, sizeof(subs->name),
+				  "%s MIDI %d",
+				  tscm->card->shortname, subs->number + 1);
 		}
 	}
 
@@ -123,9 +123,9 @@ int snd_tscm_create_midi_devices(struct snd_tscm *tscm)
 	list_for_each_entry(subs, &stream->substreams, list) {
 		if (subs->number < tscm->spec->midi_playback_ports) {
 			/* Hardware MIDI ports only. */
-			snprintf(subs->name, sizeof(subs->name),
-				 "%s MIDI %d",
-				 tscm->card->shortname, subs->number + 1);
+			scnprintf(subs->name, sizeof(subs->name),
+				  "%s MIDI %d",
+				  tscm->card->shortname, subs->number + 1);
 		}
 	}
 
Signature · 1 line
2.35.3
maintainer Reviewed-by Tested-by on v1 11/13
#13 lore raw

Re: [PATCH 11/13] ALSA: firewire: Fix -Wformat-truncation warning for MIDI stream names

Hi,

Quoted 7 lines · Takashi Iwai

The compile warnings at filling MIDI stream name strings are all false-positive; the number of streams can't go so high.

For suppressing the warning, replace snprintf() with scnprintf(). As stated in the above, truncation doesn't matter.

Quoted 10 lines
---
 sound/firewire/bebob/bebob_midi.c         |  6 +++---
 sound/firewire/dice/dice-midi.c           |  4 ++--
 sound/firewire/digi00x/digi00x-midi.c     | 14 +++++++-------
 sound/firewire/fireface/ff-midi.c         |  4 ++--
 sound/firewire/fireworks/fireworks_midi.c |  4 ++--
 sound/firewire/motu/motu-midi.c           |  4 ++--
 sound/firewire/oxfw/oxfw-midi.c           |  6 +++---
 sound/firewire/tascam/tascam-midi.c       | 12 ++++++------
 8 files changed, 27 insertions(+), 27 deletions(-)

Thanks

Takashi Sakamoto

#14 lore raw

PATCH 12/13 ALSA: cmipci: Fix -Wformat-truncation warning

In reply to Takashi Iwai

CMIPCI driver got compile warnings with -Wformat-truncation at a couple of plain sprintf() usages. Use scnprintf() for filling the longname string for avoiding the warnings.

---
 sound/pci/cmipci.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sound/pci/cmipci.c +5 -3
index 1415baac9c36..08e34b184780 100644
@@ -3102,11 +3102,13 @@ static int snd_cmipci_create(struct snd_card *card, struct pci_dev *pci,
 	}
 	sprintf(card->shortname, "C-Media CMI%d", val);
 	if (cm->chip_version < 68)
-		sprintf(modelstr, " (model %d)", cm->chip_version);
+		scnprintf(modelstr, sizeof(modelstr),
+			  " (model %d)", cm->chip_version);
 	else
 		modelstr[0] = '\0';
-	sprintf(card->longname, "%s%s at %#lx, irq %i",
-		card->shortname, modelstr, cm->iobase, cm->irq);
+	scnprintf(card->longname, sizeof(card->longname),
+		  "%s%s at %#lx, irq %i",
+		  card->shortname, modelstr, cm->iobase, cm->irq);
 
 	if (cm->chip_version >= 39) {
 		val = snd_cmipci_read_b(cm, CM_REG_MPU_PCI + 1);
Signature · 1 line
2.35.3
#15 lore raw

PATCH 13/13 ALSA: hda: generic: Check potential mixer name string truncation

In reply to Takashi Iwai

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.

---
 sound/pci/hda/hda_generic.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
sound/pci/hda/hda_generic.c +5 -1
index dbf7aa88e0e3..bf685d01259d 100644
@@ -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;
Signature · 1 line
2.35.3