* make a validity check of parsed channel maps before rteurning theme

* don't overwrite the return buffer unless the parsed channel map is known to be valid


git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@804 fefdeb5f-60dc-0310-8127-8f9354f1896f
This commit is contained in:
Lennart Poettering 2006-04-26 16:07:05 +00:00
parent 185a57cadd
commit c478b0f118

View file

@ -214,42 +214,48 @@ char* pa_channel_map_snprint(char *s, size_t l, const pa_channel_map *map) {
return s; return s;
} }
pa_channel_map *pa_channel_map_parse(pa_channel_map *map, const char *s) { pa_channel_map *pa_channel_map_parse(pa_channel_map *rmap, const char *s) {
const char *state; const char *state;
pa_channel_map map;
char *p; char *p;
assert(map); assert(rmap);
assert(s); assert(s);
memset(map, 0, sizeof(pa_channel_map)); memset(&map, 0, sizeof(map));
if (strcmp(s, "stereo") == 0) { if (strcmp(s, "stereo") == 0) {
map->channels = 2; map.channels = 2;
map->map[0] = PA_CHANNEL_POSITION_LEFT; map.map[0] = PA_CHANNEL_POSITION_LEFT;
map->map[1] = PA_CHANNEL_POSITION_RIGHT; map.map[1] = PA_CHANNEL_POSITION_RIGHT;
return map; goto finish;
} }
state = NULL; state = NULL;
map->channels = 0; map.channels = 0;
while ((p = pa_split(s, ",", &state))) { while ((p = pa_split(s, ",", &state))) {
if (map.channels >= PA_CHANNELS_MAX) {
pa_xfree(p);
return NULL;
}
/* Some special aliases */ /* Some special aliases */
if (strcmp(p, "left") == 0) if (strcmp(p, "left") == 0)
map->map[map->channels++] = PA_CHANNEL_POSITION_LEFT; map.map[map.channels++] = PA_CHANNEL_POSITION_LEFT;
else if (strcmp(p, "right") == 0) else if (strcmp(p, "right") == 0)
map->map[map->channels++] = PA_CHANNEL_POSITION_RIGHT; map.map[map.channels++] = PA_CHANNEL_POSITION_RIGHT;
else if (strcmp(p, "center") == 0) else if (strcmp(p, "center") == 0)
map->map[map->channels++] = PA_CHANNEL_POSITION_CENTER; map.map[map.channels++] = PA_CHANNEL_POSITION_CENTER;
else if (strcmp(p, "subwoofer") == 0) else if (strcmp(p, "subwoofer") == 0)
map->map[map->channels++] = PA_CHANNEL_POSITION_SUBWOOFER; map.map[map.channels++] = PA_CHANNEL_POSITION_SUBWOOFER;
else { else {
pa_channel_position_t i; pa_channel_position_t i;
for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++) for (i = 0; i < PA_CHANNEL_POSITION_MAX; i++)
if (strcmp(p, table[i]) == 0) { if (strcmp(p, table[i]) == 0) {
map->map[map->channels++] = i; map.map[map.channels++] = i;
break; break;
} }
@ -262,13 +268,15 @@ pa_channel_map *pa_channel_map_parse(pa_channel_map *map, const char *s) {
pa_xfree(p); pa_xfree(p);
} }
if (!map->channels) finish:
if (!pa_channel_map_valid(&map))
return NULL; return NULL;
return map; *rmap = map;
return rmap;
} }
int pa_channel_map_valid(const pa_channel_map *map) { int pa_channel_map_valid(const pa_channel_map *map) {
unsigned c; unsigned c;