]> code.delx.au - pulseaudio/commitdiff
Add pa_sample_rate_valid()
authorTanu Kaskinen <tanu.kaskinen@linux.intel.com>
Wed, 4 Dec 2013 07:50:10 +0000 (09:50 +0200)
committerPeter Meerwald <pmeerw@pmeerw.net>
Sun, 15 Dec 2013 10:21:56 +0000 (11:21 +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
sample rate values.

12 files changed:
src/daemon/daemon-conf.c
src/map-file
src/modules/alsa/alsa-ucm.c
src/modules/dbus/iface-core.c
src/pulse/sample.c
src/pulse/sample.h
src/pulse/stream.c
src/pulsecore/modargs.c
src/pulsecore/protocol-native.c
src/pulsecore/resampler.c
src/pulsecore/sink.c
src/pulsecore/source.c

index 5a480cc00697254115db3bd7cb0036cf1ef517ec..de3bdc455ad9b2a87c9bde063ac1dda0160c1cb4 100644 (file)
@@ -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;
index 45f5e410d5af46662b969836f70555aac47f922a..071c141e162a85ef300f463e4448becbdfdef871 100644 (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;
index ac7f70d984e702845042440a704b0796ea51d3ae..0ed2470dc14926e41538609135bf3bd7e3323849 100644 (file)
@@ -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
index 9c293fd8c1cebe0e225729191632e694eb552406..c7853ccab55cfba7f5c794a87d411aff744437bc 100644 (file)
@@ -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;
     }
index f35038081f352614d48a1c1e1c61422cd94609c4..70f7f724301038d720461a1d46108ed702a82ff3 100644 (file)
@@ -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)))
index 23c7d738a63e691bee850031c1162b412d0a79fe..b8abc1caa10dd91f88909943b676cfd09fb7fbbd 100644 (file)
@@ -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;
 
index 746179b44ef0e0297180d7a855f17044736a2018..d3763260839c93776537f4d7408cb466cb2f57ad 100644 (file)
@@ -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);
index 51e55018de3fcea2ff2af2983051e5e5dc9ce848..a02a3e7a6388006ae2133577db4c6666d64c5176 100644 (file)
@@ -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)))
index c1ff04acd28a91d0d6e57b520f08a1e6e30bf6d7..662de9802cfdfb6f37bf69808b950ec5787262f0 100644 (file)
@@ -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;
index 6dfee1c02e5fb4cda9e6938c68b2caabc51857e0..f0c663fb69facf74353f14ca2ecf0b731f564a74 100644 (file)
@@ -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);
 
index 95cf9b677eafb84997cdd2467ea5e0f9d0e78028..672a884fd3c755c2efa73433bcc0cc45223420ea 100644 (file)
@@ -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) {
index a6dc9bfe9a24e6a5801f50647d7ba9fcd4ae988a..6073b1d9f37e018f28a33a70e6a5c6b4d2a18c2b 100644 (file)
@@ -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) {