Skip to content

Commit 52d256c

Browse files
maciejsszmigierotiwai
authored andcommitted
ALSA: ac97: Use snd_ctl_rename() to rename a control
With the recent addition of hashed controls lookup it's not enough to just update the control name field, the hash entries for the modified control have to be updated too. snd_ctl_rename() takes care of that, so use it instead of directly modifying the control name. While we are at it, check also that the new control name doesn't accidentally overwrite the available buffer space. Fixes: c27e1ef ("ALSA: control: Use xarray for faster lookups") Cc: [email protected] Signed-off-by: Maciej S. Szmigiero <[email protected]> Link: https://lore.kernel.org/r/adb68bfa0885ba4a2583794b828f8e20d23f67c7.1666296963.git.maciej.szmigiero@oracle.com Signed-off-by: Takashi Iwai <[email protected]>
1 parent 957ccc4 commit 52d256c

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

sound/pci/ac97/ac97_codec.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,11 +2656,18 @@ EXPORT_SYMBOL(snd_ac97_resume);
26562656
*/
26572657
static void set_ctl_name(char *dst, const char *src, const char *suffix)
26582658
{
2659-
if (suffix)
2660-
sprintf(dst, "%s %s", src, suffix);
2661-
else
2662-
strcpy(dst, src);
2663-
}
2659+
const size_t msize = SNDRV_CTL_ELEM_ID_NAME_MAXLEN;
2660+
2661+
if (suffix) {
2662+
if (snprintf(dst, msize, "%s %s", src, suffix) >= msize)
2663+
pr_warn("ALSA: AC97 control name '%s %s' truncated to '%s'\n",
2664+
src, suffix, dst);
2665+
} else {
2666+
if (strscpy(dst, src, msize) < 0)
2667+
pr_warn("ALSA: AC97 control name '%s' truncated to '%s'\n",
2668+
src, dst);
2669+
}
2670+
}
26642671

26652672
/* remove the control with the given name and optional suffix */
26662673
static int snd_ac97_remove_ctl(struct snd_ac97 *ac97, const char *name,
@@ -2687,8 +2694,11 @@ static int snd_ac97_rename_ctl(struct snd_ac97 *ac97, const char *src,
26872694
const char *dst, const char *suffix)
26882695
{
26892696
struct snd_kcontrol *kctl = ctl_find(ac97, src, suffix);
2697+
char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2698+
26902699
if (kctl) {
2691-
set_ctl_name(kctl->id.name, dst, suffix);
2700+
set_ctl_name(name, dst, suffix);
2701+
snd_ctl_rename(ac97->bus->card, kctl, name);
26922702
return 0;
26932703
}
26942704
return -ENOENT;
@@ -2707,11 +2717,17 @@ static int snd_ac97_swap_ctl(struct snd_ac97 *ac97, const char *s1,
27072717
const char *s2, const char *suffix)
27082718
{
27092719
struct snd_kcontrol *kctl1, *kctl2;
2720+
char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2721+
27102722
kctl1 = ctl_find(ac97, s1, suffix);
27112723
kctl2 = ctl_find(ac97, s2, suffix);
27122724
if (kctl1 && kctl2) {
2713-
set_ctl_name(kctl1->id.name, s2, suffix);
2714-
set_ctl_name(kctl2->id.name, s1, suffix);
2725+
set_ctl_name(name, s2, suffix);
2726+
snd_ctl_rename(ac97->bus->card, kctl1, name);
2727+
2728+
set_ctl_name(name, s1, suffix);
2729+
snd_ctl_rename(ac97->bus->card, kctl2, name);
2730+
27152731
return 0;
27162732
}
27172733
return -ENOENT;

0 commit comments

Comments
 (0)