From beec47e70a7180f3e03556724ae7d1f67c016e8a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 9 Sep 2018 16:03:52 +0200 Subject: [PATCH] shared: use nm_utils_buf_utf8safe_unescape() for nm_utils_str_utf8safe_unescape() nm_utils_buf_utf8safe_unescape() is almost the same as g_strcompress(), with the only difference is that if the string contains NUL escapes "\000", it will be handled correctly. In other words, g_strcompress() and nm_utils_str_utf8safe_unescape() can only unescape values, that contain no NUL escapes. That's why we added our own binary unescape function. As we already have our g_strcompress() variant, use it. It just gives it more testing and usage. Also, we have full control over it's behavior. For example, g_strcompress() issues a g_warning() when encountering a trailing '\\'. I think this makes it unsuitable to unescape untrusted data. Either the function should fail, or just make the best of it. Currently, our implementation does the latter. --- shared/nm-glib-aux/nm-shared-utils.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 906c1a9f43..e941232a82 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -2264,6 +2264,9 @@ nm_utils_buf_utf8safe_unescape (const char *str, gsize *out_len, gpointer *to_fr v = v * 8 + (ch - '0'); ch = (++str)[0]; if (ch >= '0' && ch <= '7') { + /* technically, escape sequences larger than \3FF are out of range + * and invalid. We don't check for that, and do the same as + * g_strcompress(): silently clip the value with & 0xFF. */ v = v * 8 + (ch - '0'); ++str; } @@ -2432,13 +2435,11 @@ nm_utils_buf_utf8safe_escape_bytes (GBytes *bytes, NMUtilsStrUtf8SafeFlags flags const char * nm_utils_str_utf8safe_unescape (const char *str, char **to_free) { + gsize len; + g_return_val_if_fail (to_free, NULL); - if (!str || !strchr (str, '\\')) { - *to_free = NULL; - return str; - } - return (*to_free = g_strcompress (str)); + return nm_utils_buf_utf8safe_unescape (str, &len, (gpointer *) to_free); } /** @@ -2498,7 +2499,10 @@ nm_utils_str_utf8safe_escape_cp (const char *str, NMUtilsStrUtf8SafeFlags flags) char * nm_utils_str_utf8safe_unescape_cp (const char *str) { - return str ? g_strcompress (str) : NULL; + char *s; + + str = nm_utils_str_utf8safe_unescape (str, &s); + return s ?: g_strdup (str); } char *