Skip to contentSkip to conversation

ALSA: sonicvibes: add error handling for snd_ctl_add

PATCH In review Zhouyang Jia 2 participants 7 messages Jun 11, 2018 – Jun 14, 2018 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: sonicvibes: add error handling for snd_ctl_add

When snd_ctl_add fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling snd_ctl_add.

---
 sound/pci/sonicvibes.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
sound/pci/sonicvibes.c +5 -1
index a8abb15..57cb77a 100644
@@ -1188,6 +1188,7 @@ SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
 static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 {
 	struct gameport *gp;
+	int err;
 
 	sonic->gameport = gp = gameport_allocate_port();
 	if (!gp) {
@@ -1203,7 +1204,10 @@ static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 
 	gameport_register_port(gp);
 
-	snd_ctl_add(sonic->card, snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	err = snd_ctl_add(sonic->card,
+		snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	if (err < 0)
+		return err;
 
 	return 0;
 }
Signature · 1 line
2.7.4
#1 lore raw
Quoted 7 lines · Zhouyang Jia

When snd_ctl_add fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling snd_ctl_add.

I postpone this one because the change won't give anything as is although this change itself is correct. If you look at the code closely, the caller of snd_sonicvibes_create_gameport() ignores the return value.
So we need to fix two places.

thanks,

Takashi

#2 lore raw

Hi,

I'll fix this in v2. Thanks for your kind reply.

Best,
Zhouyang

2018-06-11 21:26 GMT+08:00 Takashi Iwai <[email protected]>:

Quoted 21 lines
Quoted 7 lines · Zhouyang Jia

When snd_ctl_add fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling snd_ctl_add.

I postpone this one because the change won't give anything as is although this change itself is correct. If you look at the code closely, the caller of snd_sonicvibes_create_gameport() ignores the return value.
So we need to fix two places.

thanks,

Takashi

#3 lore raw

PATCH v2 1/1 ALSA: sonicvibes: add error handling for snd_ctl_add

In reply to Zhouyang Jia

When snd_ctl_add fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling snd_ctl_add.

---
v1->v2:
- Check the return value of snd_sonicvibes_create_gameport.

---
 sound/pci/sonicvibes.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
sound/pci/sonicvibes.c +9 -2
index a8abb15..75225b04 100644
@@ -1188,6 +1188,7 @@ SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
 static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 {
 	struct gameport *gp;
+	int err;
 
 	sonic->gameport = gp = gameport_allocate_port();
 	if (!gp) {
@@ -1203,7 +1204,10 @@ static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 
 	gameport_register_port(gp);
 
-	snd_ctl_add(sonic->card, snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	err = snd_ctl_add(sonic->card,
+		snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	if (err < 0)
+		return err;
 
 	return 0;
 }
@@ -1515,7 +1519,10 @@ static int snd_sonic_probe(struct pci_dev *pci,
 		return err;
 	}
 
-	snd_sonicvibes_create_gameport(sonic);
+	if ((err = snd_sonicvibes_create_gameport(sonic)) < 0) {
+		snd_card_free(card);
+		return err;
+        }
 
 	if ((err = snd_card_register(card)) < 0) {
 		snd_card_free(card);
Signature · 1 line
2.7.4
#4 lore raw
Quoted 39 lines · Zhouyang Jia

When snd_ctl_add fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling snd_ctl_add.

---
v1->v2:
- Check the return value of snd_sonicvibes_create_gameport.

---
 sound/pci/sonicvibes.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
sound/pci/sonicvibes.c +5 -1
index a8abb15..75225b04 100644
@@ -1188,6 +1188,7 @@ SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
 static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 {
 	struct gameport *gp;
+	int err;
 
 	sonic->gameport = gp = gameport_allocate_port();
 	if (!gp) {
@@ -1203,7 +1204,10 @@ static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 
 	gameport_register_port(gp);
 
-	snd_ctl_add(sonic->card, snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	err = snd_ctl_add(sonic->card,
+		snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	if (err < 0)
+		return err;
 
 	return 0;
 }

sound/pci/sonicvibes.c static int snd_sonic_probe(struct pci_dev *pci, · line 1525

Quoted 8 lines
sound/pci/sonicvibes.c +4 -1
@@ -1515,7 +1519,10 @@ static int snd_sonic_probe(struct pci_dev *pci,
 		return err;
 	}
 
-	snd_sonicvibes_create_gameport(sonic);
+	if ((err = snd_sonicvibes_create_gameport(sonic)) < 0) {
+		snd_card_free(card);
+		return err;
+        }

You don't need to inherit the old-fashioned style "if ((err = xxx)" in a new code. Check what checkpatch.pl complains.

thanks,

Takashi

#5 lore raw

PATCH v3 1/1 ALSA: sonicvibes: add error handling for snd_ctl_add

In reply to Zhouyang Jia

When snd_ctl_add fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling snd_ctl_add.

---
v1->v2:
- Check the return value of snd_sonicvibes_create_gameport. v2->v3:
- Fix the code style.

---
 sound/pci/sonicvibes.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
sound/pci/sonicvibes.c +10 -2
index a8abb15..7fbdb70 100644
@@ -1188,6 +1188,7 @@ SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
 static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 {
 	struct gameport *gp;
+	int err;
 
 	sonic->gameport = gp = gameport_allocate_port();
 	if (!gp) {
@@ -1203,7 +1204,10 @@ static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
 
 	gameport_register_port(gp);
 
-	snd_ctl_add(sonic->card, snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	err = snd_ctl_add(sonic->card,
+		snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
+	if (err < 0)
+		return err;
 
 	return 0;
 }
@@ -1515,7 +1519,11 @@ static int snd_sonic_probe(struct pci_dev *pci,
 		return err;
 	}
 
-	snd_sonicvibes_create_gameport(sonic);
+	err = snd_sonicvibes_create_gameport(sonic);
+	if (err < 0) {
+		snd_card_free(card);
+		return err;
+	}
 
 	if ((err = snd_card_register(card)) < 0) {
 		snd_card_free(card);
Signature · 1 line
2.7.4
Applied
#6 lore raw
Quoted 4 lines · Zhouyang Jia

When snd_ctl_add fails, the lack of error-handling code may cause unexpected results.

Quoted 8 lines

This patch adds error-handling code after calling snd_ctl_add.

---
v1->v2:
- Check the return value of snd_sonicvibes_create_gameport. v2->v3:
- Fix the code style.

Applied, thanks.

Takashi