Skip to content

Commit 8a2d0b5

Browse files
CassivsGabriellistiwai
authored andcommitted
ALSA: seq: Use flexible array for device arguments
snd_seq_device_new() allocates struct snd_seq_device together with a caller-specific argument area. SNDRV_SEQ_DEVICE_ARGPTR() reaches that area by adding sizeof(struct snd_seq_device) to the object pointer. Make the trailing storage explicit with a flexible array and allocate it with kzalloc_flex(). This makes the object layout self-describing and avoids open-coded size arithmetic in the allocation and accessor. Reject negative argsize values before calculating the allocation size. Current in-tree callers pass either zero or sizeof() values, but the function takes an int size argument and should not let a negative value flow into unsigned allocation arithmetic. Signed-off-by: Cássio Gabriel <cassiogabrielcontato@gmail.com> Link: https://patch.msgid.link/20260531-alsa-seq-flex-args-v2-1-6e068d4ed9b0@gmail.com Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 635b5c6 commit 8a2d0b5

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

include/sound/seq_device.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct snd_seq_device {
2222
void *private_data; /* private data for the caller */
2323
void (*private_free)(struct snd_seq_device *device);
2424
struct device dev;
25+
unsigned char args[]; /* driver-specific argument */
2526
};
2627

2728
#define to_seq_dev(_dev) \
@@ -64,7 +65,7 @@ void snd_seq_device_load_drivers(void);
6465
int snd_seq_device_new(struct snd_card *card, int device, const char *id,
6566
int argsize, struct snd_seq_device **result);
6667

67-
#define SNDRV_SEQ_DEVICE_ARGPTR(dev) (void *)((char *)(dev) + sizeof(struct snd_seq_device))
68+
#define SNDRV_SEQ_DEVICE_ARGPTR(dev) ((void *)(dev)->args)
6869

6970
int __must_check __snd_seq_driver_register(struct snd_seq_driver *drv,
7071
struct module *mod);

sound/core/seq_device.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ int snd_seq_device_new(struct snd_card *card, int device, const char *id,
234234
if (snd_BUG_ON(!id))
235235
return -EINVAL;
236236

237-
dev = kzalloc(sizeof(*dev) + argsize, GFP_KERNEL);
237+
if (argsize < 0)
238+
return -EINVAL;
239+
240+
dev = kzalloc_flex(*dev, args, argsize);
238241
if (!dev)
239242
return -ENOMEM;
240243

0 commit comments

Comments
 (0)