diff mbox

ALSA: core: Use common error handling code in two functions

Message ID b8c9e41b-a47e-1d99-3d1a-5d39772e4b54@users.sourceforge.net (mailing list archive)
State New, archived
Headers show

Commit Message

SF Markus Elfring Aug. 22, 2017, 7:12 p.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Tue, 22 Aug 2017 21:01:01 +0200

Add jump targets so that a bit of exception handling can be better reused
at the end of these functions.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 sound/core/pcm.c   | 30 ++++++++++++++++++------------
 sound/core/timer.c | 10 ++++++----
 2 files changed, 24 insertions(+), 16 deletions(-)

Comments

Takashi Iwai Aug. 23, 2017, 5:16 a.m. UTC | #1
On Tue, 22 Aug 2017 21:12:02 +0200,
SF Markus Elfring wrote:
> 
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 22 Aug 2017 21:01:01 +0200
> 
> Add jump targets so that a bit of exception handling can be better reused
> at the end of these functions.
> 
> This issue was detected by using the Coccinelle software.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>

Please don't mix the changes for both PCM and timer.


Takashi


> ---
>  sound/core/pcm.c   | 30 ++++++++++++++++++------------
>  sound/core/timer.c | 10 ++++++----
>  2 files changed, 24 insertions(+), 16 deletions(-)
> 
> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
> index 89c7485519cb..048df9658f50 100644
> --- a/sound/core/pcm.c
> +++ b/sound/core/pcm.c
> @@ -783,21 +783,27 @@ static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
>  	INIT_LIST_HEAD(&pcm->list);
>  	if (id)
>  		strlcpy(pcm->id, id, sizeof(pcm->id));
> -	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
> -		snd_pcm_free(pcm);
> -		return err;
> -	}
> -	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
> -		snd_pcm_free(pcm);
> -		return err;
> -	}
> -	if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
> -		snd_pcm_free(pcm);
> -		return err;
> -	}
> +
> +	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
> +				 playback_count);
> +	if (err < 0)
> +		goto free_pcm;
> +
> +	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count);
> +	if (err < 0)
> +		goto free_pcm;
> +
> +	err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops);
> +	if (err < 0)
> +		goto free_pcm;
> +
>  	if (rpcm)
>  		*rpcm = pcm;
>  	return 0;
> +
> +free_pcm:
> +	snd_pcm_free(pcm);
> +	return err;
>  }
>  
>  /**
> diff --git a/sound/core/timer.c b/sound/core/timer.c
> index a9b9a277e00c..6d73a63f6e2b 100644
> --- a/sound/core/timer.c
> +++ b/sound/core/timer.c
> @@ -2096,8 +2096,7 @@ static int __init alsa_timer_init(void)
>  	err = snd_timer_register_system();
>  	if (err < 0) {
>  		pr_err("ALSA: unable to register system timer (%i)\n", err);
> -		put_device(&timer_dev);
> -		return err;
> +		goto put_timer;
>  	}
>  
>  	err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
> @@ -2105,12 +2104,15 @@ static int __init alsa_timer_init(void)
>  	if (err < 0) {
>  		pr_err("ALSA: unable to register timer device (%i)\n", err);
>  		snd_timer_free_all();
> -		put_device(&timer_dev);
> -		return err;
> +		goto put_timer;
>  	}
>  
>  	snd_timer_proc_init();
>  	return 0;
> +
> +put_timer:
> +	put_device(&timer_dev);
> +	return err;
>  }
>  
>  static void __exit alsa_timer_exit(void)
> -- 
> 2.14.0
> 
>
SF Markus Elfring Aug. 23, 2017, 8:21 a.m. UTC | #2
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Wed, 23 Aug 2017 10:05:43 +0200

Some update suggestions were taken into account
from static source code analysis.

Markus Elfring (6):
  pcm: Use common error handling code in _snd_pcm_new()
  pcm: Adjust nine function calls together with a variable assignment
  pcm: Adjust 11 checks for null pointers
  timer: Use common error handling code in alsa_timer_init()
  timer: Adjust a condition check in snd_timer_resolution()
  timer: Adjust 13 checks for null pointers

 sound/core/pcm.c   | 90 +++++++++++++++++++++++++++++++-----------------------
 sound/core/timer.c | 39 ++++++++++++-----------
 2 files changed, 73 insertions(+), 56 deletions(-)
Takashi Iwai Aug. 23, 2017, 8:35 a.m. UTC | #3
On Wed, 23 Aug 2017 10:25:49 +0200,
SF Markus Elfring wrote:
> 
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Wed, 23 Aug 2017 09:28:00 +0200
> MIME-Version: 1.0
> Content-Type: text/plain; charset=UTF-8
> Content-Transfer-Encoding: 8bit
> 
> The script “checkpatch.pl” pointed information out like the following.
> 
> Comparison to NULL could be written …
> 
> Thus fix the affected source code places.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>

Converting only these alone doesn't give any big merit.  Another patch
in the series fixed the style if ((err = xxx)), so it's fine, though.

checkpatch is no bible, after all.


thanks,

Takashi
diff mbox

Patch

diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index 89c7485519cb..048df9658f50 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -783,21 +783,27 @@  static int _snd_pcm_new(struct snd_card *card, const char *id, int device,
 	INIT_LIST_HEAD(&pcm->list);
 	if (id)
 		strlcpy(pcm->id, id, sizeof(pcm->id));
-	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, playback_count)) < 0) {
-		snd_pcm_free(pcm);
-		return err;
-	}
-	if ((err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count)) < 0) {
-		snd_pcm_free(pcm);
-		return err;
-	}
-	if ((err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops)) < 0) {
-		snd_pcm_free(pcm);
-		return err;
-	}
+
+	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK,
+				 playback_count);
+	if (err < 0)
+		goto free_pcm;
+
+	err = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, capture_count);
+	if (err < 0)
+		goto free_pcm;
+
+	err = snd_device_new(card, SNDRV_DEV_PCM, pcm, &ops);
+	if (err < 0)
+		goto free_pcm;
+
 	if (rpcm)
 		*rpcm = pcm;
 	return 0;
+
+free_pcm:
+	snd_pcm_free(pcm);
+	return err;
 }
 
 /**
diff --git a/sound/core/timer.c b/sound/core/timer.c
index a9b9a277e00c..6d73a63f6e2b 100644
--- a/sound/core/timer.c
+++ b/sound/core/timer.c
@@ -2096,8 +2096,7 @@  static int __init alsa_timer_init(void)
 	err = snd_timer_register_system();
 	if (err < 0) {
 		pr_err("ALSA: unable to register system timer (%i)\n", err);
-		put_device(&timer_dev);
-		return err;
+		goto put_timer;
 	}
 
 	err = snd_register_device(SNDRV_DEVICE_TYPE_TIMER, NULL, 0,
@@ -2105,12 +2104,15 @@  static int __init alsa_timer_init(void)
 	if (err < 0) {
 		pr_err("ALSA: unable to register timer device (%i)\n", err);
 		snd_timer_free_all();
-		put_device(&timer_dev);
-		return err;
+		goto put_timer;
 	}
 
 	snd_timer_proc_init();
 	return 0;
+
+put_timer:
+	put_device(&timer_dev);
+	return err;
 }
 
 static void __exit alsa_timer_exit(void)