From e7b2ea7c97714e202829841f7ef6d21171474965 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 1 Feb 2018 18:11:02 +0900 Subject: [PATCH] strv: drop strv_join_quoted() (#8057) The function `strv_join_quoted()` is now not used, and has a bug in the buffer size calculation when the strings needs to escaped, as reported in #8056. So, let's remove the function. Closes #8056. --- src/basic/strv.c | 36 ------------------------------- src/basic/strv.h | 1 - src/test/test-strv.c | 61 +--------------------------------------------------- 3 files changed, 1 insertion(+), 97 deletions(-) diff --git a/src/basic/strv.c b/src/basic/strv.c index 54c701a..1103245 100644 --- a/src/basic/strv.c +++ b/src/basic/strv.c @@ -393,42 +393,6 @@ char *strv_join(char **l, const char *separator) { return r; } -char *strv_join_quoted(char **l) { - char *buf = NULL; - char **s; - size_t allocated = 0, len = 0; - - STRV_FOREACH(s, l) { - /* assuming here that escaped string cannot be more - * than twice as long, and reserving space for the - * separator and quotes. - */ - _cleanup_free_ char *esc = NULL; - size_t needed; - - if (!GREEDY_REALLOC(buf, allocated, - len + strlen(*s) * 2 + 3)) - goto oom; - - esc = cescape(*s); - if (!esc) - goto oom; - - needed = snprintf(buf + len, allocated - len, "%s\"%s\"", - len > 0 ? " " : "", esc); - assert(needed < allocated - len); - len += needed; - } - - if (!buf) - buf = malloc0(1); - - return buf; - - oom: - return mfree(buf); -} - int strv_push(char ***l, char *value) { char **c; unsigned n, m; diff --git a/src/basic/strv.h b/src/basic/strv.h index 0ed6b74..75369d2 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -86,7 +86,6 @@ char **strv_split_newlines(const char *s); int strv_split_extract(char ***t, const char *s, const char *separators, ExtractFlags flags); char *strv_join(char **l, const char *separator); -char *strv_join_quoted(char **l); char **strv_parse_nulstr(const char *s, size_t l); char **strv_split_nulstr(const char *s); diff --git a/src/test/test-strv.c b/src/test/test-strv.c index aec00eb..f78b1bc 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -22,6 +22,7 @@ #include #include "alloc-util.h" +#include "escape.h" #include "specifier.h" #include "string-util.h" #include "strv.h" @@ -102,36 +103,6 @@ static const char* const input_table_one_empty[] = { }; -static const char* const input_table_quotes[] = { - "\"", - "'", - "\"\"", - "\\", - "\\\\", - NULL, -}; -#define QUOTES_STRING \ - "\"\\\"\" " \ - "\"\\\'\" " \ - "\"\\\"\\\"\" " \ - "\"\\\\\" " \ - "\"\\\\\\\\\"" - -static const char * const input_table_spaces[] = { - " ", - "' '", - "\" ", - " \"", - " \\\\ ", - NULL, -}; -#define SPACES_STRING \ - "\" \" " \ - "\"\\' \\'\" " \ - "\"\\\" \" " \ - "\" \\\"\" " \ - "\" \\\\\\\\ \"" - static void test_strv_find(void) { assert_se(strv_find((char **)input_table_multiple, "three")); assert_se(!strv_find((char **)input_table_multiple, "four")); @@ -193,28 +164,6 @@ static void test_strv_join(void) { assert_se(streq(w, "")); } -static void test_strv_quote_unquote(const char* const *split, const char *quoted) { - _cleanup_free_ char *p; - _cleanup_strv_free_ char **s = NULL; - char **t; - int r; - - p = strv_join_quoted((char **)split); - assert_se(p); - printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */ - assert_se(p); - assert_se(streq(p, quoted)); - - r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_QUOTES); - assert_se(r == (int) strv_length(s)); - assert_se(s); - STRV_FOREACH(t, s) { - assert_se(*t); - assert_se(streq(*t, *split)); - split++; - } -} - static void test_strv_unquote(const char *quoted, char **list) { _cleanup_strv_free_ char **s; _cleanup_free_ char *j; @@ -738,14 +687,6 @@ int main(int argc, char *argv[]) { test_strv_find_startswith(); test_strv_join(); - test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\""); - test_strv_quote_unquote(input_table_one, "\"one\""); - test_strv_quote_unquote(input_table_none, ""); - test_strv_quote_unquote(input_table_one_empty, "\"\""); - test_strv_quote_unquote(input_table_two_empties, "\"\" \"\""); - test_strv_quote_unquote(input_table_quotes, QUOTES_STRING); - test_strv_quote_unquote(input_table_spaces, SPACES_STRING); - test_strv_unquote(" foo=bar \"waldo\" zzz ", STRV_MAKE("foo=bar", "waldo", "zzz")); test_strv_unquote("", STRV_MAKE_EMPTY); test_strv_unquote(" ", STRV_MAKE_EMPTY); -- 2.7.4