From 4ba6bafe34af2a7334389ef84f64d291d91fdf74 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 30 Apr 2017 13:00:16 +0200 Subject: [PATCH] refactor cJSONUtils_InplaceDecodePointerString --- cJSON_Utils.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 9c0c33e..e9e809c 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -288,22 +288,35 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *po /* JSON Patch implementation. */ static void cJSONUtils_InplaceDecodePointerString(unsigned char *string) { - unsigned char *s2 = string; + unsigned char *decoded_string = string; if (string == NULL) { return; } - for (; *string; (void)s2++, string++) + for (; *string; (void)decoded_string++, string++) { - *s2 = (unsigned char) ((*string != '~') - ? (*string) - : ((*(++string) == '0') - ? '~' - : '/')); + if (string[0] == '~') + { + if (string[1] == '0') + { + decoded_string[0] = '~'; + } + else if (string[1] == '1') + { + decoded_string[1] = '/'; + } + else + { + /* invalid escape sequence */ + return; + } + + string++; + } } - *s2 = '\0'; + decoded_string[0] = '\0'; } /* non-broken cJSON_DetachItemFromArray */ -- GitLab