[alsa-devel] [PATCH 0/4] ALSA: emux: Adjustments for four function implementations

From: Markus Elfring elfring@users.sourceforge.net Date: Wed, 9 Aug 2017 09:50:05 +0200
A few update suggestions were taken into account from static source code analysis.
Markus Elfring (4): Adjust one function call together with a variable assignment Improve a size determination in two functions Adjust four checks for null pointers Delete two error messages for a failed memory allocation in snd_emux_create_port()
sound/synth/emux/emux_seq.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)

From: Markus Elfring elfring@users.sourceforge.net Date: Wed, 9 Aug 2017 08:40:14 +0200
The script "checkpatch.pl" pointed information out like the following.
ERROR: do not use assignment in if condition
Thus fix the affected source code place.
Signed-off-by: Markus Elfring elfring@users.sourceforge.net --- sound/synth/emux/emux_seq.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/sound/synth/emux/emux_seq.c b/sound/synth/emux/emux_seq.c index 55579f6b8cb2..53820f75ba3e 100644 --- a/sound/synth/emux/emux_seq.c +++ b/sound/synth/emux/emux_seq.c @@ -144,7 +144,8 @@ snd_emux_create_port(struct snd_emux *emu, char *name, int i, type, cap;
/* Allocate structures for this channel */ - if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) { + p = kzalloc(sizeof(*p), GFP_KERNEL); + if (!p) { snd_printk(KERN_ERR "no memory\n"); return NULL; }

From: Markus Elfring elfring@users.sourceforge.net Date: Wed, 9 Aug 2017 09:11:26 +0200
Replace the specification of data types by pointer dereferences as the parameter for the operator "sizeof" to make the corresponding size determination a bit safer according to the Linux coding style convention.
Signed-off-by: Markus Elfring elfring@users.sourceforge.net --- sound/synth/emux/emux_seq.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/sound/synth/emux/emux_seq.c b/sound/synth/emux/emux_seq.c index 53820f75ba3e..12acf18c5be4 100644 --- a/sound/synth/emux/emux_seq.c +++ b/sound/synth/emux/emux_seq.c @@ -149,7 +149,8 @@ snd_emux_create_port(struct snd_emux *emu, char *name, snd_printk(KERN_ERR "no memory\n"); return NULL; } - p->chset.channels = kcalloc(max_channels, sizeof(struct snd_midi_channel), GFP_KERNEL); + p->chset.channels = kcalloc(max_channels, sizeof(*p->chset.channels), + GFP_KERNEL); if (p->chset.channels == NULL) { snd_printk(KERN_ERR "no memory\n"); kfree(p); @@ -371,7 +372,7 @@ int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card) if (emu->midi_ports <= 0) return 0;
- emu->vmidi = kcalloc(emu->midi_ports, sizeof(struct snd_rawmidi *), GFP_KERNEL); + emu->vmidi = kcalloc(emu->midi_ports, sizeof(*emu->vmidi), GFP_KERNEL); if (emu->vmidi == NULL) return -ENOMEM;

From: Markus Elfring elfring@users.sourceforge.net Date: Wed, 9 Aug 2017 09:22:42 +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 affected source code places.
Signed-off-by: Markus Elfring elfring@users.sourceforge.net --- sound/synth/emux/emux_seq.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/sound/synth/emux/emux_seq.c b/sound/synth/emux/emux_seq.c index 12acf18c5be4..fd6cbd439511 100644 --- a/sound/synth/emux/emux_seq.c +++ b/sound/synth/emux/emux_seq.c @@ -99,7 +99,7 @@ snd_emux_init_seq(struct snd_emux *emu, struct snd_card *card, int index) sprintf(tmpname, "%s Port %d", emu->name, i); p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS, 0, &pinfo); - if (p == NULL) { + if (!p) { snd_printk(KERN_ERR "can't create port\n"); return -ENOMEM; } @@ -151,7 +151,7 @@ snd_emux_create_port(struct snd_emux *emu, char *name, } p->chset.channels = kcalloc(max_channels, sizeof(*p->chset.channels), GFP_KERNEL); - if (p->chset.channels == NULL) { + if (!p->chset.channels) { snd_printk(KERN_ERR "no memory\n"); kfree(p); return NULL; @@ -373,7 +373,7 @@ int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card) return 0;
emu->vmidi = kcalloc(emu->midi_ports, sizeof(*emu->vmidi), GFP_KERNEL); - if (emu->vmidi == NULL) + if (!emu->vmidi) return -ENOMEM;
for (i = 0; i < emu->midi_ports; i++) { @@ -405,7 +405,7 @@ int snd_emux_delete_virmidi(struct snd_emux *emu) { int i;
- if (emu->vmidi == NULL) + if (!emu->vmidi) return 0;
for (i = 0; i < emu->midi_ports; i++) {

From: Markus Elfring elfring@users.sourceforge.net Date: Wed, 9 Aug 2017 09:30:34 +0200
Omit extra messages for a memory allocation failure in this function.
This issue was detected by using the Coccinelle software.
Link: http://events.linuxfoundation.org/sites/events/files/slides/LCJ16-Refactor_S... Signed-off-by: Markus Elfring elfring@users.sourceforge.net --- sound/synth/emux/emux_seq.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/sound/synth/emux/emux_seq.c b/sound/synth/emux/emux_seq.c index fd6cbd439511..396c406d0f77 100644 --- a/sound/synth/emux/emux_seq.c +++ b/sound/synth/emux/emux_seq.c @@ -145,14 +145,12 @@ snd_emux_create_port(struct snd_emux *emu, char *name,
/* Allocate structures for this channel */ p = kzalloc(sizeof(*p), GFP_KERNEL); - if (!p) { - snd_printk(KERN_ERR "no memory\n"); + if (!p) return NULL; - } + p->chset.channels = kcalloc(max_channels, sizeof(*p->chset.channels), GFP_KERNEL); if (!p->chset.channels) { - snd_printk(KERN_ERR "no memory\n"); kfree(p); return NULL; }

On Wed, 09 Aug 2017 10:00:23 +0200, SF Markus Elfring wrote:
From: Markus Elfring elfring@users.sourceforge.net Date: Wed, 9 Aug 2017 09:50:05 +0200
A few update suggestions were taken into account from static source code analysis.
Markus Elfring (4): Adjust one function call together with a variable assignment Improve a size determination in two functions Adjust four checks for null pointers Delete two error messages for a failed memory allocation in snd_emux_create_port()
Applied all four patches now. Thanks.
Takashi
participants (2)
-
SF Markus Elfring
-
Takashi Iwai