Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ALSA: compress_offload: 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-4-tiwai@suse.de

+13 -23
+13 -23
sound/core/compress_offload.c
··· 465 465 snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg) 466 466 { 467 467 int retval; 468 - struct snd_compr_codec_caps *caps; 468 + struct snd_compr_codec_caps *caps __free(kfree) = NULL; 469 469 470 470 if (!stream->ops->get_codec_caps) 471 471 return -ENXIO; ··· 476 476 477 477 retval = stream->ops->get_codec_caps(stream, caps); 478 478 if (retval) 479 - goto out; 479 + return retval; 480 480 if (copy_to_user((void __user *)arg, caps, sizeof(*caps))) 481 - retval = -EFAULT; 482 - 483 - out: 484 - kfree(caps); 481 + return -EFAULT; 485 482 return retval; 486 483 } 487 484 #endif /* !COMPR_CODEC_CAPS_OVERFLOW */ ··· 583 586 static int 584 587 snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg) 585 588 { 586 - struct snd_compr_params *params; 589 + struct snd_compr_params *params __free(kfree) = NULL; 587 590 int retval; 588 591 589 592 if (stream->runtime->state == SNDRV_PCM_STATE_OPEN || stream->next_track) { ··· 593 596 */ 594 597 params = memdup_user((void __user *)arg, sizeof(*params)); 595 598 if (IS_ERR(params)) 596 - return PTR_ERR(params); 599 + return PTR_ERR(no_free_ptr(params)); 597 600 598 601 retval = snd_compress_check_input(params); 599 602 if (retval) 600 - goto out; 603 + return retval; 601 604 602 605 retval = snd_compr_allocate_buffer(stream, params); 603 - if (retval) { 604 - retval = -ENOMEM; 605 - goto out; 606 - } 606 + if (retval) 607 + return -ENOMEM; 607 608 608 609 retval = stream->ops->set_params(stream, params); 609 610 if (retval) 610 - goto out; 611 + return retval; 611 612 612 613 if (stream->next_track) 613 - goto out; 614 + return retval; 614 615 615 616 stream->metadata_set = false; 616 617 stream->next_track = false; ··· 617 622 } else { 618 623 return -EPERM; 619 624 } 620 - out: 621 - kfree(params); 622 625 return retval; 623 626 } 624 627 625 628 static int 626 629 snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg) 627 630 { 628 - struct snd_codec *params; 631 + struct snd_codec *params __free(kfree) = NULL; 629 632 int retval; 630 633 631 634 if (!stream->ops->get_params) ··· 634 641 return -ENOMEM; 635 642 retval = stream->ops->get_params(stream, params); 636 643 if (retval) 637 - goto out; 644 + return retval; 638 645 if (copy_to_user((char __user *)arg, params, sizeof(*params))) 639 - retval = -EFAULT; 640 - 641 - out: 642 - kfree(params); 646 + return -EFAULT; 643 647 return retval; 644 648 } 645 649