Skip to contentSkip to conversation

ALSA: control: Annotate snd_kcontrol with __counted_by()

PATCH In review Takashi Iwai 2 participants 2 messages Jul 26, 2024 – Aug 1, 2024 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: control: Annotate snd_kcontrol with __counted_by()

struct snd_kcontrol contains a flex array of snd_kcontrol_volatile objects at its end, and the array size is stored in count field. This can be annotated gracefully with __counted_by() for catching possible array overflows.

One additional change is the order of the count field initialization; The assignment of the count field is moved before assignment of vd[] elements for avoiding false-positive warnings from compilers.

---

This one seems out of Gustavo's radar. I have no compiler that can actually check this annotation, so it's a bit untested.

 include/sound/control.h | 2 +-
 sound/core/control.c    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/sound/control.h +1 -1
index c1659036c4a7..13511c50825f 100644
@@ -81,7 +81,7 @@ struct snd_kcontrol {
 	unsigned long private_value;
 	void *private_data;
 	void (*private_free)(struct snd_kcontrol *kcontrol);
-	struct snd_kcontrol_volatile vd[];	/* volatile data */
+	struct snd_kcontrol_volatile vd[] __counted_by(count);	/* volatile data */
 };
 
 #define snd_kcontrol(n) list_entry(n, struct snd_kcontrol, list)
sound/core/control.c +1 -1
index f64a555f404f..c3aad64ffbf5 100644
@@ -237,11 +237,11 @@ static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
 	if (!*kctl)
 		return -ENOMEM;
 
+	(*kctl)->count = count;
 	for (idx = 0; idx < count; idx++) {
 		(*kctl)->vd[idx].access = access;
 		(*kctl)->vd[idx].owner = file;
 	}
-	(*kctl)->count = count;
 
 	return 0;
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Takashi Iwai

struct snd_kcontrol contains a flex array of snd_kcontrol_volatile objects at its end, and the array size is stored in count field. This can be annotated gracefully with __counted_by() for catching possible array overflows.

One additional change is the order of the count field initialization; The assignment of the count field is moved before assignment of vd[] elements for avoiding false-positive warnings from compilers.

Thanks for catching this!

Quoted 1 line

-
Gustavo

Quoted 39 lines

---

This one seems out of Gustavo's radar. I have no compiler that can actually check this annotation, so it's a bit untested.

  include/sound/control.h | 2 +-
  sound/core/control.c    | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
include/sound/control.h +1 -1
index c1659036c4a7..13511c50825f 100644
@@ -81,7 +81,7 @@ struct snd_kcontrol {
  	unsigned long private_value;
  	void *private_data;
  	void (*private_free)(struct snd_kcontrol *kcontrol);
-	struct snd_kcontrol_volatile vd[];	/* volatile data */
+	struct snd_kcontrol_volatile vd[] __counted_by(count);	/* volatile data */
  };
 
  #define snd_kcontrol(n) list_entry(n, struct snd_kcontrol, list)
sound/core/control.c +1 -1
index f64a555f404f..c3aad64ffbf5 100644
@@ -237,11 +237,11 @@ static int snd_ctl_new(struct snd_kcontrol **kctl, unsigned int count,
  	if (!*kctl)
  		return -ENOMEM;
 
+	(*kctl)->count = count;
  	for (idx = 0; idx < count; idx++) {
  		(*kctl)->vd[idx].access = access;
  		(*kctl)->vd[idx].owner = file;
  	}
-	(*kctl)->count = count;
 
  	return 0;
  }