* [PATCH] ALSA: Warn when control names are truncated
@ 2008-10-29 14:14 Mark Brown
2008-10-29 14:28 ` Takashi Iwai
0 siblings, 1 reply; 10+ messages in thread
From: Mark Brown @ 2008-10-29 14:14 UTC (permalink / raw)
To: Takashi Iwai; +Cc: alsa-devel, 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 <broonie@opensource.wolfsonmicro.com>
---
sound/core/control.c | 6 +++++-
1 files changed, 5 insertions(+), 1 deletions(-)
diff --git a/sound/core/control.c b/sound/core/control.c
index 6d71f9a..1257ca8 100644
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -225,8 +225,12 @@ 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) {
+ if (strlen(ncontrol->name) > sizeof(kctl.id.name))
+ snd_printk(KERN_WARNING "Control name '%s' truncated\n",
+ ncontrol->name);
strlcpy(kctl.id.name, ncontrol->name, sizeof(kctl.id.name));
+ }
kctl.id.index = ncontrol->index;
kctl.count = ncontrol->count ? ncontrol->count : 1;
access = ncontrol->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
--
1.5.6.5
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 14:14 Mark Brown
@ 2008-10-29 14:28 ` Takashi Iwai
2008-10-29 14:33 ` Mark Brown
0 siblings, 1 reply; 10+ messages in thread
From: Takashi Iwai @ 2008-10-29 14:28 UTC (permalink / raw)
To: Mark Brown; +Cc: alsa-devel
At Wed, 29 Oct 2008 14:14:54 +0000,
Mark Brown wrote:
>
> - if (ncontrol->name)
> + if (ncontrol->name) {
> + if (strlen(ncontrol->name) > sizeof(kctl.id.name))
This should be '>=' since strlen() doesn't count the terminator.
thanks,
Takashi
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 14:28 ` Takashi Iwai
@ 2008-10-29 14:33 ` Mark Brown
0 siblings, 0 replies; 10+ messages in thread
From: Mark Brown @ 2008-10-29 14:33 UTC (permalink / raw)
To: Takashi Iwai; +Cc: alsa-devel
On Wed, Oct 29, 2008 at 03:28:50PM +0100, Takashi Iwai wrote:
> Mark Brown wrote:
> > - if (ncontrol->name)
> > + if (ncontrol->name) {
> > + if (strlen(ncontrol->name) > sizeof(kctl.id.name))
> This should be '>=' since strlen() doesn't count the terminator.
Oh, sorry - I'd misread strlcpy() as strncpy() (which would mean that
the termination character wasn't needed).
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH] ALSA: Warn when control names are truncated
@ 2008-10-29 14:40 Mark Brown
2008-10-29 14:46 ` Takashi Iwai
0 siblings, 1 reply; 10+ messages in thread
From: Mark Brown @ 2008-10-29 14:40 UTC (permalink / raw)
To: Takashi Iwai; +Cc: alsa-devel, 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 <broonie@opensource.wolfsonmicro.com>
---
sound/core/control.c | 7 ++++++-
1 files changed, 6 insertions(+), 1 deletions(-)
diff --git a/sound/core/control.c b/sound/core/control.c
index 6d71f9a..b0bf426 100644
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -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 :
--
1.5.6.5
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 14:40 [PATCH] ALSA: Warn when control names are truncated Mark Brown
@ 2008-10-29 14:46 ` Takashi Iwai
2008-10-29 14:51 ` Jaroslav Kysela
0 siblings, 1 reply; 10+ messages in thread
From: Takashi Iwai @ 2008-10-29 14:46 UTC (permalink / raw)
To: Mark Brown; +Cc: alsa-devel
At Wed, 29 Oct 2008 14:40:30 +0000,
Mark Brown wrote:
>
> This is likely to confuse user interfaces since the end of the control
> name is interpreted (eg, "Volume", "Switch").
>
> Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
Thanks, applied now.
Takashi
> ---
> sound/core/control.c | 7 ++++++-
> 1 files changed, 6 insertions(+), 1 deletions(-)
>
> diff --git a/sound/core/control.c b/sound/core/control.c
> index 6d71f9a..b0bf426 100644
> --- a/sound/core/control.c
> +++ b/sound/core/control.c
> @@ -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 :
> --
> 1.5.6.5
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 14:46 ` Takashi Iwai
@ 2008-10-29 14:51 ` Jaroslav Kysela
2008-10-29 14:55 ` Takashi Iwai
2008-10-29 14:55 ` Mark Brown
0 siblings, 2 replies; 10+ messages in thread
From: Jaroslav Kysela @ 2008-10-29 14:51 UTC (permalink / raw)
To: Takashi Iwai; +Cc: alsa-devel, Mark Brown
On Wed, 29 Oct 2008, Takashi Iwai wrote:
> At Wed, 29 Oct 2008 14:40:30 +0000,
> Mark Brown wrote:
> >
> > This is likely to confuse user interfaces since the end of the control
> > name is interpreted (eg, "Volume", "Switch").
> >
> > Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
>
> Thanks, applied now.
>
>
> > + 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 <perex@perex.cz>
Linux Kernel Sound Maintainer
ALSA Project, Red Hat, Inc.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 14:51 ` Jaroslav Kysela
@ 2008-10-29 14:55 ` Takashi Iwai
2008-10-29 18:31 ` Jaroslav Kysela
2008-10-29 14:55 ` Mark Brown
1 sibling, 1 reply; 10+ messages in thread
From: Takashi Iwai @ 2008-10-29 14:55 UTC (permalink / raw)
To: Jaroslav Kysela; +Cc: alsa-devel, Mark Brown
At Wed, 29 Oct 2008 15:51:40 +0100 (CET),
Jaroslav Kysela wrote:
>
> On Wed, 29 Oct 2008, Takashi Iwai wrote:
>
> > At Wed, 29 Oct 2008 14:40:30 +0000,
> > Mark Brown wrote:
> > >
> > > This is likely to confuse user interfaces since the end of the control
> > > name is interpreted (eg, "Volume", "Switch").
> > >
> > > Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
> >
> > Thanks, applied now.
> >
> >
> > > + 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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 14:51 ` Jaroslav Kysela
2008-10-29 14:55 ` Takashi Iwai
@ 2008-10-29 14:55 ` Mark Brown
1 sibling, 0 replies; 10+ messages in thread
From: Mark Brown @ 2008-10-29 14:55 UTC (permalink / raw)
To: Jaroslav Kysela; +Cc: Takashi Iwai, alsa-devel
On Wed, Oct 29, 2008 at 03:51:40PM +0100, Jaroslav Kysela wrote:
> 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.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 14:55 ` Takashi Iwai
@ 2008-10-29 18:31 ` Jaroslav Kysela
2008-10-29 19:00 ` Takashi Iwai
0 siblings, 1 reply; 10+ messages in thread
From: Jaroslav Kysela @ 2008-10-29 18:31 UTC (permalink / raw)
To: Takashi Iwai; +Cc: alsa-devel, Mark Brown
On Wed, 29 Oct 2008, Takashi Iwai wrote:
> At Wed, 29 Oct 2008 15:51:40 +0100 (CET),
> Jaroslav Kysela wrote:
> >
> > On Wed, 29 Oct 2008, Takashi Iwai wrote:
> >
> > > At Wed, 29 Oct 2008 14:40:30 +0000,
> > > Mark Brown wrote:
> > > >
> > > > This is likely to confuse user interfaces since the end of the control
> > > > name is interpreted (eg, "Volume", "Switch").
> > > >
> > > > Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
> > >
> > > Thanks, applied now.
> > >
> > >
> > > > + 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.
> (and way too hacky to understand what's the purpose of the code...)
The printk explains check nicely.
Jaroslav
-----
Jaroslav Kysela <perex@perex.cz>
Linux Kernel Sound Maintainer
ALSA Project, Red Hat, Inc.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] ALSA: Warn when control names are truncated
2008-10-29 18:31 ` Jaroslav Kysela
@ 2008-10-29 19:00 ` Takashi Iwai
0 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2008-10-29 19:00 UTC (permalink / raw)
To: Jaroslav Kysela; +Cc: alsa-devel, Mark Brown
At Wed, 29 Oct 2008 19:31:25 +0100 (CET),
Jaroslav Kysela wrote:
>
> On Wed, 29 Oct 2008, Takashi Iwai wrote:
>
> > At Wed, 29 Oct 2008 15:51:40 +0100 (CET),
> > Jaroslav Kysela wrote:
> > >
> > > On Wed, 29 Oct 2008, Takashi Iwai wrote:
> > >
> > > > At Wed, 29 Oct 2008 14:40:30 +0000,
> > > > Mark Brown wrote:
> > > > >
> > > > > This is likely to confuse user interfaces since the end of the control
> > > > > name is interpreted (eg, "Volume", "Switch").
> > > > >
> > > > > Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
> > > >
> > > > Thanks, applied now.
> > > >
> > > >
> > > > > + 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.
Hm, OK, this would work indeed surprisingly.
>
> > (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
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2008-10-29 19:00 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-10-29 14:40 [PATCH] ALSA: Warn when control names are truncated Mark Brown
2008-10-29 14:46 ` Takashi Iwai
2008-10-29 14:51 ` Jaroslav Kysela
2008-10-29 14:55 ` Takashi Iwai
2008-10-29 18:31 ` Jaroslav Kysela
2008-10-29 19:00 ` Takashi Iwai
2008-10-29 14:55 ` Mark Brown
-- strict thread matches above, loose matches on Subject: below --
2008-10-29 14:14 Mark Brown
2008-10-29 14:28 ` Takashi Iwai
2008-10-29 14:33 ` Mark Brown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox