diff options
author | Takashi Iwai <tiwai@suse.de> | 2024-02-22 14:15:02 +0300 |
---|---|---|
committer | Takashi Iwai <tiwai@suse.de> | 2024-02-23 12:57:30 +0300 |
commit | 1052d988226948493eb9730b3424308972eca5f4 (patch) | |
tree | a1150bb31123afe45b33cdc093a826a63157abae /sound/core/control.c | |
parent | ae921398486419c6284d70bd8332eb7edbdb4f70 (diff) | |
download | linux-1052d988226948493eb9730b3424308972eca5f4.tar.xz |
ALSA: control: Use automatic cleanup of kfree()
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-3-tiwai@suse.de
Diffstat (limited to 'sound/core/control.c')
-rw-r--r-- | sound/core/control.c | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/sound/core/control.c b/sound/core/control.c index 59c8658966d4..c8cd70aed6af 100644 --- a/sound/core/control.c +++ b/sound/core/control.c @@ -932,7 +932,7 @@ EXPORT_SYMBOL(snd_ctl_find_id); static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl, unsigned int cmd, void __user *arg) { - struct snd_ctl_card_info *info; + struct snd_ctl_card_info *info __free(kfree) = NULL; info = kzalloc(sizeof(*info), GFP_KERNEL); if (! info) @@ -946,11 +946,8 @@ static int snd_ctl_card_info(struct snd_card *card, struct snd_ctl_file * ctl, strscpy(info->mixername, card->mixername, sizeof(info->mixername)); strscpy(info->components, card->components, sizeof(info->components)); up_read(&snd_ioctl_rwsem); - if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) { - kfree(info); + if (copy_to_user(arg, info, sizeof(struct snd_ctl_card_info))) return -EFAULT; - } - kfree(info); return 0; } @@ -1339,12 +1336,10 @@ static int snd_ctl_elem_read_user(struct snd_card *card, result = snd_ctl_elem_read(card, control); if (result < 0) - goto error; + return result; if (copy_to_user(_control, control, sizeof(*control))) - result = -EFAULT; - error: - kfree(control); + return -EFAULT; return result; } @@ -1406,23 +1401,21 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file, static int snd_ctl_elem_write_user(struct snd_ctl_file *file, struct snd_ctl_elem_value __user *_control) { - struct snd_ctl_elem_value *control; + struct snd_ctl_elem_value *control __free(kfree) = NULL; struct snd_card *card; int result; control = memdup_user(_control, sizeof(*control)); if (IS_ERR(control)) - return PTR_ERR(control); + return PTR_ERR(no_free_ptr(control)); card = file->card; result = snd_ctl_elem_write(card, file, control); if (result < 0) - goto error; + return result; if (copy_to_user(_control, control, sizeof(*control))) - result = -EFAULT; - error: - kfree(control); + return -EFAULT; return result; } |