Skip to contentSkip to conversation

alsa-lib namehint fixes

PATCH New Takashi Iwai 1 participant 6 messages Apr 30, 2015

lore
Share & tools
lore
#0 lore raw

cover alsa-lib namehint fixes

Hi,

here is a series of fixes for alsa-lib namehint stuff. Currently snd_device_name_hint() doesn't return proper lists except for PCM. Also it may do bogus free() or segfault for wrong operations. This patchset addresses such issues and also adds the missing hint descriptions to ctl, seq, hwdep, etc.

Takashi

===

Takashi Iwai (5):
namehint: Fix invalid list access in snd_device_name_hint()
namehint: Fix the listing without device number
namehint: Fix bad free with invalid iface name
Allow hint for ctl, hwdep, timer and seq
conf: Add hint descriptions to ctl, hwdep, seq and timer devices

 include/local.h             |  2 ++
 src/conf/alsa.conf          | 15 ++++++++++++++-
 src/confmisc.c              | 13 +++++++++++++
 src/control/control_hw.c    |  4 +---
 src/control/control_shm.c   |  7 ++-----
 src/control/namehint.c      | 26 ++++++++++++--------------
 src/hwdep/hwdep_hw.c        |  4 +---
 src/pcm/pcm.c               | 12 ------------
 src/pcm/pcm_local.h         |  5 ++---
 src/rawmidi/rawmidi.c       | 18 ------------------
 src/rawmidi/rawmidi_local.h |  2 +-
 src/seq/seq_hw.c            |  4 +---
 src/timer/timer_hw.c        |  4 +---
 src/timer/timer_query_hw.c  |  4 +---
 14 files changed, 51 insertions(+), 69 deletions(-)
Signature · 1 line
2.3.6
#1 lore raw

PATCH 1/5 namehint: Fix invalid list access in snd_device_name_hint()

snd_device_name_hint() tries to free the allocated list at the error path via snd_device_name_free_hint(). But snd_device_name_free_hint() expects a list terminated by NULL while snd_device_name_hint() doesn't add it. Adding it may again result in an error and thus isn't guaranteed to work. Hence we can't add NULL at the error path.

Instead, now the code always allocates one entry more, and zero-clears the newly allocated beforehand to guarantee the NULL termination.

---
 src/control/namehint.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
src/control/namehint.c +10 -11
index 28975a400b75..66de634d3550 100644
@@ -52,10 +52,11 @@ static int hint_list_add(struct hint_list *list,
 {
 	char *x;
 
-	if (list->count == list->allocated) {
+	if (list->count + 1 >= list->allocated) {
 		char **n = realloc(list->list, (list->allocated + 10) * sizeof(char *));
 		if (n == NULL)
 			return -ENOMEM;
+		memset(n + list->allocated, 0, 10 * sizeof(*n));
 		list->allocated += 10;
 		list->list = n;
 	}
@@ -620,18 +621,16 @@ int snd_device_name_hint(int card, const char *iface, void ***hints)
 	}
 	err = 0;
       __error:
-      	if (err < 0) {
+	/* add an empty entry if nothing has been added yet; the caller
+	 * expects non-NULL return
+	 */
+	if (!err && !list.list)
+		err = hint_list_add(&list, NULL, NULL);
+	if (err < 0)
       		snd_device_name_free_hint((void **)list.list);
-      		if (list.cardname)
-	      		free(list.cardname);
-      	} else {
-      		err = hint_list_add(&list, NULL, NULL);
-      		if (err < 0)
-      			goto __error;
+	else
       		*hints = (void **)list.list;
-      		if (list.cardname)
-	      		free(list.cardname);
-	}
+	free(list.cardname);
 	if (local_config_rw)
 		snd_config_delete(local_config_rw);
 	if (local_config)
Signature · 1 line
2.3.6
#2 lore raw

PATCH 2/5 namehint: Fix the listing without device number

In reply to Takashi Iwai

The current code of snd_device_name_hint() has a bug when listing up devices without the device index (e.g. ctl). Because it assigns the default device index 0 unconditionally and it has a check at the later point to filter entries with dev >= 0, it ended up with empty outputs.

The fix is simply to remove the bogus assignment of dev = 0.

---
 src/control/namehint.c | 1 -
 1 file changed, 1 deletion(-)
src/control/namehint.c +0 -1
index 66de634d3550..6c04143a185c 100644
@@ -272,7 +272,6 @@ static int try_config(snd_config_t *config,
 	if (snd_config_search(cfg1, "type", &cfg) >= 0 &&
 	    snd_config_get_string(cfg, &str) >= 0 &&
 	    strcmp(str, "hw") == 0) {
-	    	dev = 0;
 		list->device_input = -1;
 		list->device_output = -1;
 		if (snd_config_search(cfg1, "device", &cfg) >= 0) {
Signature · 1 line
2.3.6
#3 lore raw

PATCH 3/5 namehint: Fix bad free with invalid iface name

In reply to Takashi Iwai

Due to the uninitialized field before the error path, passing an invalid iface argument may result in a bad free() call. Initialize the fields properly beforehand.

---
 src/control/namehint.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/control/namehint.c +2 -2
index 6c04143a185c..b3e646eb10af 100644
@@ -562,6 +562,8 @@ int snd_device_name_hint(int card, const char *iface, void ***hints)
 	list.list = NULL;
 	list.count = list.allocated = 0;
 	list.siface = iface;
+	list.show_all = 0;
+	list.cardname = NULL;
 	if (strcmp(iface, "card") == 0)
 		list.iface = SND_CTL_ELEM_IFACE_CARD;
 	else if (strcmp(iface, "pcm") == 0)
@@ -581,8 +583,6 @@ int snd_device_name_hint(int card, const char *iface, void ***hints)
 		goto __error;
 	}
 
-	list.show_all = 0;
-	list.cardname = NULL;
 	if (snd_config_search(local_config, "defaults.namehint.showall", &conf) >= 0)
 		list.show_all = snd_config_get_bool(conf) > 0;
 	if (card >= 0) {
Signature · 1 line
2.3.6
#4 lore raw

PATCH 4/5 Allow hint for ctl, hwdep, timer and seq

In reply to Takashi Iwai

Like pcm and rawmidi, each object parser needs to accept the hint component. Now a new local function _snd_conf_generic_id() was introduced to replace each call of "comment" and "type" field checks.

Also, the two existing identical functions for pcm and rawmidi are removed and the new function is used commonly.

---
 include/local.h             |  2 ++
 src/confmisc.c              | 13 +++++++++++++
 src/control/control_hw.c    |  4 +---
 src/control/control_shm.c   |  7 ++-----
 src/hwdep/hwdep_hw.c        |  4 +---
 src/pcm/pcm.c               | 12 ------------
 src/pcm/pcm_local.h         |  5 ++---
 src/rawmidi/rawmidi.c       | 18 ------------------
 src/rawmidi/rawmidi_local.h |  2 +-
 src/seq/seq_hw.c            |  4 +---
 src/timer/timer_hw.c        |  4 +---
 src/timer/timer_query_hw.c  |  4 +---
 12 files changed, 25 insertions(+), 54 deletions(-)
include/local.h +2 -0
index 2fe9a273f0b0..660081638a1c 100644
@@ -348,4 +348,6 @@ int snd_config_search_alias_hooks(snd_config_t *config,
                                   const char *base, const char *key,
 				  snd_config_t **result);
 
+int _snd_conf_generic_id(const char *id);
+
 #endif
src/confmisc.c +13 -0
index af686bea323c..1fb4f282217e 100644
@@ -1302,3 +1302,16 @@ int snd_func_refer(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
 #ifndef DOC_HIDDEN
 SND_DLSYM_BUILD_VERSION(snd_func_refer, SND_CONFIG_DLSYM_VERSION_EVALUATE);
 #endif
+
+#ifndef DOC_HIDDEN
+int _snd_conf_generic_id(const char *id)
+{
+	static const char ids[3][8] = { "comment", "type", "hint" };
+	unsigned int k;
+	for (k = 0; k < sizeof(ids) / sizeof(ids[0]); ++k) {
+		if (strcmp(id, ids[k]) == 0)
+			return 1;
+	}
+	return 0;
+}
+#endif
src/control/control_hw.c +1 -3
index dfc9dcd51e20..7d23151c7d75 100644
@@ -446,9 +446,7 @@ int _snd_ctl_hw_open(snd_ctl_t **handlep, char *name, snd_config_t *root ATTRIBU
 		const char *id;
 		if (snd_config_get_id(n, &id) < 0)
 			continue;
-		if (strcmp(id, "comment") == 0)
-			continue;
-		if (strcmp(id, "type") == 0)
+		if (_snd_conf_generic_id(id))
 			continue;
 		if (strcmp(id, "card") == 0) {
 			err = snd_config_get_integer(n, &card);
src/control/control_shm.c +2 -5
index 40bc705d8036..bd07d4af503a 100644
@@ -551,10 +551,7 @@ int _snd_ctl_shm_open(snd_ctl_t **handlep, char *name, snd_config_t *root, snd_c
 		const char *id;
 		if (snd_config_get_id(n, &id) < 0)
 			continue;
-		if (strcmp(id, "comment") == 0)
-			continue;
-		if (strcmp(id, "type") == 0)
-			continue;
+		if (_snd_conf_generic_id(id))
 		if (strcmp(id, "server") == 0) {
 			err = snd_config_get_string(n, &server);
 			if (err < 0) {
@@ -597,7 +594,7 @@ int _snd_ctl_shm_open(snd_ctl_t **handlep, char *name, snd_config_t *root, snd_c
 		const char *id;
 		if (snd_config_get_id(n, &id) < 0)
 			continue;
-		if (strcmp(id, "comment") == 0)
+		if (_snd_conf_generic_id(id))
 			continue;
 		if (strcmp(id, "host") == 0)
 			continue;
src/hwdep/hwdep_hw.c +1 -3
index 4314e32bade6..12528c55bac7 100644
@@ -158,9 +158,7 @@ int _snd_hwdep_hw_open(snd_hwdep_t **hwdep, char *name,
 		const char *id;
 		if (snd_config_get_id(n, &id) < 0)
 			continue;
-		if (strcmp(id, "comment") == 0)
-			continue;
-		if (strcmp(id, "type") == 0)
+		if (_snd_conf_generic_id(id))
 			continue;
 		if (strcmp(id, "card") == 0) {
 			err = snd_config_get_integer(n, &card);
src/pcm/pcm.c +0 -12
index e74e02fc568f..bc18954b92da 100644
@@ -7059,18 +7059,6 @@ int snd_pcm_slave_conf(snd_config_t *root, snd_config_t *conf,
 	return err;
 }
 		
-
-int snd_pcm_conf_generic_id(const char *id)
-{
-	static const char ids[3][8] = { "comment", "type", "hint" };
-	unsigned int k;
-	for (k = 0; k < sizeof(ids) / sizeof(ids[0]); ++k) {
-		if (strcmp(id, ids[k]) == 0)
-			return 1;
-	}
-	return 0;
-}
-
 static void snd_pcm_set_ptr(snd_pcm_t *pcm, snd_pcm_rbptr_t *rbptr,
 			    volatile snd_pcm_uframes_t *hw_ptr, int fd, off_t offset)
 {
src/pcm/pcm_local.h +2 -3
index 394505f978ac..326618ecd0c0 100644
@@ -262,8 +262,6 @@ struct _snd_pcm {
 	snd1_pcm_areas_from_bufs
 #define snd_pcm_open_named_slave \
 	snd1_pcm_open_named_slave
-#define snd_pcm_conf_generic_id \
-	snd1_pcm_conf_generic_id
 #define snd_pcm_hw_open_fd \
 	snd1_pcm_hw_open_fd
 #define snd_pcm_wait_nocheck \
@@ -882,7 +880,8 @@ snd_pcm_open_slave(snd_pcm_t **pcmp, snd_config_t *root,
 	return snd_pcm_open_named_slave(pcmp, NULL, root, conf, stream,
 					mode, parent_conf);
 }
-int snd_pcm_conf_generic_id(const char *id);
+
+#define snd_pcm_conf_generic_id(id) _snd_conf_generic_id(id)
 
 int snd_pcm_hw_open_fd(snd_pcm_t **pcmp, const char *name, int fd, int mmap_emulation, int sync_ptr_ioctl);
 int __snd_pcm_mmap_emul_open(snd_pcm_t **pcmp, const char *name,
src/rawmidi/rawmidi.c +0 -18
index ac699b439b11..0c89b8b984b9 100644
@@ -990,21 +990,3 @@ ssize_t snd_rawmidi_read(snd_rawmidi_t *rawmidi, void *buffer, size_t size)
 	assert(buffer || size == 0);
 	return (rawmidi->ops->read)(rawmidi, buffer, size);
 }
-
-#ifndef DOC_HIDDEN
-int snd_rawmidi_conf_generic_id(const char *id)
-{
-	static const char ids[][8] = {
-		"comment",
-		"type",
-		"hint",
-	};
-	unsigned int k;
-
-	for (k = 0; k < sizeof ids / sizeof *ids; ++k) {
-		if (strcmp(id, ids[k]) == 0)
-			return 1;
-	}
-	return 0;
-}
-#endif
src/rawmidi/rawmidi_local.h +1 -1
index 3388502cece0..8992771eb5d3 100644
@@ -58,4 +58,4 @@ int snd_rawmidi_virtual_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
 			     const char *name, snd_seq_t *seq_handle, int port,
 			     int merge, int mode);
 
-int snd_rawmidi_conf_generic_id(const char *id);
+#define snd_rawmidi_conf_generic_id(id)	_snd_conf_generic_id(id)
src/seq/seq_hw.c +1 -3
index 6cb31d6f4c25..d03336738944 100644
@@ -546,9 +546,7 @@ int _snd_seq_hw_open(snd_seq_t **handlep, char *name,
 		const char *id;
 		if (snd_config_get_id(n, &id) < 0)
 			continue;
-		if (strcmp(id, "comment") == 0)
-			continue;
-		if (strcmp(id, "type") == 0)
+		if (_snd_conf_generic_id(id))
 			continue;
 		return -EINVAL;
 	}
src/timer/timer_hw.c +1 -3
index aa6a0b1b42f1..e833fc8cbb41 100644
@@ -299,9 +299,7 @@ int _snd_timer_hw_open(snd_timer_t **timer, char *name,
 		const char *id;
 		if (snd_config_get_id(n, &id) < 0)
 			continue;
-		if (strcmp(id, "comment") == 0)
-			continue;
-		if (strcmp(id, "type") == 0)
+		if (_snd_conf_generic_id(id))
 			continue;
 		if (strcmp(id, "class") == 0) {
 			err = snd_config_get_integer(n, &dev_class);
src/timer/timer_query_hw.c +1 -3
index 9f62b78aeb2e..289ca52f2afa 100644
@@ -134,9 +134,7 @@ int _snd_timer_query_hw_open(snd_timer_query_t **timer, char *name,
 		const char *id;
 		if (snd_config_get_id(n, &id) < 0)
 			continue;
-		if (strcmp(id, "comment") == 0)
-			continue;
-		if (strcmp(id, "type") == 0)
+		if (_snd_conf_generic_id(id))
 			continue;
 		SNDERR("Unexpected field %s", id);
 		return -EINVAL;
Signature · 1 line
2.3.6
#5 lore raw

PATCH 5/5 conf: Add hint descriptions to ctl, hwdep, seq and timer devices

In reply to Takashi Iwai
---
 src/conf/alsa.conf | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
src/conf/alsa.conf +14 -1
index 5c928e8afbcd..f22918fbbf83 100644
@@ -345,6 +345,7 @@ ctl.sysdefault {
 			name defaults.ctl.card
 		}
 	}
+	hint.description "Default control device"
 }
 ctl.default ctl.sysdefault
 
@@ -366,6 +367,7 @@ ctl.hw {
 	}
 	type hw
 	card $CARD
+	hint.description "Direct control device"
 }
 
 ctl.shm {
@@ -408,6 +410,7 @@ rawmidi.default {
 			name defaults.rawmidi.device
 		}
 	}
+	hint.description "Default raw MIDI device"
 }
 
 rawmidi.hw {
@@ -469,6 +472,7 @@ rawmidi.virtual {
 
 seq.default {
 	type hw
+	hint.description "Default sequencer device"
 }
 
 seq.hw {
@@ -502,6 +506,7 @@ hwdep.default {
 			name defaults.hwdep.device
 		}
 	}
+	hint.description "Default hardware dependent device"
 }
 
 hwdep.hw {
@@ -536,6 +541,10 @@ hwdep.hw {
 	type hw
 	card $CARD
 	device $DEV
+	hint {
+		description "Direct hardware dependent device"
+		device $DEV
+	}
 }
 
 #
@@ -572,7 +581,7 @@ timer.default {
 		@func refer
 		name defaults.timer.subdevice
 	}
-	hint.description "Default direct hardware timer device"
+	hint.description "Default timer device"
 }
 
 timer.hw {
@@ -618,4 +627,8 @@ timer.hw {
 	card $CARD
 	device $DEV
 	subdevice $SUBDEV
+	hint {
+		description "Direct timer device"
+		device $DEV
+	}
 }
Signature · 1 line
2.3.6