Add pa_channels_valid()
authorTanu Kaskinen <tanu.kaskinen@linux.intel.com>
Wed, 4 Dec 2013 07:50:11 +0000 (09:50 +0200)
committerPeter Meerwald <pmeerw@pmeerw.net>
Sun, 15 Dec 2013 10:44:35 +0000 (11:44 +0100)
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.

12 files changed:
src/daemon/daemon-conf.c
src/map-file
src/modules/alsa/alsa-ucm.c
src/modules/bluetooth/module-bluez4-device.c
src/modules/jack/module-jack-sink.c
src/modules/jack/module-jack-source.c
src/modules/jack/module-jackdbus-detect.c
src/pulse/channelmap.c
src/pulse/sample.c
src/pulse/sample.h
src/pulse/volume.c
src/pulsecore/modargs.c

index de3bdc4..ce777a6 100644 (file)
@@ -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;
     }
index 071c141..fbad1a4 100644 (file)
@@ -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;
index 0ed2470..c88fc77 100644 (file)
@@ -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);
     }
 
index f419cb9..83e603f 100644 (file)
@@ -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;
     }
index dccf032..ab97758 100644 (file)
@@ -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;
     }
index 8f550e1..5d9668b 100644 (file)
@@ -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;
     }
index 5c29772..160794b 100644 (file)
@@ -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;
     }
index fec0623..72e4130 100644 (file)
@@ -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++)
index 70f7f72..82c1b01 100644 (file)
@@ -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;
 
index b8abc1c..86973d4 100644 (file)
@@ -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;
 
index fd67254..6927392 100644 (file)
@@ -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++)
index a02a3e7..a9c4e10 100644 (file)
@@ -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;