From: Timo Sirainen Date: Sat, 21 Apr 2018 13:11:48 +0000 (+0300) Subject: lib: Add str_append_max() X-Git-Tag: 2.3.9~1966 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=495c324e4d361d52ac869306cc8c060b60fb317b;p=thirdparty%2Fdovecot%2Fcore.git lib: Add str_append_max() 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. --- diff --git a/src/lib/str.c b/src/lib/str.c index af2c24807b..0f6d3b8488 100644 --- a/src/lib/str.c +++ b/src/lib/str.c @@ -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; diff --git a/src/lib/str.h b/src/lib/str.h index cf7f296a57..16edf4c680 100644 --- a/src/lib/str.h +++ b/src/lib/str.h @@ -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) { diff --git a/src/lib/test-str.c b/src/lib/test-str.c index c5bd53fe06..79ce5fbc45 100644 --- a/src/lib/test-str.c +++ b/src/lib/test-str.c @@ -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(); }