ALSA: Warn when control names are truncated
PATCH 1/1 ALSA: Warn when control names are truncated
This is likely to confuse user interfaces since the end of the control name is interpreted (eg, "Volume", "Switch").
--- sound/core/control.c | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-)
sound/core/control.c +6 -1
@@ -225,8 +225,13 @@ struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, kctl.id.iface = ncontrol->iface; kctl.id.device = ncontrol->device; kctl.id.subdevice = ncontrol->subdevice; -if (ncontrol->name) +if (ncontrol->name) {strlcpy (kctl.id.name, ncontrol->name,sizeof (kctl.id.name)); +if (strcmp (ncontrol->name, kctl.id.name) !=0 ) +snd_printk (KERN_WARNING +"Control name '%s' truncated to '%s'\n" , + ncontrol->name, kctl.id.name); + } kctl.id.index = ncontrol->index; kctl.count = ncontrol->count ? ncontrol->count :1 ; access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
Signature · 1 line
1.5.6.5
Takashi Iwai
AppliedAt Wed, 29 Oct 2008 14:40:30 +0000,
Quoted 5 lines · Mark Brown
This is likely to confuse user interfaces since the end of the control name is interpreted (eg, "Volume", "Switch").
Signed-off-by Mark Brown <[email protected]>
Thanks, applied now.
Takashi
Quoted 26 lines
--- sound/core/control.c | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-)sound/core/control.c +6
-1@@ -225,8 +225,13 @@ struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, kctl.id.iface = ncontrol->iface; kctl.id.device = ncontrol->device; kctl.id.subdevice = ncontrol->subdevice; -if (ncontrol->name) +if (ncontrol->name) {strlcpy (kctl.id.name, ncontrol->name,sizeof (kctl.id.name)); +if (strcmp (ncontrol->name, kctl.id.name) !=0 ) +snd_printk (KERN_WARNING +"Control name '%s' truncated to '%s'\n" , + ncontrol->name, kctl.id.name); + } kctl.id.index = ncontrol->index; kctl.count = ncontrol->count ? ncontrol->count :1 ; access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :Signature · 1 line
1.5.6.5
Jaroslav Kysela
maintainerQuoted 6 lines · Takashi Iwai
At Wed, 29 Oct 2008 14:40:30 +0000,
Quoted 4 lines · Mark Brown
This is likely to confuse user interfaces since the end of the control name is interpreted (eg, "Volume", "Switch").
Quoted 6 lines
Quoted 1 line
Signed-off-by Mark Brown <[email protected]>Thanks, applied now.
sound/core/control.c struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, · line 230
Quoted 1 line
sound/core/control.c +1
-0+if (strcmp (ncontrol->name, kctl.id.name) !=0 )
Maybe better is to just use end char comparsion to save few CPU ticks:
if (kctl.id.name[sizeof(kctl.id.name)-2] != '\0' &&
ncontrol->name[sizeof(kctl.id.name)-1] != '\0') print_warning_here; Jaroslav
-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, Red Hat, Inc.
At Wed, 29 Oct 2008 15:51:40 +0100 (CET),
Quoted 14 lines · Jaroslav Kysela
Quoted 11 lines · Takashi Iwai
At Wed, 29 Oct 2008 14:40:30 +0000,
Quoted 5 lines · Mark Brown
This is likely to confuse user interfaces since the end of the control name is interpreted (eg, "Volume", "Switch").
Signed-off-by Mark Brown <[email protected]>Thanks, applied now.
Quoted 6 lines
Quoted 1 line
sound/core/control.c struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, · line 230
Quoted 1 line
sound/core/control.c +1
-0+if (strcmp (ncontrol->name, kctl.id.name) !=0 )Maybe better is to just use end char comparsion to save few CPU ticks:
if (kctl.id.name[sizeof(kctl.id.name)-2] != '\0' &&
ncontrol->name[sizeof(kctl.id.name)-1] != '\0')
No, this may cause segfault (and way too hacky to understand what's the purpose of the code...)
Takashi
Jaroslav Kysela
maintainerQuoted 16 lines · Takashi Iwai
At Wed, 29 Oct 2008 15:51:40 +0100 (CET),
Quoted 14 lines · Jaroslav Kysela
Quoted 11 lines · Takashi Iwai
At Wed, 29 Oct 2008 14:40:30 +0000,
Quoted 5 lines · Mark Brown
This is likely to confuse user interfaces since the end of the control name is interpreted (eg, "Volume", "Switch").
Signed-off-by Mark Brown <[email protected]>Thanks, applied now.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
sound/core/control.c struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, · line 230
Quoted 1 line
sound/core/control.c +1
-0+if (strcmp (ncontrol->name, kctl.id.name) !=0 )Maybe better is to just use end char comparsion to save few CPU ticks:
if (kctl.id.name[sizeof(kctl.id.name)-2] != '\0' &&
ncontrol->name[sizeof(kctl.id.name)-1] != '\0')No, this may cause segfault
How? The first check in the fixed size variable initialized with zeros allocated on heap ensures that ncontrol->name string is at least sizeof(kctl.id.name) long. There is no possibility of segfault.
Quoted 1 line
(and way too hacky to understand what's the purpose of the code...)
The printk explains check nicely.
Jaroslav
-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, Red Hat, Inc.
At Wed, 29 Oct 2008 19:31:25 +0100 (CET),
Quoted 23 lines · Jaroslav Kysela
Quoted 20 lines · Takashi Iwai
At Wed, 29 Oct 2008 15:51:40 +0100 (CET),
Quoted 18 lines · Jaroslav Kysela
Quoted 6 lines · Takashi Iwai
At Wed, 29 Oct 2008 14:40:30 +0000,
Quoted 4 lines · Mark Brown
This is likely to confuse user interfaces since the end of the control name is interpreted (eg, "Volume", "Switch").
Quoted 6 lines
Quoted 1 line
Signed-off-by Mark Brown <[email protected]>Thanks, applied now.
sound/core/control.c struct snd_kcontrol *snd_ctl_new1(const struct snd_kcontrol_new *ncontrol, · line 230
Quoted 1 line
sound/core/control.c +1
-0+if (strcmp (ncontrol->name, kctl.id.name) !=0 )Maybe better is to just use end char comparsion to save few CPU ticks:
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
if (kctl.id.name[sizeof(kctl.id.name)-2] != '\0' &&
ncontrol->name[sizeof(kctl.id.name)-1] != '\0')No, this may cause segfault
How? The first check in the fixed size variable initialized with zeros allocated on heap ensures that
ncontrol->namestring is at least sizeof(kctl.id.name) long. There is no possibility of segfault.
Hm, OK, this would work indeed surprisingly.
Quoted 4 lines
Quoted 2 lines
(and way too hacky to understand what's the purpose of the code...)
The printk explains check nicely.
Uh, no. The problem is that you'll likely need to consider what really the code does and even whether it's correct at the first glance. That's definitely bad for readability.
Since the code path is absolutely no fast path and a rarely used path, there is no win at all in practice for such a nano optimization.
Takashi
Quoted 5 lines · Jaroslav Kysela
Maybe better is to just use end char comparsion to save few CPU ticks:
if (kctl.id.name[sizeof(kctl.id.name)-2] != '\0' &&
ncontrol->name[sizeof(kctl.id.name)-1] != '\0') print_warning_here;
It will be but given that this only happens when controls are created I'd be surprised if performance were a practical issue.