[pulseaudio-discuss] [PATCH 3/3] Improve pa_replace() behaviour.

Ulrich Eckhardt ulrich.eckhardt at base-42.de
Mon May 16 17:51:05 UTC 2016


 - Assert that the search string isn't empty.
 - Add test.
 - Improve documentation.
---
 src/pulsecore/core-util.c  | 1 +
 src/pulsecore/core-util.h  | 4 ++++
 src/tests/core-util-test.c | 6 ++++++
 3 files changed, 11 insertions(+)

diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index b6eb85a..f816da9 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -3194,6 +3194,7 @@ char *pa_replace(const char*s, const char*a, const char *b) {
 
     pa_assert(s);
     pa_assert(a);
+    pa_assert(*a);
     pa_assert(b);
 
     an = strlen(a);
diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h
index d5a2d39..5725ca7 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -249,6 +249,10 @@ void pa_reduce(unsigned *num, unsigned *den);
 
 unsigned pa_ncpus(void);
 
+/* Replaces all occurrences of `a' in `s' with `b'. The caller has to free the
+ * returned string. All parameters must be non-NULL and additionally `a' must
+ * not be a zero-length string.
+ */
 char *pa_replace(const char*s, const char*a, const char *b);
 
 /* Escapes p by inserting backslashes in front of backslashes. chars is a
diff --git a/src/tests/core-util-test.c b/src/tests/core-util-test.c
index e829cd2..51c5349 100644
--- a/src/tests/core-util-test.c
+++ b/src/tests/core-util-test.c
@@ -228,6 +228,11 @@ START_TEST (modargs_test_escape) {
 }
 END_TEST
 
+START_TEST (modargs_test_replace_fail_4) {
+    pa_replace("abe", "", "bab");
+}
+END_TEST
+
 START_TEST (modargs_test_unescape) {
     char* value;
 
@@ -264,6 +269,7 @@ int main(int argc, char *argv[]) {
     tcase_add_test_raise_signal(tc, modargs_test_replace_fail_1, SIGABRT);
     tcase_add_test_raise_signal(tc, modargs_test_replace_fail_2, SIGABRT);
     tcase_add_test_raise_signal(tc, modargs_test_replace_fail_3, SIGABRT);
+    tcase_add_test_raise_signal(tc, modargs_test_replace_fail_4, SIGABRT);
     tcase_add_test(tc, modargs_test_escape);
     tcase_add_test(tc, modargs_test_unescape);
 
-- 
1.9.1



More information about the pulseaudio-discuss mailing list