From 4effcd2cf5b8e1cf2620f740c5dae260f9e7bca1 Mon Sep 17 00:00:00 2001 From: Scott Balneaves Date: Wed, 24 Feb 2016 20:53:13 -0600 Subject: [PATCH] Fix major bug in escape function; also need to worry about newlines, etc. Replace with g_str functions. (cherry picked from commit 1ea83d1) --- src/lightdm-webkit2-greeter-ext.c | 39 ++++++++++--------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/lightdm-webkit2-greeter-ext.c b/src/lightdm-webkit2-greeter-ext.c index 20b1c3d..a666c45 100644 --- a/src/lightdm-webkit2-greeter-ext.c +++ b/src/lightdm-webkit2-greeter-ext.c @@ -170,39 +170,24 @@ arg_to_string(JSContextRef context, JSValueRef arg, JSValueRef *exception) { * quote characters escaped. */ static char * -escape(const gchar *text) { - size_t len; - size_t i, j; - int count = 0; + escape(const gchar *text) { gchar *escaped; + gchar **split; + gchar *result; - len = strlen(text); - - for (i = 0; i < len; i++) { - if (text[i] == '\'') { - count++; - } - } - - if (count == 0) { - - return g_strdup(text); - } + /* Make sure all newlines, tabs, etc. are escaped. */ + escaped = g_strescape (text, NULL); - escaped = g_malloc(len + count + 1); + /* Split the string up on the single quote character (') */ + split = g_strsplit (escaped, "'", -1); - j = 0; + /* Rejoin, substituting the escaped single quote for the separator. */ + result = g_strjoinv ("\\'", split); - for (i = 0; i <= len; i++) { - if (text[i] == '\'') { - escaped[j] = '\\'; - j++; - } - escaped[j] = text[i]; - j++; - } + g_free (escaped); + g_strfreev (split); - return escaped; + return result; }