]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
ALSA: seq: Use flexible array for device arguments
authorCássio Gabriel <cassiogabrielcontato@gmail.com>
Sun, 31 May 2026 23:41:41 +0000 (20:41 -0300)
committerTakashi Iwai <tiwai@suse.de>
Mon, 1 Jun 2026 09:28:32 +0000 (11:28 +0200)
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>
include/sound/seq_device.h
sound/core/seq_device.c

index a72380c202e984d670f3ab729033ddb689932c5d..3137d4c5f5a850b2541ffa60fa21499155ef85c3 100644 (file)
@@ -22,6 +22,7 @@ struct snd_seq_device {
        void *private_data;     /* private data for the caller */
        void (*private_free)(struct snd_seq_device *device);
        struct device dev;
+       unsigned char args[];   /* driver-specific argument */
 };
 
 #define to_seq_dev(_dev) \
@@ -64,7 +65,7 @@ void snd_seq_device_load_drivers(void);
 int snd_seq_device_new(struct snd_card *card, int device, const char *id,
                       int argsize, struct snd_seq_device **result);
 
-#define SNDRV_SEQ_DEVICE_ARGPTR(dev) (void *)((char *)(dev) + sizeof(struct snd_seq_device))
+#define SNDRV_SEQ_DEVICE_ARGPTR(dev) ((void *)(dev)->args)
 
 int __must_check __snd_seq_driver_register(struct snd_seq_driver *drv,
                                           struct module *mod);
index 1b062d6b17ea8316525c91d13e1e10ad1d009dc3..8be1f3ab5b63c9aa03b284d88389455901a72492 100644 (file)
@@ -234,7 +234,10 @@ int snd_seq_device_new(struct snd_card *card, int device, const char *id,
        if (snd_BUG_ON(!id))
                return -EINVAL;
 
-       dev = kzalloc(sizeof(*dev) + argsize, GFP_KERNEL);
+       if (argsize < 0)
+               return -EINVAL;
+
+       dev = kzalloc_flex(*dev, args, argsize);
        if (!dev)
                return -ENOMEM;