From 9e4e4121b8b441d3b5b56edece0a05bb16caac3c Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Sat, 6 Feb 2016 13:56:37 +0100 Subject: unit test for dynstr_append_os_quoted() and a trivial cleanup to avoid manually-specified lengths --- mysys/string.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'mysys/string.c') diff --git a/mysys/string.c b/mysys/string.c index d9791341c60..06fd2c3d014 100644 --- a/mysys/string.c +++ b/mysys/string.c @@ -142,16 +142,14 @@ my_bool dynstr_trunc(DYNAMIC_STRING *str, size_t n) my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...) { #ifdef __WIN__ - const char *quote_str= "\""; - const uint quote_len= 1; + LEX_CSTRING quote= { C_STRING_WITH_LEN("\"") }; #else - const char *quote_str= "\'"; - const uint quote_len= 1; + LEX_CSTRING quote= { C_STRING_WITH_LEN("\'") }; #endif /* __WIN__ */ my_bool ret= TRUE; va_list dirty_text; - ret&= dynstr_append_mem(str, quote_str, quote_len); /* Leading quote */ + ret&= dynstr_append_mem(str, quote.str, quote.length); /* Leading quote */ va_start(dirty_text, append); while (append != NullS) { @@ -159,18 +157,18 @@ my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...) const char *next_pos= cur_pos; /* Search for quote in each string and replace with escaped quote */ - while(*(next_pos= strcend(cur_pos, quote_str[0])) != '\0') + while(*(next_pos= strcend(cur_pos, quote.str[0])) != '\0') { ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos)); - ret&= dynstr_append_mem(str ,"\\", 1); - ret&= dynstr_append_mem(str, quote_str, quote_len); + ret&= dynstr_append_mem(str, STRING_WITH_LEN("\\")); + ret&= dynstr_append_mem(str, quote.str, quote.length); cur_pos= next_pos + 1; } ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos)); append= va_arg(dirty_text, char *); } va_end(dirty_text); - ret&= dynstr_append_mem(str, quote_str, quote_len); /* Trailing quote */ + ret&= dynstr_append_mem(str, quote.str, quote.length); /* Trailing quote */ return ret; } -- cgit v1.2.1 From 1e361f286bc2cd42c79a4c0ac40209d222e60b11 Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Sat, 6 Feb 2016 13:57:59 +0100 Subject: MDEV-4664 mysql_upgrade crashes if root's password contains an apostrophe/single quotation mark fix dynstr_append_os_quoted() to escape single quotes correctly for a POSIX shell --- mysys/string.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'mysys/string.c') diff --git a/mysys/string.c b/mysys/string.c index 06fd2c3d014..a63b1f502e5 100644 --- a/mysys/string.c +++ b/mysys/string.c @@ -143,8 +143,10 @@ my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...) { #ifdef __WIN__ LEX_CSTRING quote= { C_STRING_WITH_LEN("\"") }; + LEX_CSTRING replace= { C_STRING_WITH_LEN("\\\"") }; #else LEX_CSTRING quote= { C_STRING_WITH_LEN("\'") }; + LEX_CSTRING replace= { C_STRING_WITH_LEN("'\"'\"'") }; #endif /* __WIN__ */ my_bool ret= TRUE; va_list dirty_text; @@ -160,8 +162,7 @@ my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append, ...) while(*(next_pos= strcend(cur_pos, quote.str[0])) != '\0') { ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos)); - ret&= dynstr_append_mem(str, STRING_WITH_LEN("\\")); - ret&= dynstr_append_mem(str, quote.str, quote.length); + ret&= dynstr_append_mem(str, replace.str, replace.length); cur_pos= next_pos + 1; } ret&= dynstr_append_mem(str, cur_pos, (uint) (next_pos - cur_pos)); -- cgit v1.2.1