[pulseaudio-commits] 3 commits - src/daemon src/map-file src/modules src/pulse src/pulsecore

Peter Meerwald pmeerw at kemper.freedesktop.org
Sun Dec 15 03:29:08 PST 2013


 src/daemon/daemon-conf.c                     |    6 ++---
 src/map-file                                 |    3 ++
 src/modules/alsa/alsa-ucm.c                  |   10 ++++----
 src/modules/bluetooth/module-bluez4-device.c |    2 -
 src/modules/dbus/iface-core.c                |   10 ++++----
 src/modules/jack/module-jack-sink.c          |    3 --
 src/modules/jack/module-jack-source.c        |    3 --
 src/modules/jack/module-jackdbus-detect.c    |    2 -
 src/pulse/channelmap.c                       |    8 ++----
 src/pulse/sample.c                           |   29 +++++++++++++++---------
 src/pulse/sample.h                           |   10 ++++++++
 src/pulse/stream.c                           |    2 -
 src/pulse/volume.c                           |    5 +---
 src/pulsecore/mix.c                          |    6 +----
 src/pulsecore/modargs.c                      |    9 ++-----
 src/pulsecore/protocol-native.c              |    2 -
 src/pulsecore/resampler.c                    |   12 +++++-----
 src/pulsecore/sconv.c                        |   32 ++++++---------------------
 src/pulsecore/sink.c                         |    3 --
 src/pulsecore/source.c                       |    3 --
 src/pulsecore/svolume_c.c                    |    6 +----
 21 files changed, 78 insertions(+), 88 deletions(-)

New commits:
commit 2747c961015ba00ec9a1cad8a8a95b4a34db9ee0
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Wed Dec 4 09:50:11 2013 +0200

    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.

diff --git a/src/daemon/daemon-conf.c b/src/daemon/daemon-conf.c
index de3bdc4..ce777a6 100644
--- a/src/daemon/daemon-conf.c
+++ b/src/daemon/daemon-conf.c
@@ -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;
     }
diff --git a/src/map-file b/src/map-file
index 071c141..fbad1a4 100644
--- a/src/map-file
+++ b/src/map-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;
diff --git a/src/modules/alsa/alsa-ucm.c b/src/modules/alsa/alsa-ucm.c
index 0ed2470..c88fc77 100644
--- a/src/modules/alsa/alsa-ucm.c
+++ b/src/modules/alsa/alsa-ucm.c
@@ -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);
     }
 
diff --git a/src/modules/bluetooth/module-bluez4-device.c b/src/modules/bluetooth/module-bluez4-device.c
index f419cb9..83e603f 100644
--- a/src/modules/bluetooth/module-bluez4-device.c
+++ b/src/modules/bluetooth/module-bluez4-device.c
@@ -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;
     }
diff --git a/src/modules/jack/module-jack-sink.c b/src/modules/jack/module-jack-sink.c
index dccf032..ab97758 100644
--- a/src/modules/jack/module-jack-sink.c
+++ b/src/modules/jack/module-jack-sink.c
@@ -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;
     }
diff --git a/src/modules/jack/module-jack-source.c b/src/modules/jack/module-jack-source.c
index 8f550e1..5d9668b 100644
--- a/src/modules/jack/module-jack-source.c
+++ b/src/modules/jack/module-jack-source.c
@@ -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;
     }
diff --git a/src/modules/jack/module-jackdbus-detect.c b/src/modules/jack/module-jackdbus-detect.c
index 5c29772..160794b 100644
--- a/src/modules/jack/module-jackdbus-detect.c
+++ b/src/modules/jack/module-jackdbus-detect.c
@@ -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;
     }
diff --git a/src/pulse/channelmap.c b/src/pulse/channelmap.c
index fec0623..72e4130 100644
--- a/src/pulse/channelmap.c
+++ b/src/pulse/channelmap.c
@@ -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++)
diff --git a/src/pulse/sample.c b/src/pulse/sample.c
index 70f7f72..82c1b01 100644
--- a/src/pulse/sample.c
+++ b/src/pulse/sample.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;
 
diff --git a/src/pulse/sample.h b/src/pulse/sample.h
index b8abc1c..86973d4 100644
--- a/src/pulse/sample.h
+++ b/src/pulse/sample.h
@@ -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;
 
diff --git a/src/pulse/volume.c b/src/pulse/volume.c
index fd67254..6927392 100644
--- a/src/pulse/volume.c
+++ b/src/pulse/volume.c
@@ -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++)
diff --git a/src/pulsecore/modargs.c b/src/pulsecore/modargs.c
index a02a3e7..a9c4e10 100644
--- a/src/pulsecore/modargs.c
+++ b/src/pulsecore/modargs.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;
 

commit a67318f8affc4973507811946708bc17e63f1ec7
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Wed Dec 4 09:50:10 2013 +0200

    Add pa_sample_rate_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
    sample rate values.

diff --git a/src/daemon/daemon-conf.c b/src/daemon/daemon-conf.c
index 5a480cc..de3bdc4 100644
--- a/src/daemon/daemon-conf.c
+++ b/src/daemon/daemon-conf.c
@@ -345,7 +345,7 @@ static int parse_sample_rate(pa_config_parser_state *state) {
 
     c = state->data;
 
-    if (pa_atou(state->rvalue, &r) < 0 || r > (uint32_t) PA_RATE_MAX || r <= 0 ||
+    if (pa_atou(state->rvalue, &r) < 0 || !pa_sample_rate_valid(r) ||
         !((r % 4000 == 0) || (r % 11025 == 0))) {
         pa_log(_("[%s:%u] Invalid sample rate '%s'."), state->filename, state->lineno, state->rvalue);
         return -1;
@@ -363,7 +363,7 @@ static int parse_alternate_sample_rate(pa_config_parser_state *state) {
 
     c = state->data;
 
-    if (pa_atou(state->rvalue, &r) < 0 || r > (uint32_t) PA_RATE_MAX || r <= 0 ||
+    if (pa_atou(state->rvalue, &r) < 0 || !pa_sample_rate_valid(r) ||
         !((r % 4000==0) || (r % 11025 == 0))) {
         pa_log(_("[%s:%u] Invalid sample rate '%s'."), state->filename, state->lineno, state->rvalue);
         return -1;
diff --git a/src/map-file b/src/map-file
index 45f5e41..071c141 100644
--- a/src/map-file
+++ b/src/map-file
@@ -252,6 +252,7 @@ pa_sample_format_is_be;
 pa_sample_format_is_le;
 pa_sample_format_to_string;
 pa_sample_format_valid;
+pa_sample_rate_valid;
 pa_sample_size;
 pa_sample_size_of_format;
 pa_sample_spec_equal;
diff --git a/src/modules/alsa/alsa-ucm.c b/src/modules/alsa/alsa-ucm.c
index ac7f70d..0ed2470 100644
--- a/src/modules/alsa/alsa-ucm.c
+++ b/src/modules/alsa/alsa-ucm.c
@@ -263,7 +263,7 @@ static int ucm_get_device_property(
         /* get rate */
         if ((value = pa_proplist_gets(device->proplist, PA_ALSA_PROP_UCM_PLAYBACK_RATE)) ||
             (value = pa_proplist_gets(verb->proplist, PA_ALSA_PROP_UCM_PLAYBACK_RATE))) {
-            if (pa_atou(value, &ui) == 0 && ui > 0 && ui < PA_RATE_MAX) {
+            if (pa_atou(value, &ui) == 0 && pa_sample_rate_valid(ui)) {
                 pa_log_debug("UCM playback device %s rate %d", device_name, ui);
                 device->playback_rate = ui;
             } else
@@ -284,7 +284,7 @@ static int ucm_get_device_property(
         /* get rate */
         if ((value = pa_proplist_gets(device->proplist, PA_ALSA_PROP_UCM_CAPTURE_RATE)) ||
             (value = pa_proplist_gets(verb->proplist, PA_ALSA_PROP_UCM_CAPTURE_RATE))) {
-            if (pa_atou(value, &ui) == 0 && ui > 0 && ui < PA_RATE_MAX) {
+            if (pa_atou(value, &ui) == 0 && pa_sample_rate_valid(ui)) {
                 pa_log_debug("UCM capture device %s rate %d", device_name, ui);
                 device->capture_rate = ui;
             } else
diff --git a/src/modules/dbus/iface-core.c b/src/modules/dbus/iface-core.c
index 9c293fd..c7853cc 100644
--- a/src/modules/dbus/iface-core.c
+++ b/src/modules/dbus/iface-core.c
@@ -544,7 +544,7 @@ static void handle_set_default_sample_rate(DBusConnection *conn, DBusMessage *ms
 
     dbus_message_iter_get_basic(iter, &default_sample_rate);
 
-    if (default_sample_rate <= 0 || default_sample_rate > PA_RATE_MAX ||
+    if (!pa_sample_rate_valid(default_sample_rate) ||
         !((default_sample_rate % 4000 == 0) || (default_sample_rate % 11025 == 0)))  {
         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample rate.");
         return;
@@ -579,7 +579,7 @@ static void handle_set_alternate_sample_rate(DBusConnection *conn, DBusMessage *
 
     dbus_message_iter_get_basic(iter, &alternate_sample_rate);
 
-    if (alternate_sample_rate <= 0 || alternate_sample_rate > PA_RATE_MAX ||
+    if (!pa_sample_rate_valid(alternate_sample_rate) ||
         !((alternate_sample_rate % 4000 == 0) || (alternate_sample_rate % 11025 == 0))) {
         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample rate.");
         return;
@@ -1322,7 +1322,7 @@ static void handle_upload_sample(DBusConnection *conn, DBusMessage *msg, void *u
         goto finish;
     }
 
-    if (sample_rate <= 0 || sample_rate > PA_RATE_MAX) {
+    if (!pa_sample_rate_valid(sample_rate)) {
         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample rate.");
         goto finish;
     }
diff --git a/src/pulse/sample.c b/src/pulse/sample.c
index f350380..70f7f72 100644
--- a/src/pulse/sample.c
+++ b/src/pulse/sample.c
@@ -107,11 +107,14 @@ int pa_sample_format_valid(unsigned format) {
     return format < PA_SAMPLE_MAX;
 }
 
+int pa_sample_rate_valid(uint32_t rate) {
+    return rate > 0 && rate <= PA_RATE_MAX;
+}
+
 int pa_sample_spec_valid(const pa_sample_spec *spec) {
     pa_assert(spec);
 
-    if (PA_UNLIKELY (spec->rate <= 0 ||
-        spec->rate > PA_RATE_MAX ||
+    if (PA_UNLIKELY(!pa_sample_rate_valid(spec->rate) ||
         spec->channels <= 0 ||
         spec->channels > PA_CHANNELS_MAX ||
         !pa_sample_format_valid(spec->format)))
diff --git a/src/pulse/sample.h b/src/pulse/sample.h
index 23c7d73..b8abc1c 100644
--- a/src/pulse/sample.h
+++ b/src/pulse/sample.h
@@ -292,6 +292,9 @@ pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec);
 /** Return non-zero if the given integer is a valid sample format. \since 5.0 */
 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 when the sample type specification is valid */
 int pa_sample_spec_valid(const pa_sample_spec *spec) PA_GCC_PURE;
 
diff --git a/src/pulse/stream.c b/src/pulse/stream.c
index 746179b..d376326 100644
--- a/src/pulse/stream.c
+++ b/src/pulse/stream.c
@@ -2785,7 +2785,7 @@ pa_operation *pa_stream_update_sample_rate(pa_stream *s, uint32_t rate, pa_strea
     pa_assert(PA_REFCNT_VALUE(s) >= 1);
 
     PA_CHECK_VALIDITY_RETURN_NULL(s->context, !pa_detect_fork(), PA_ERR_FORKED);
-    PA_CHECK_VALIDITY_RETURN_NULL(s->context, rate > 0 && rate <= PA_RATE_MAX, PA_ERR_INVALID);
+    PA_CHECK_VALIDITY_RETURN_NULL(s->context, pa_sample_rate_valid(rate), PA_ERR_INVALID);
     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->state == PA_STREAM_READY, PA_ERR_BADSTATE);
     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->direction != PA_STREAM_UPLOAD, PA_ERR_BADSTATE);
     PA_CHECK_VALIDITY_RETURN_NULL(s->context, s->flags & PA_STREAM_VARIABLE_RATE, PA_ERR_BADSTATE);
diff --git a/src/pulsecore/modargs.c b/src/pulsecore/modargs.c
index 51e5501..a02a3e7 100644
--- a/src/pulsecore/modargs.c
+++ b/src/pulsecore/modargs.c
@@ -371,8 +371,7 @@ int pa_modargs_get_sample_rate(pa_modargs *ma, uint32_t *rate) {
 
     rate_local = *rate;
     if ((pa_modargs_get_value_u32(ma, "rate", &rate_local)) < 0 ||
-        rate_local <= 0 ||
-        rate_local > PA_RATE_MAX)
+        !pa_sample_rate_valid(rate_local))
         return -1;
 
     *rate = rate_local;
@@ -417,8 +416,7 @@ int pa_modargs_get_alternate_sample_rate(pa_modargs *ma, uint32_t *alternate_rat
 
     rate_local = *alternate_rate;
     if ((pa_modargs_get_value_u32(ma, "alternate_rate", &rate_local)) < 0 ||
-        rate_local <= 0 ||
-        rate_local > PA_RATE_MAX)
+        !pa_sample_rate_valid(*alternate_rate))
         return -1;
 
     if (!((rate_local % 4000 == 0) || (rate_local % 11025 == 0)))
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index c1ff04a..662de98 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -4112,7 +4112,7 @@ static void command_update_stream_sample_rate(pa_pdispatch *pd, uint32_t command
     }
 
     CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS);
-    CHECK_VALIDITY(c->pstream, rate > 0 && rate <= PA_RATE_MAX, tag, PA_ERR_INVALID);
+    CHECK_VALIDITY(c->pstream, pa_sample_rate_valid(rate), tag, PA_ERR_INVALID);
 
     if (command == PA_COMMAND_UPDATE_PLAYBACK_STREAM_SAMPLE_RATE) {
         playback_stream *s;
diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 6dfee1c..f0c663f 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -202,8 +202,8 @@ static pa_resample_method_t pa_resampler_fix_method(
                 const uint32_t rate_a,
                 const uint32_t rate_b) {
 
-    pa_assert(rate_a > 0 && rate_a <= PA_RATE_MAX);
-    pa_assert(rate_b > 0 && rate_b <= PA_RATE_MAX);
+    pa_assert(pa_sample_rate_valid(rate_a));
+    pa_assert(pa_sample_rate_valid(rate_b));
     pa_assert(method >= 0);
     pa_assert(method < PA_RESAMPLER_MAX);
 
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c
index 95cf9b6..672a884 100644
--- a/src/pulsecore/sink.c
+++ b/src/pulsecore/sink.c
@@ -1420,8 +1420,7 @@ int pa_sink_update_rate(pa_sink *s, uint32_t rate, bool passthrough) {
         }
     }
 
-    if (PA_UNLIKELY (desired_rate < 8000 ||
-                     desired_rate > PA_RATE_MAX))
+    if (PA_UNLIKELY(!pa_sample_rate_valid(desired_rate)))
         return -1;
 
     if (!passthrough) {
diff --git a/src/pulsecore/source.c b/src/pulsecore/source.c
index a6dc9bf..6073b1d 100644
--- a/src/pulsecore/source.c
+++ b/src/pulsecore/source.c
@@ -1009,8 +1009,7 @@ int pa_source_update_rate(pa_source *s, uint32_t rate, bool passthrough) {
         }
     }
 
-    if (PA_UNLIKELY (desired_rate < 8000 ||
-                     desired_rate > PA_RATE_MAX))
+    if (PA_UNLIKELY(!pa_sample_rate_valid(desired_rate)))
         return -1;
 
     if (!passthrough) {

commit 1cda71725240bd4911f0f34c5d384b3966f06369
Author: Tanu Kaskinen <tanu.kaskinen at linux.intel.com>
Date:   Wed Dec 4 09:50:09 2013 +0200

    Add pa_sample_format_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
    the sample format value.

diff --git a/src/map-file b/src/map-file
index 39d5a01..45f5e41 100644
--- a/src/map-file
+++ b/src/map-file
@@ -251,6 +251,7 @@ pa_rtclock_now;
 pa_sample_format_is_be;
 pa_sample_format_is_le;
 pa_sample_format_to_string;
+pa_sample_format_valid;
 pa_sample_size;
 pa_sample_size_of_format;
 pa_sample_spec_equal;
diff --git a/src/modules/dbus/iface-core.c b/src/modules/dbus/iface-core.c
index 22dae32..9c293fd 100644
--- a/src/modules/dbus/iface-core.c
+++ b/src/modules/dbus/iface-core.c
@@ -510,7 +510,7 @@ static void handle_set_default_sample_format(DBusConnection *conn, DBusMessage *
 
     dbus_message_iter_get_basic(iter, &default_sample_format);
 
-    if (default_sample_format >= PA_SAMPLE_MAX) {
+    if (!pa_sample_format_valid(default_sample_format)) {
         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
         return;
     }
@@ -1317,7 +1317,7 @@ static void handle_upload_sample(DBusConnection *conn, DBusMessage *msg, void *u
     dbus_message_iter_recurse(&msg_iter, &array_iter);
     dbus_message_iter_get_fixed_array(&array_iter, &data, &data_length);
 
-    if (sample_format >= PA_SAMPLE_MAX) {
+    if (!pa_sample_format_valid(sample_format)) {
         pa_dbus_send_error(conn, msg, DBUS_ERROR_INVALID_ARGS, "Invalid sample format.");
         goto finish;
     }
diff --git a/src/pulse/sample.c b/src/pulse/sample.c
index b613612..f350380 100644
--- a/src/pulse/sample.c
+++ b/src/pulse/sample.c
@@ -52,8 +52,7 @@ static const size_t size_table[] = {
 };
 
 size_t pa_sample_size_of_format(pa_sample_format_t f) {
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     return size_table[f];
 }
@@ -104,6 +103,10 @@ pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec) {
     return spec;
 }
 
+int pa_sample_format_valid(unsigned format) {
+    return format < PA_SAMPLE_MAX;
+}
+
 int pa_sample_spec_valid(const pa_sample_spec *spec) {
     pa_assert(spec);
 
@@ -111,8 +114,7 @@ int pa_sample_spec_valid(const pa_sample_spec *spec) {
         spec->rate > PA_RATE_MAX ||
         spec->channels <= 0 ||
         spec->channels > PA_CHANNELS_MAX ||
-        spec->format >= PA_SAMPLE_MAX ||
-        spec->format < 0))
+        !pa_sample_format_valid(spec->format)))
         return 0;
 
     return 1;
@@ -152,7 +154,7 @@ const char *pa_sample_format_to_string(pa_sample_format_t f) {
         [PA_SAMPLE_S24_32BE] = "s24-32be",
     };
 
-    if (f < 0 || f >= PA_SAMPLE_MAX)
+    if (!pa_sample_format_valid(f))
         return NULL;
 
     return table[f];
@@ -245,8 +247,7 @@ pa_sample_format_t pa_parse_sample_format(const char *format) {
 }
 
 int pa_sample_format_is_le(pa_sample_format_t f) {
-    pa_assert(f >= PA_SAMPLE_U8);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     switch (f) {
         case PA_SAMPLE_S16LE:
diff --git a/src/pulse/sample.h b/src/pulse/sample.h
index 34087e0..23c7d73 100644
--- a/src/pulse/sample.h
+++ b/src/pulse/sample.h
@@ -289,6 +289,9 @@ size_t pa_usec_to_bytes(pa_usec_t t, const pa_sample_spec *spec) PA_GCC_PURE;
  * pa_sample_spec_valid() will fail for it. \since 0.9.13 */
 pa_sample_spec* pa_sample_spec_init(pa_sample_spec *spec);
 
+/** Return non-zero if the given integer is a valid sample format. \since 5.0 */
+int pa_sample_format_valid(unsigned format) 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;
 
diff --git a/src/pulsecore/mix.c b/src/pulsecore/mix.c
index 4520d06..4b789a6 100644
--- a/src/pulsecore/mix.c
+++ b/src/pulsecore/mix.c
@@ -650,15 +650,13 @@ size_t pa_mix(
 }
 
 pa_do_mix_func_t pa_get_mix_func(pa_sample_format_t f) {
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     return do_mix_table[f];
 }
 
 void pa_set_mix_func(pa_sample_format_t f, pa_do_mix_func_t func) {
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     do_mix_table[f] = func;
 }
diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index b475571..6dfee1c 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -253,8 +253,8 @@ static pa_resample_method_t pa_resampler_fix_method(
 
 /* Return true if a is a more precise sample format than b, else return false */
 static bool sample_format_more_precise(pa_sample_format_t a, pa_sample_format_t b) {
-    pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
-    pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(a));
+    pa_assert(pa_sample_format_valid(b));
 
     switch (a) {
         case PA_SAMPLE_U8:
@@ -305,8 +305,8 @@ static pa_sample_format_t pa_resampler_choose_work_format(
                     bool map_required) {
     pa_sample_format_t work_format;
 
-    pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
-    pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(a));
+    pa_assert(pa_sample_format_valid(b));
     pa_assert(method >= 0);
     pa_assert(method < PA_RESAMPLER_MAX);
 
diff --git a/src/pulsecore/sconv.c b/src/pulsecore/sconv.c
index f0f154f..7e4f6bf 100644
--- a/src/pulsecore/sconv.c
+++ b/src/pulsecore/sconv.c
@@ -202,17 +202,13 @@ static pa_convert_func_t to_float32ne_table[] = {
 };
 
 pa_convert_func_t pa_get_convert_to_float32ne_function(pa_sample_format_t f) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     return to_float32ne_table[f];
 }
 
 void pa_set_convert_to_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     to_float32ne_table[f] = func;
 }
@@ -234,17 +230,13 @@ static pa_convert_func_t from_float32ne_table[] = {
 };
 
 pa_convert_func_t pa_get_convert_from_float32ne_function(pa_sample_format_t f) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     return from_float32ne_table[f];
 }
 
 void pa_set_convert_from_float32ne_function(pa_sample_format_t f, pa_convert_func_t func) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     from_float32ne_table[f] = func;
 }
@@ -266,17 +258,13 @@ static pa_convert_func_t to_s16ne_table[] = {
 };
 
 pa_convert_func_t pa_get_convert_to_s16ne_function(pa_sample_format_t f) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     return to_s16ne_table[f];
 }
 
 void pa_set_convert_to_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     to_s16ne_table[f] = func;
 }
@@ -298,17 +286,13 @@ static pa_convert_func_t from_s16ne_table[] = {
 };
 
 pa_convert_func_t pa_get_convert_from_s16ne_function(pa_sample_format_t f) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     return from_s16ne_table[f];
 }
 
 void pa_set_convert_from_s16ne_function(pa_sample_format_t f, pa_convert_func_t func) {
-
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     from_s16ne_table[f] = func;
 }
diff --git a/src/pulsecore/svolume_c.c b/src/pulsecore/svolume_c.c
index 43b953c..eb04973 100644
--- a/src/pulsecore/svolume_c.c
+++ b/src/pulsecore/svolume_c.c
@@ -261,15 +261,13 @@ static pa_do_volume_func_t do_volume_table[] = {
 };
 
 pa_do_volume_func_t pa_get_volume_func(pa_sample_format_t f) {
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     return do_volume_table[f];
 }
 
 void pa_set_volume_func(pa_sample_format_t f, pa_do_volume_func_t func) {
-    pa_assert(f >= 0);
-    pa_assert(f < PA_SAMPLE_MAX);
+    pa_assert(pa_sample_format_valid(f));
 
     do_volume_table[f] = func;
 }



More information about the pulseaudio-commits mailing list