#222434 amixer: Should reject invalid commands

Package:
alsa-utils
Source:
alsa-utils
Description:
Utilities for configuring and using ALSA
Submitter:
Dan Jacobson
Date:
2015-12-29 02:09:04 UTC
Severity:
minor
#222434#5
Date:
2003-11-29 02:16:34 UTC
From:
To:
The following proves that amixer accepts nonsense arguments with no
hint to the user that nothing was done,

$ amixer set Master,0 nocap; amixer|sum
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 31
  Front Left: Playback 23 [74%] [on]
  Front Right: Playback 23 [74%] [on]
63938     6
$ amixer set Master,0 cap; amixer|sum
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 31
  Front Left: Playback 23 [74%] [on]
  Front Right: Playback 23 [74%] [on]
63938     6

Furthermore, not even $? was set [shell error value].

#222434#20
Date:
2006-01-30 09:04:07 UTC
From:
To:
The patch brings amixer to reject invalid commands, for example:	 "amixer set
Master,0 nocap" makes no sense, if there is no capture switch at the Master
Mixer Control. It also handles the invalid command, when more than 1 command is
submited. For example: "amixer set Master,0 nocap 50%,50%" does nothing.

Additional it rejects commands like "amixer -c 1 get Line,0 80%,40% unmute cap"
where unnecessary options are submited.

Signed-off-by: Torsten Büchner <buechi@rz.uni-potsdam.de>

Patch for alsa-utils-1.0.10:
--- amixer.orig.c	2005-09-22 14:47:02.000000000 +0200
+++ amixer.c	2006-01-30 08:01:58.000000000 +0100
@@ -1188,6 +1188,10 @@
 		fprintf(stderr, "Specify what you want to set...\n");
 		return 1;
 	}
+  if (roflag && !(argc < 2)) {
+    fprintf(stderr, "Unknown option: %s\n", argv[1]);
+    return 1;
+  }
 	if ((err = snd_mixer_open(&handle, 0)) < 0) {
 		error("Mixer %s open error: %s\n", card, snd_strerror(err));
 		return err;
@@ -1218,6 +1222,28 @@
 		goto __skip_write;
 	snd_mixer_selem_get_playback_volume_range(elem, &pmin, &pmax);
 	snd_mixer_selem_get_capture_volume_range(elem, &cmin, &cmax);
+  for (idx = 1; idx < argc; idx++) {
+    char *ptr = argv[idx];
+    channels = channels_mask(&ptr, channels);
+    if (*ptr == '\0')
+      continue;
+    dir = dir_mask(&ptr, dir);
+    if (*ptr == '\0')
+      continue;
+    for (chn = 0; chn <= SND_MIXER_SCHN_LAST; chn++) {
+      if (!(channels & (1 << chn)))
+        continue;
+      if ((!strncmp(ptr, "mute", 4) || !strncmp(ptr, "off", 3) || !strncmp(ptr,
"unmute", 6) ||
+         !strncmp(ptr, "on", 2) || !strncmp(ptr, "toggle", 6)) &&
snd_mixer_selem_has_playback_switch(elem)) continue;
+      if ((isdigit(*ptr) || *ptr == '-' || *ptr == '+') &&
snd_mixer_selem_has_playback_volume(elem)) continue;
+
+      if ((!strncmp(ptr, "cap", 3) || !strncmp(ptr, "rec", 3) || !strncmp(ptr,
"nocap", 5) ||
+         !strncmp(ptr, "norec", 5) || !strncmp(ptr, "toggle", 6)) &&
snd_mixer_selem_has_capture_switch(elem)) continue;
+      if ((isdigit(*ptr) || *ptr == '-' || *ptr == '+') &&
snd_mixer_selem_has_capture_volume(elem)) continue;
+      fprintf(stderr, "Setup not available: %s\n",ptr);
+      return 1;
+    }
+  }
 	for (idx = 1; idx < argc; idx++) {
 		char *ptr = argv[idx], *optr;
 		int multi, firstchn = 1;