[alsa-devel] [PATCH 0/3] ALSA: mpu401: Adjustments for three function implementations
From: Markus Elfring elfring@users.sourceforge.net Date: Sat, 12 Aug 2017 21:05:21 +0200
A few update suggestions were taken into account from static source code analysis.
Markus Elfring (3): Delete an error message for a failed memory allocation in snd_mpu401_uart_new() Use common error handling code in snd_mpu401_uart_new() Adjust four checks for null pointers
sound/drivers/mpu401/mpu401_uart.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
From: Markus Elfring elfring@users.sourceforge.net Date: Sat, 12 Aug 2017 20:20:11 +0200
Omit an extra message for a memory allocation failure in this function.
This issue was detected by using the Coccinelle software.
Signed-off-by: Markus Elfring elfring@users.sourceforge.net --- sound/drivers/mpu401/mpu401_uart.c | 1 - 1 file changed, 1 deletion(-)
diff --git a/sound/drivers/mpu401/mpu401_uart.c b/sound/drivers/mpu401/mpu401_uart.c index 3a7c317ae012..cc347386fc2b 100644 --- a/sound/drivers/mpu401/mpu401_uart.c +++ b/sound/drivers/mpu401/mpu401_uart.c @@ -545,7 +545,6 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, return err; mpu = kzalloc(sizeof(*mpu), GFP_KERNEL); if (mpu == NULL) { - snd_printk(KERN_ERR "mpu401_uart: cannot allocate\n"); snd_device_free(card, rmidi); return -ENOMEM; }
From: Markus Elfring elfring@users.sourceforge.net Date: Sat, 12 Aug 2017 20:40:17 +0200
Add a jump target so that a bit of exception handling can be better reused at the end of this function.
Signed-off-by: Markus Elfring elfring@users.sourceforge.net --- sound/drivers/mpu401/mpu401_uart.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/sound/drivers/mpu401/mpu401_uart.c b/sound/drivers/mpu401/mpu401_uart.c index cc347386fc2b..e8bdea193eab 100644 --- a/sound/drivers/mpu401/mpu401_uart.c +++ b/sound/drivers/mpu401/mpu401_uart.c @@ -545,8 +545,8 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, return err; mpu = kzalloc(sizeof(*mpu), GFP_KERNEL); if (mpu == NULL) { - snd_device_free(card, rmidi); - return -ENOMEM; + err = -ENOMEM; + goto free_device; } rmidi->private_data = mpu; rmidi->private_free = snd_mpu401_uart_free; @@ -562,8 +562,8 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, snd_printk(KERN_ERR "mpu401_uart: " "unable to grab port 0x%lx size %d\n", port, res_size); - snd_device_free(card, rmidi); - return -EBUSY; + err = -EBUSY; + goto free_device; } } if (info_flags & MPU401_INFO_MMIO) { @@ -583,8 +583,8 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, "MPU401 UART", (void *) mpu)) { snd_printk(KERN_ERR "mpu401_uart: " "unable to grab IRQ %d\n", irq); - snd_device_free(card, rmidi); - return -EBUSY; + err = -EBUSY; + goto free_device; } } if (irq < 0 && !(info_flags & MPU401_INFO_IRQ_HOOK)) @@ -612,6 +612,9 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, if (rrawmidi) *rrawmidi = rmidi; return 0; +free_device: + snd_device_free(card, rmidi); + return err; }
EXPORT_SYMBOL(snd_mpu401_uart_new);
On Sat, 2017-08-12 at 21:12 +0200, SF Markus Elfring wrote:
Add a jump target so that a bit of exception handling can be better reused at the end of this function.
[]
diff --git a/sound/drivers/mpu401/mpu401_uart.c b/sound/drivers/mpu401/mpu401_uart.c
[]
@@ -612,6 +612,9 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, if (rrawmidi) *rrawmidi = rmidi; return 0; +free_device:
- snd_device_free(card, rmidi);
- return err;
}
EXPORT_SYMBOL(snd_mpu401_uart_new);
It may be more common and better to leave a blank line between a single line return and a label.
It separates logical points in the function a bit more.
From: Markus Elfring elfring@users.sourceforge.net Date: Sat, 12 Aug 2017 20:50:16 +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 --- sound/drivers/mpu401/mpu401_uart.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/sound/drivers/mpu401/mpu401_uart.c b/sound/drivers/mpu401/mpu401_uart.c index e8bdea193eab..b997222274bd 100644 --- a/sound/drivers/mpu401/mpu401_uart.c +++ b/sound/drivers/mpu401/mpu401_uart.c @@ -136,7 +136,7 @@ irqreturn_t snd_mpu401_uart_interrupt(int irq, void *dev_id) { struct snd_mpu401 *mpu = dev_id; - if (mpu == NULL) + if (!mpu) return IRQ_NONE; _snd_mpu401_uart_interrupt(mpu); return IRQ_HANDLED; @@ -157,7 +157,7 @@ irqreturn_t snd_mpu401_uart_interrupt_tx(int irq, void *dev_id) { struct snd_mpu401 *mpu = dev_id; - if (mpu == NULL) + if (!mpu) return IRQ_NONE; uart_interrupt_tx(mpu); return IRQ_HANDLED; @@ -544,7 +544,7 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, out_enable, in_enable, &rmidi)) < 0) return err; mpu = kzalloc(sizeof(*mpu), GFP_KERNEL); - if (mpu == NULL) { + if (!mpu) { err = -ENOMEM; goto free_device; } @@ -558,7 +558,7 @@ int snd_mpu401_uart_new(struct snd_card *card, int device, if (! (info_flags & MPU401_INFO_INTEGRATED)) { int res_size = hardware == MPU401_HW_PC98II ? 4 : 2; mpu->res = request_region(port, res_size, "MPU401 UART"); - if (mpu->res == NULL) { + if (!mpu->res) { snd_printk(KERN_ERR "mpu401_uart: " "unable to grab port 0x%lx size %d\n", port, res_size);
On Sat, 12 Aug 2017 21:10:12 +0200, SF Markus Elfring wrote:
From: Markus Elfring elfring@users.sourceforge.net Date: Sat, 12 Aug 2017 21:05:21 +0200
A few update suggestions were taken into account from static source code analysis.
Markus Elfring (3): Delete an error message for a failed memory allocation in snd_mpu401_uart_new() Use common error handling code in snd_mpu401_uart_new() Adjust four checks for null pointers
Applied all three patches, thanks.
Takashi
participants (3)
-
Joe Perches
-
SF Markus Elfring
-
Takashi Iwai