From 3ec7cf64bcf8b8bfa098529c784d73c41f46a348 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 10 Dec 2020 16:01:04 +1100 Subject: Fix BLI_str_quoted_substrN use with escaped strings BLI_str_quoted_substrN didn't unescape the resulting string, yet all callers were using this with animation paths which are created using BLI_str_escape(). - Fix BLI_str_quoted_substrN use with escaped strings by calling BLI_str_unescape(). Note that it's possible we want an a version of this function that keeps escape characters. This could be added if it's required. - Fix end quote detection using BLI_str_escape_find_quote checking for `\"` isn't reliable since an even number of back-slashes before a quote means it's not escaped. --- source/blender/blenlib/intern/string.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'source/blender/blenlib/intern') diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index d02241230de..dccc700c761 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -414,16 +414,14 @@ const char *BLI_str_escape_find_quote(const char *str) } /** - * Makes a copy of the text within the "" that appear after some text 'blahblah' - * i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples" + * Makes a copy of the text within the "" that appear after some text `blahblah`. + * i.e. for string `pose["apples"]` with prefix `pose[`, it will return `apples`. * - * - str: is the entire string to chop - * - prefix: is the part of the string to leave out + * \param str: is the entire string to chop. + * \param prefix: is the part of the string to step over. * - * Assume that the strings returned must be freed afterwards, and that the inputs will contain - * data we want... - * - * \return the offset and a length so as to avoid doing an allocation. + * Assume that the strings returned must be freed afterwards, + * and that the inputs will contain data we want. */ char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix) { @@ -436,18 +434,15 @@ char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict const size_t prefix_len = strlen(prefix); start_match += prefix_len + 1; /* get the end point (i.e. where the next occurrence of " is after the starting point) */ - - end_match = start_match; - while ((end_match = strchr(end_match, '"'))) { - if (LIKELY(*(end_match - 1) != '\\')) { - break; - } - end_match++; - } - + end_match = BLI_str_escape_find_quote(start_match); if (end_match) { - /* return the slice indicated */ - return BLI_strdupn(start_match, (size_t)(end_match - start_match)); + const size_t unescaped_len = (size_t)(end_match - start_match); + char *result = MEM_mallocN(sizeof(char) * (unescaped_len + 1), __func__); + const size_t escaped_len = BLI_str_unescape(result, start_match, unescaped_len); + if (escaped_len != unescaped_len) { + result = MEM_reallocN(result, sizeof(char) * (escaped_len + 1)); + } + return result; } } return BLI_strdupn("", 0); -- cgit v1.2.3