amixer: Parse the value more strictly

So far amixer allows some unexpected suffix and assumes as a raw
absolute value without returning an error.  This is rather dangerous,
e.g. user might not notice that a completely wrong value was set when
the command line included a typo.

This patch makes the parser a bit more strict: it doesn't allow any
longer invalid suffixes, instead either returns an error or skips the
invalid value, depending on the operation mode.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Iwai 2014-11-23 10:04:24 +01:00
parent 45a334e71c
commit 088593c039

View file

@ -325,7 +325,7 @@ static int set_volume_simple(snd_mixer_elem_t *elem,
long val, orig, pmin, pmax; long val, orig, pmin, pmax;
char *p = *ptr, *s; char *p = *ptr, *s;
int invalid = 0, percent = 0, err = 0; int invalid = 0, percent = 0, err = 0;
int vol_type = std_vol_type; int vol_type;
double scale = 1.0; double scale = 1.0;
int correct = 0; int correct = 0;
@ -344,14 +344,19 @@ static int set_volume_simple(snd_mixer_elem_t *elem,
strtol(p, &p, 10); strtol(p, &p, 10);
} }
if (*p == '%') { if (*p == '%') {
vol_type = std_vol_type;
percent = 1; percent = 1;
p++; p++;
} else if (toupper(p[0]) == 'D' && toupper(p[1]) == 'B') { } else if (toupper(p[0]) == 'D' && toupper(p[1]) == 'B') {
vol_type = VOL_DB; vol_type = VOL_DB;
p += 2; p += 2;
scale = 100; scale = 100;
} else } else {
vol_type = VOL_RAW; vol_type = VOL_RAW;
}
if (*p && !strchr(",:+-", *p))
invalid = 1;
val = (long)(strtod(s, NULL) * scale); val = (long)(strtod(s, NULL) * scale);
if (vol_ops[dir].v[vol_type].get_range(elem, &pmin, &pmax) < 0) if (vol_ops[dir].v[vol_type].get_range(elem, &pmin, &pmax) < 0)
@ -372,6 +377,10 @@ static int set_volume_simple(snd_mixer_elem_t *elem,
} }
p++; p++;
} }
if (*p && !strchr(",:", *p))
invalid = 1;
if (! invalid) { if (! invalid) {
val = check_range(val, pmin, pmax); val = check_range(val, pmin, pmax);
err = vol_ops[dir].v[vol_type].set(elem, chn, val, correct); err = vol_ops[dir].v[vol_type].set(elem, chn, val, correct);