]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add str_append_max()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Sat, 21 Apr 2018 13:11:48 +0000 (16:11 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 30 Aug 2018 08:13:38 +0000 (11:13 +0300)
It's otherwise the same as str_append_n(), except it takes const char* as
input instead of const void*. This, as well as its name, should make it
less likely to be used wrong when the input may legitimately have NULs.

The unit test is changed from str_append_n() to str_append_max(), because
str_append_n() will be deprecated.

src/lib/str.c
src/lib/str.h
src/lib/test-str.c

index af2c24807b4d2c87c36fe9beb6b116b06e96c17c..0f6d3b84888de3c58a9d9d539f67328627dfab59 100644 (file)
@@ -84,7 +84,7 @@ bool str_equals(const string_t *str1, const string_t *str2)
        return memcmp(str1->data, str2->data, str1->used) == 0;
 }
 
-void str_append_n(string_t *str, const void *cstr, size_t max_len)
+void str_append_max(string_t *str, const char *cstr, size_t max_len)
 {
        const char *p;
        size_t len;
index cf7f296a573ac9ea7d4facd2f1fba3850edfc1a9..16edf4c6808693348535e3537f8b23e49a2a8c5b 100644 (file)
@@ -28,7 +28,12 @@ static inline size_t str_len(const string_t *str)
 
 /* Append NUL-terminated string. If the trailing NUL isn't found earlier,
    append a maximum of max_len characters. */
-void str_append_n(string_t *str, const void *cstr, size_t max_len);
+void str_append_max(string_t *str, const char *cstr, size_t max_len);
+static inline void
+str_append_n(string_t *str, const void *cstr, size_t max_len)
+{
+       str_append_max(str, cstr, max_len);
+}
 
 static inline void str_append(string_t *str, const char *cstr)
 {
index c5bd53fe06924ab09baf9beb12126bc0739d612b..79ce5fbc454b89bd741d90267b185aa138559710 100644 (file)
@@ -84,26 +84,26 @@ static void test_str_delete(void)
        test_end();
 }
 
-static void test_str_append_n(void)
+static void test_str_append_max(void)
 {
        string_t *str = t_str_new(32);
 
-       test_begin("str_append_n()");
-       str_append_n(str, "foo", 0);
+       test_begin("str_append_max()");
+       str_append_max(str, "foo", 0);
        test_assert(str->used == 0);
 
-       str_append_n(str, "\0foo", 4);
+       str_append_max(str, "\0foo", 4);
        test_assert(str->used == 0);
 
-       str_append_n(str, "foo", 3);
+       str_append_max(str, "foo", 3);
        test_assert(str->used == 3 && memcmp(str_data(str), "foo", 3) == 0);
        str_truncate(str, 0);
 
-       str_append_n(str, "foo", 2);
+       str_append_max(str, "foo", 2);
        test_assert(str->used == 2 && memcmp(str_data(str), "fo", 2) == 0);
        str_truncate(str, 0);
 
-       str_append_n(str, "foo\0bar", 7);
+       str_append_max(str, "foo\0bar", 7);
        test_assert(str->used == 3 && memcmp(str_data(str), "foo", 3) == 0);
        str_truncate(str, 0);
        test_end();
@@ -133,6 +133,6 @@ void test_str(void)
        test_str_c();
        test_str_insert();
        test_str_delete();
-       test_str_append_n();
+       test_str_append_max();
        test_str_truncate();
 }