]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ALSA: pcm: Relax __free() variable declarations
authorTakashi Iwai <tiwai@suse.de>
Tue, 16 Dec 2025 14:06:25 +0000 (15:06 +0100)
committerTakashi Iwai <tiwai@suse.de>
Wed, 17 Dec 2025 09:08:29 +0000 (10:08 +0100)
We used to have a variable declaration with __free() initialized with
NULL.  This was to keep the old coding style rule, but recently it's
relaxed and rather recommends to follow the new rule to declare in
place of use for __free() -- which avoids potential deadlocks or UAFs
with nested cleanups.

Although the current code has no bug, per se, let's follow the new
standard and move the declaration to the place of assignment (or
directly assign the allocated result) instead of NULL initializations.

Fixes: ae9213984864 ("ALSA: pcm: Use automatic cleanup of kfree()")
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20251216140634.171890-4-tiwai@suse.de
sound/core/pcm.c
sound/core/pcm_compat.c
sound/core/pcm_native.c

index 283aac441fa0a7b1ddfdfc66ab66a2a48329f0f9..0b512085eb63f28aeabbe6d2f9b07fe08ef4a734 100644 (file)
@@ -328,13 +328,13 @@ static const char *snd_pcm_oss_format_name(int format)
 static void snd_pcm_proc_info_read(struct snd_pcm_substream *substream,
                                   struct snd_info_buffer *buffer)
 {
-       struct snd_pcm_info *info __free(kfree) = NULL;
        int err;
 
        if (! substream)
                return;
 
-       info = kmalloc(sizeof(*info), GFP_KERNEL);
+       struct snd_pcm_info *info __free(kfree) =
+               kmalloc(sizeof(*info), GFP_KERNEL);
        if (!info)
                return;
 
index 54eb9bd8eb218862d7a0a4769785c9ea07697163..e86f68f1f23c1e2fd79900ca40ac700c19175bec 100644 (file)
@@ -235,7 +235,6 @@ static int snd_pcm_ioctl_hw_params_compat(struct snd_pcm_substream *substream,
                                          int refine, 
                                          struct snd_pcm_hw_params32 __user *data32)
 {
-       struct snd_pcm_hw_params *data __free(kfree) = NULL;
        struct snd_pcm_runtime *runtime;
        int err;
 
@@ -243,7 +242,8 @@ static int snd_pcm_ioctl_hw_params_compat(struct snd_pcm_substream *substream,
        if (!runtime)
                return -ENOTTY;
 
-       data = kmalloc(sizeof(*data), GFP_KERNEL);
+       struct snd_pcm_hw_params *data __free(kfree) =
+               kmalloc(sizeof(*data), GFP_KERNEL);
        if (!data)
                return -ENOMEM;
 
@@ -332,7 +332,6 @@ static int snd_pcm_ioctl_xfern_compat(struct snd_pcm_substream *substream,
        compat_caddr_t buf;
        compat_caddr_t __user *bufptr;
        u32 frames;
-       void __user **bufs __free(kfree) = NULL;
        int err, ch, i;
 
        if (! substream->runtime)
@@ -349,7 +348,9 @@ static int snd_pcm_ioctl_xfern_compat(struct snd_pcm_substream *substream,
            get_user(frames, &data32->frames))
                return -EFAULT;
        bufptr = compat_ptr(buf);
-       bufs = kmalloc_array(ch, sizeof(void __user *), GFP_KERNEL);
+
+       void __user **bufs __free(kfree) =
+               kmalloc_array(ch, sizeof(void __user *), GFP_KERNEL);
        if (bufs == NULL)
                return -ENOMEM;
        for (i = 0; i < ch; i++) {
index 68bee40c9adafd9bbb079c058ecf80d14f8885c7..4352c0a40f8d098d05d64c46eb5698463ea5448d 100644 (file)
@@ -242,10 +242,10 @@ int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
 int snd_pcm_info_user(struct snd_pcm_substream *substream,
                      struct snd_pcm_info __user * _info)
 {
-       struct snd_pcm_info *info __free(kfree) = NULL;
        int err;
+       struct snd_pcm_info *info __free(kfree) =
+               kmalloc(sizeof(*info), GFP_KERNEL);
 
-       info = kmalloc(sizeof(*info), GFP_KERNEL);
        if (! info)
                return -ENOMEM;
        err = snd_pcm_info(substream, info);
@@ -364,7 +364,6 @@ static int constrain_params_by_rules(struct snd_pcm_substream *substream,
        struct snd_pcm_hw_constraints *constrs =
                                        &substream->runtime->hw_constraints;
        unsigned int k;
-       unsigned int *rstamps __free(kfree) = NULL;
        unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
        unsigned int stamp;
        struct snd_pcm_hw_rule *r;
@@ -380,7 +379,8 @@ static int constrain_params_by_rules(struct snd_pcm_substream *substream,
         * Each member of 'rstamps' array represents the sequence number of
         * recent application of corresponding rule.
         */
-       rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
+       unsigned int *rstamps __free(kfree) =
+               kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
        if (!rstamps)
                return -ENOMEM;
 
@@ -583,10 +583,10 @@ EXPORT_SYMBOL(snd_pcm_hw_refine);
 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
                                  struct snd_pcm_hw_params __user * _params)
 {
-       struct snd_pcm_hw_params *params __free(kfree) = NULL;
        int err;
+       struct snd_pcm_hw_params *params __free(kfree) =
+               memdup_user(_params, sizeof(*params));
 
-       params = memdup_user(_params, sizeof(*params));
        if (IS_ERR(params))
                return PTR_ERR(params);
 
@@ -884,10 +884,10 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
                                  struct snd_pcm_hw_params __user * _params)
 {
-       struct snd_pcm_hw_params *params __free(kfree) = NULL;
        int err;
+       struct snd_pcm_hw_params *params __free(kfree) =
+               memdup_user(_params, sizeof(*params));
 
-       params = memdup_user(_params, sizeof(*params));
        if (IS_ERR(params))
                return PTR_ERR(params);
 
@@ -2262,7 +2262,6 @@ static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
 {
        struct snd_pcm_file *pcm_file;
        struct snd_pcm_substream *substream1;
-       struct snd_pcm_group *group __free(kfree) = NULL;
        struct snd_pcm_group *target_group;
        bool nonatomic = substream->pcm->nonatomic;
        CLASS(fd, f)(fd);
@@ -2278,7 +2277,8 @@ static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
        if (substream == substream1)
                return -EINVAL;
 
-       group = kzalloc(sizeof(*group), GFP_KERNEL);
+       struct snd_pcm_group *group __free(kfree) =
+               kzalloc(sizeof(*group), GFP_KERNEL);
        if (!group)
                return -ENOMEM;
        snd_pcm_group_init(group);
@@ -3286,7 +3286,6 @@ static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
 {
        struct snd_xfern xfern;
        struct snd_pcm_runtime *runtime = substream->runtime;
-       void *bufs __free(kfree) = NULL;
        snd_pcm_sframes_t result;
 
        if (runtime->state == SNDRV_PCM_STATE_OPEN)
@@ -3298,7 +3297,8 @@ static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
        if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
                return -EFAULT;
 
-       bufs = memdup_array_user(xfern.bufs, runtime->channels, sizeof(void *));
+       void *bufs __free(kfree) =
+               memdup_array_user(xfern.bufs, runtime->channels, sizeof(void *));
        if (IS_ERR(bufs))
                return PTR_ERR(bufs);
        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
@@ -3572,7 +3572,6 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
        struct snd_pcm_runtime *runtime;
        snd_pcm_sframes_t result;
        unsigned long i;
-       void __user **bufs __free(kfree) = NULL;
        snd_pcm_uframes_t frames;
        const struct iovec *iov = iter_iov(to);
 
@@ -3591,7 +3590,9 @@ static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
        if (!frame_aligned(runtime, iov->iov_len))
                return -EINVAL;
        frames = bytes_to_samples(runtime, iov->iov_len);
-       bufs = kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
+
+       void __user **bufs __free(kfree) =
+               kmalloc_array(to->nr_segs, sizeof(void *), GFP_KERNEL);
        if (bufs == NULL)
                return -ENOMEM;
        for (i = 0; i < to->nr_segs; ++i) {
@@ -3611,7 +3612,6 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
        struct snd_pcm_runtime *runtime;
        snd_pcm_sframes_t result;
        unsigned long i;
-       void __user **bufs __free(kfree) = NULL;
        snd_pcm_uframes_t frames;
        const struct iovec *iov = iter_iov(from);
 
@@ -3629,7 +3629,9 @@ static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
            !frame_aligned(runtime, iov->iov_len))
                return -EINVAL;
        frames = bytes_to_samples(runtime, iov->iov_len);
-       bufs = kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
+
+       void __user **bufs __free(kfree) =
+               kmalloc_array(from->nr_segs, sizeof(void *), GFP_KERNEL);
        if (bufs == NULL)
                return -ENOMEM;
        for (i = 0; i < from->nr_segs; ++i) {
@@ -4101,15 +4103,15 @@ static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *opara
 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
                                      struct snd_pcm_hw_params_old __user * _oparams)
 {
-       struct snd_pcm_hw_params *params __free(kfree) = NULL;
-       struct snd_pcm_hw_params_old *oparams __free(kfree) = NULL;
        int err;
 
-       params = kmalloc(sizeof(*params), GFP_KERNEL);
+       struct snd_pcm_hw_params *params __free(kfree) =
+               kmalloc(sizeof(*params), GFP_KERNEL);
        if (!params)
                return -ENOMEM;
 
-       oparams = memdup_user(_oparams, sizeof(*oparams));
+       struct snd_pcm_hw_params_old *oparams __free(kfree) =
+               memdup_user(_oparams, sizeof(*oparams));
        if (IS_ERR(oparams))
                return PTR_ERR(oparams);
        snd_pcm_hw_convert_from_old_params(params, oparams);
@@ -4130,15 +4132,15 @@ static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
                                      struct snd_pcm_hw_params_old __user * _oparams)
 {
-       struct snd_pcm_hw_params *params __free(kfree) = NULL;
-       struct snd_pcm_hw_params_old *oparams __free(kfree) = NULL;
        int err;
 
-       params = kmalloc(sizeof(*params), GFP_KERNEL);
+       struct snd_pcm_hw_params *params __free(kfree) =
+               kmalloc(sizeof(*params), GFP_KERNEL);
        if (!params)
                return -ENOMEM;
 
-       oparams = memdup_user(_oparams, sizeof(*oparams));
+       struct snd_pcm_hw_params_old *oparams __free(kfree) =
+               memdup_user(_oparams, sizeof(*oparams));
        if (IS_ERR(oparams))
                return PTR_ERR(oparams);