ALSA: compress_offload: Use automatic cleanup of kfree()
authorTakashi Iwai <tiwai@suse.de>
Thu, 22 Feb 2024 11:15:03 +0000 (12:15 +0100)
committerTakashi Iwai <tiwai@suse.de>
Fri, 23 Feb 2024 09:57:30 +0000 (10:57 +0100)
There are common patterns where a temporary buffer is allocated and
freed at the exit, and those can be simplified with the recent cleanup
mechanism via __free(kfree).

A caveat is that some allocations are memdup_user() and they return an
error pointer instead of NULL.  Those need special cares and the value
has to be cleared with no_free_ptr() at the allocation error path.

Other than that, the conversions are straightforward.

No functional changes, only code refactoring.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20240222111509.28390-4-tiwai@suse.de
sound/core/compress_offload.c

index 619371a..5d926c5 100644 (file)
@@ -465,7 +465,7 @@ static int
 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
 {
        int retval;
-       struct snd_compr_codec_caps *caps;
+       struct snd_compr_codec_caps *caps __free(kfree) = NULL;
 
        if (!stream->ops->get_codec_caps)
                return -ENXIO;
@@ -476,12 +476,9 @@ snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
 
        retval = stream->ops->get_codec_caps(stream, caps);
        if (retval)
-               goto out;
+               return retval;
        if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
-               retval = -EFAULT;
-
-out:
-       kfree(caps);
+               return -EFAULT;
        return retval;
 }
 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
@@ -586,7 +583,7 @@ static int snd_compress_check_input(struct snd_compr_params *params)
 static int
 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
 {
-       struct snd_compr_params *params;
+       struct snd_compr_params *params __free(kfree) = NULL;
        int retval;
 
        if (stream->runtime->state == SNDRV_PCM_STATE_OPEN || stream->next_track) {
@@ -596,24 +593,22 @@ snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
                 */
                params = memdup_user((void __user *)arg, sizeof(*params));
                if (IS_ERR(params))
-                       return PTR_ERR(params);
+                       return PTR_ERR(no_free_ptr(params));
 
                retval = snd_compress_check_input(params);
                if (retval)
-                       goto out;
+                       return retval;
 
                retval = snd_compr_allocate_buffer(stream, params);
-               if (retval) {
-                       retval = -ENOMEM;
-                       goto out;
-               }
+               if (retval)
+                       return -ENOMEM;
 
                retval = stream->ops->set_params(stream, params);
                if (retval)
-                       goto out;
+                       return retval;
 
                if (stream->next_track)
-                       goto out;
+                       return retval;
 
                stream->metadata_set = false;
                stream->next_track = false;
@@ -622,15 +617,13 @@ snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
        } else {
                return -EPERM;
        }
-out:
-       kfree(params);
        return retval;
 }
 
 static int
 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
 {
-       struct snd_codec *params;
+       struct snd_codec *params __free(kfree) = NULL;
        int retval;
 
        if (!stream->ops->get_params)
@@ -641,12 +634,9 @@ snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
                return -ENOMEM;
        retval = stream->ops->get_params(stream, params);
        if (retval)
-               goto out;
+               return retval;
        if (copy_to_user((char __user *)arg, params, sizeof(*params)))
-               retval = -EFAULT;
-
-out:
-       kfree(params);
+               return -EFAULT;
        return retval;
 }