[ALSA] seq: resource leak fix and various code cleanups

This patch fixes:
1) a resource leak (CID: 1817)
2) various code cleanups

Signed-off-by: Eugene Teo <eugeneteo@kernel.sg>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Jaroslav Kysela <perex@suse.cz>
This commit is contained in:
Eugene Teo 2007-08-07 14:34:23 +02:00 committed by Jaroslav Kysela
parent 2880a8670d
commit 7034632d88
2 changed files with 28 additions and 20 deletions

View File

@ -176,28 +176,28 @@ snd_seq_oss_open(struct file *file, int level)
int i, rc; int i, rc;
struct seq_oss_devinfo *dp; struct seq_oss_devinfo *dp;
if ((dp = kzalloc(sizeof(*dp), GFP_KERNEL)) == NULL) { dp = kzalloc(sizeof(*dp), GFP_KERNEL);
if (!dp) {
snd_printk(KERN_ERR "can't malloc device info\n"); snd_printk(KERN_ERR "can't malloc device info\n");
return -ENOMEM; return -ENOMEM;
} }
debug_printk(("oss_open: dp = %p\n", dp)); debug_printk(("oss_open: dp = %p\n", dp));
dp->cseq = system_client;
dp->port = -1;
dp->queue = -1;
for (i = 0; i < SNDRV_SEQ_OSS_MAX_CLIENTS; i++) { for (i = 0; i < SNDRV_SEQ_OSS_MAX_CLIENTS; i++) {
if (client_table[i] == NULL) if (client_table[i] == NULL)
break; break;
} }
if (i >= SNDRV_SEQ_OSS_MAX_CLIENTS) {
snd_printk(KERN_ERR "too many applications\n");
kfree(dp);
return -ENOMEM;
}
dp->index = i; dp->index = i;
dp->cseq = system_client; if (i >= SNDRV_SEQ_OSS_MAX_CLIENTS) {
dp->port = -1; snd_printk(KERN_ERR "too many applications\n");
dp->queue = -1; rc = -ENOMEM;
dp->readq = NULL; goto _error;
dp->writeq = NULL; }
/* look up synth and midi devices */ /* look up synth and midi devices */
snd_seq_oss_synth_setup(dp); snd_seq_oss_synth_setup(dp);
@ -211,14 +211,16 @@ snd_seq_oss_open(struct file *file, int level)
/* create port */ /* create port */
debug_printk(("create new port\n")); debug_printk(("create new port\n"));
if ((rc = create_port(dp)) < 0) { rc = create_port(dp);
if (rc < 0) {
snd_printk(KERN_ERR "can't create port\n"); snd_printk(KERN_ERR "can't create port\n");
goto _error; goto _error;
} }
/* allocate queue */ /* allocate queue */
debug_printk(("allocate queue\n")); debug_printk(("allocate queue\n"));
if ((rc = alloc_seq_queue(dp)) < 0) rc = alloc_seq_queue(dp);
if (rc < 0)
goto _error; goto _error;
/* set address */ /* set address */
@ -235,7 +237,8 @@ snd_seq_oss_open(struct file *file, int level)
/* initialize read queue */ /* initialize read queue */
debug_printk(("initialize read queue\n")); debug_printk(("initialize read queue\n"));
if (is_read_mode(dp->file_mode)) { if (is_read_mode(dp->file_mode)) {
if ((dp->readq = snd_seq_oss_readq_new(dp, maxqlen)) == NULL) { dp->readq = snd_seq_oss_readq_new(dp, maxqlen);
if (!dp->readq) {
rc = -ENOMEM; rc = -ENOMEM;
goto _error; goto _error;
} }
@ -245,7 +248,7 @@ snd_seq_oss_open(struct file *file, int level)
debug_printk(("initialize write queue\n")); debug_printk(("initialize write queue\n"));
if (is_write_mode(dp->file_mode)) { if (is_write_mode(dp->file_mode)) {
dp->writeq = snd_seq_oss_writeq_new(dp, maxqlen); dp->writeq = snd_seq_oss_writeq_new(dp, maxqlen);
if (dp->writeq == NULL) { if (!dp->writeq) {
rc = -ENOMEM; rc = -ENOMEM;
goto _error; goto _error;
} }
@ -253,7 +256,8 @@ snd_seq_oss_open(struct file *file, int level)
/* initialize timer */ /* initialize timer */
debug_printk(("initialize timer\n")); debug_printk(("initialize timer\n"));
if ((dp->timer = snd_seq_oss_timer_new(dp)) == NULL) { dp->timer = snd_seq_oss_timer_new(dp);
if (!dp->timer) {
snd_printk(KERN_ERR "can't alloc timer\n"); snd_printk(KERN_ERR "can't alloc timer\n");
rc = -ENOMEM; rc = -ENOMEM;
goto _error; goto _error;
@ -276,11 +280,13 @@ snd_seq_oss_open(struct file *file, int level)
return 0; return 0;
_error: _error:
snd_seq_oss_writeq_delete(dp->writeq);
snd_seq_oss_readq_delete(dp->readq);
snd_seq_oss_synth_cleanup(dp); snd_seq_oss_synth_cleanup(dp);
snd_seq_oss_midi_cleanup(dp); snd_seq_oss_midi_cleanup(dp);
i = dp->queue;
delete_port(dp); delete_port(dp);
delete_seq_queue(i); delete_seq_queue(dp->queue);
kfree(dp);
return rc; return rc;
} }

View File

@ -63,8 +63,10 @@ snd_seq_oss_writeq_new(struct seq_oss_devinfo *dp, int maxlen)
void void
snd_seq_oss_writeq_delete(struct seq_oss_writeq *q) snd_seq_oss_writeq_delete(struct seq_oss_writeq *q)
{ {
snd_seq_oss_writeq_clear(q); /* to be sure */ if (q) {
kfree(q); snd_seq_oss_writeq_clear(q); /* to be sure */
kfree(q);
}
} }