mirror of
				https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
				synced 2025-11-03 09:01:50 -05:00 
			
		
		
		
	Add pa_channels_valid()
I think this makes the code a bit nicer to read and write. This also reduces the chances of off-by-one errors when checking the bounds of channel count values.
This commit is contained in:
		
							parent
							
								
									a67318f8af
								
							
						
					
					
						commit
						2747c96101
					
				
					 12 changed files with 24 additions and 22 deletions
				
			
		| 
						 | 
				
			
			@ -387,7 +387,7 @@ static int parse_sample_channels(pa_config_parser_state *state) {
 | 
			
		|||
 | 
			
		||||
    i = state->data;
 | 
			
		||||
 | 
			
		||||
    if (pa_atoi(state->rvalue, &n) < 0 || n > (int32_t) PA_CHANNELS_MAX || n <= 0) {
 | 
			
		||||
    if (pa_atoi(state->rvalue, &n) < 0 || !pa_channels_valid(n)) {
 | 
			
		||||
        pa_log(_("[%s:%u] Invalid sample channels '%s'."), state->filename, state->lineno, state->rvalue);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,6 +25,7 @@ pa_channel_map_valid;
 | 
			
		|||
pa_channel_position_from_string;
 | 
			
		||||
pa_channel_position_to_pretty_string;
 | 
			
		||||
pa_channel_position_to_string;
 | 
			
		||||
pa_channels_valid;
 | 
			
		||||
pa_context_add_autoload;
 | 
			
		||||
pa_context_connect;
 | 
			
		||||
pa_context_disconnect;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -214,7 +214,7 @@ static int ucm_get_device_property(
 | 
			
		|||
    value = pa_proplist_gets(device->proplist, PA_ALSA_PROP_UCM_PLAYBACK_CHANNELS);
 | 
			
		||||
    if (value) { /* output */
 | 
			
		||||
        /* get channels */
 | 
			
		||||
        if (pa_atou(value, &ui) == 0 && ui < PA_CHANNELS_MAX)
 | 
			
		||||
        if (pa_atou(value, &ui) == 0 && pa_channels_valid(ui))
 | 
			
		||||
            device->playback_channels = ui;
 | 
			
		||||
        else
 | 
			
		||||
            pa_log("UCM playback channels %s for device %s out of range", value, device_name);
 | 
			
		||||
| 
						 | 
				
			
			@ -234,7 +234,7 @@ static int ucm_get_device_property(
 | 
			
		|||
    value = pa_proplist_gets(device->proplist, PA_ALSA_PROP_UCM_CAPTURE_CHANNELS);
 | 
			
		||||
    if (value) { /* input */
 | 
			
		||||
        /* get channels */
 | 
			
		||||
        if (pa_atou(value, &ui) == 0 && ui < PA_CHANNELS_MAX)
 | 
			
		||||
        if (pa_atou(value, &ui) == 0 && pa_channels_valid(ui))
 | 
			
		||||
            device->capture_channels = ui;
 | 
			
		||||
        else
 | 
			
		||||
            pa_log("UCM capture channels %s for device %s out of range", value, device_name);
 | 
			
		||||
| 
						 | 
				
			
			@ -1128,7 +1128,7 @@ static void alsa_mapping_add_ucm_modifier(pa_alsa_mapping *m, pa_alsa_ucm_modifi
 | 
			
		|||
        /* FIXME: channel_str is unsanitized input from the UCM configuration,
 | 
			
		||||
         * we should do proper error handling instead of asserting.
 | 
			
		||||
         * https://bugs.freedesktop.org/show_bug.cgi?id=71823 */
 | 
			
		||||
        pa_assert_se(pa_atou(channel_str, &channels) == 0 && channels < PA_CHANNELS_MAX);
 | 
			
		||||
        pa_assert_se(pa_atou(channel_str, &channels) == 0 && pa_channels_valid(channels));
 | 
			
		||||
        pa_log_debug("Got channel count %" PRIu32 " for modifier", channels);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2463,7 +2463,7 @@ int pa__init(pa_module *m) {
 | 
			
		|||
 | 
			
		||||
    channels = u->sample_spec.channels;
 | 
			
		||||
    if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
 | 
			
		||||
        channels <= 0 || channels > PA_CHANNELS_MAX) {
 | 
			
		||||
        !pa_channels_valid(channels)) {
 | 
			
		||||
        pa_log_error("Failed to get channels from module arguments");
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -350,8 +350,7 @@ int pa__init(pa_module*m) {
 | 
			
		|||
        channels = m->core->default_sample_spec.channels;
 | 
			
		||||
 | 
			
		||||
    if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
 | 
			
		||||
        channels <= 0 ||
 | 
			
		||||
        channels > PA_CHANNELS_MAX) {
 | 
			
		||||
        !pa_channels_valid(channels)) {
 | 
			
		||||
        pa_log("Failed to parse channels= argument.");
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -297,8 +297,7 @@ int pa__init(pa_module*m) {
 | 
			
		|||
        channels = m->core->default_sample_spec.channels;
 | 
			
		||||
 | 
			
		||||
    if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
 | 
			
		||||
        channels <= 0 ||
 | 
			
		||||
        channels >= PA_CHANNELS_MAX) {
 | 
			
		||||
        !pa_channels_valid(channels)) {
 | 
			
		||||
        pa_log("failed to parse channels= argument.");
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -238,7 +238,7 @@ int pa__init(pa_module *m) {
 | 
			
		|||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (pa_modargs_get_value_u32(ma, "channels", &u->channels) < 0 || u->channels > PA_CHANNELS_MAX) {
 | 
			
		||||
    if (pa_modargs_get_value_u32(ma, "channels", &u->channels) < 0 || !pa_channels_valid(u->channels)) {
 | 
			
		||||
        pa_log("Failed to parse channels= argument.");
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -199,8 +199,7 @@ pa_channel_map* pa_channel_map_init_stereo(pa_channel_map *m) {
 | 
			
		|||
 | 
			
		||||
pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, pa_channel_map_def_t def) {
 | 
			
		||||
    pa_assert(m);
 | 
			
		||||
    pa_assert(channels > 0);
 | 
			
		||||
    pa_assert(channels <= PA_CHANNELS_MAX);
 | 
			
		||||
    pa_assert(pa_channels_valid(channels));
 | 
			
		||||
    pa_assert(def < PA_CHANNEL_MAP_DEF_MAX);
 | 
			
		||||
 | 
			
		||||
    pa_channel_map_init(m);
 | 
			
		||||
| 
						 | 
				
			
			@ -401,8 +400,7 @@ pa_channel_map* pa_channel_map_init_extend(pa_channel_map *m, unsigned channels,
 | 
			
		|||
    unsigned c;
 | 
			
		||||
 | 
			
		||||
    pa_assert(m);
 | 
			
		||||
    pa_assert(channels > 0);
 | 
			
		||||
    pa_assert(channels <= PA_CHANNELS_MAX);
 | 
			
		||||
    pa_assert(pa_channels_valid(channels));
 | 
			
		||||
    pa_assert(def < PA_CHANNEL_MAP_DEF_MAX);
 | 
			
		||||
 | 
			
		||||
    pa_channel_map_init(m);
 | 
			
		||||
| 
						 | 
				
			
			@ -617,7 +615,7 @@ int pa_channel_map_valid(const pa_channel_map *map) {
 | 
			
		|||
 | 
			
		||||
    pa_assert(map);
 | 
			
		||||
 | 
			
		||||
    if (map->channels <= 0 || map->channels > PA_CHANNELS_MAX)
 | 
			
		||||
    if (!pa_channels_valid(map->channels))
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    for (c = 0; c < map->channels; c++)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -111,12 +111,15 @@ int pa_sample_rate_valid(uint32_t rate) {
 | 
			
		|||
    return rate > 0 && rate <= PA_RATE_MAX;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int pa_channels_valid(uint8_t channels) {
 | 
			
		||||
    return channels > 0 && channels <= PA_CHANNELS_MAX;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int pa_sample_spec_valid(const pa_sample_spec *spec) {
 | 
			
		||||
    pa_assert(spec);
 | 
			
		||||
 | 
			
		||||
    if (PA_UNLIKELY(!pa_sample_rate_valid(spec->rate) ||
 | 
			
		||||
        spec->channels <= 0 ||
 | 
			
		||||
        spec->channels > PA_CHANNELS_MAX ||
 | 
			
		||||
        !pa_channels_valid(spec->channels) ||
 | 
			
		||||
        !pa_sample_format_valid(spec->format)))
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -295,6 +295,10 @@ int pa_sample_format_valid(unsigned format) PA_GCC_PURE;
 | 
			
		|||
/** Return non-zero if the rate is within the supported range. \since 5.0 */
 | 
			
		||||
int pa_sample_rate_valid(uint32_t rate) PA_GCC_PURE;
 | 
			
		||||
 | 
			
		||||
/** Return non-zero if the channel count is within the supported range.
 | 
			
		||||
 * \since 5.0 */
 | 
			
		||||
int pa_channels_valid(uint8_t channels) PA_GCC_PURE;
 | 
			
		||||
 | 
			
		||||
/** Return non-zero when the sample type specification is valid */
 | 
			
		||||
int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -73,8 +73,7 @@ pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
 | 
			
		|||
    int i;
 | 
			
		||||
 | 
			
		||||
    pa_assert(a);
 | 
			
		||||
    pa_assert(channels > 0);
 | 
			
		||||
    pa_assert(channels <= PA_CHANNELS_MAX);
 | 
			
		||||
    pa_assert(pa_channels_valid(channels));
 | 
			
		||||
 | 
			
		||||
    a->channels = (uint8_t) channels;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -533,7 +532,7 @@ int pa_cvolume_valid(const pa_cvolume *v) {
 | 
			
		|||
 | 
			
		||||
    pa_assert(v);
 | 
			
		||||
 | 
			
		||||
    if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
 | 
			
		||||
    if (!pa_channels_valid(v->channels))
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    for (c = 0; c < v->channels; c++)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -392,8 +392,7 @@ int pa_modargs_get_sample_spec(pa_modargs *ma, pa_sample_spec *rss) {
 | 
			
		|||
 | 
			
		||||
    channels = ss.channels;
 | 
			
		||||
    if ((pa_modargs_get_value_u32(ma, "channels", &channels)) < 0 ||
 | 
			
		||||
        channels <= 0 ||
 | 
			
		||||
        channels >= PA_CHANNELS_MAX)
 | 
			
		||||
        !pa_channels_valid(channels))
 | 
			
		||||
        return -1;
 | 
			
		||||
    ss.channels = (uint8_t) channels;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue