浏览代码

refactor cJSONUtils_InplaceDecodePointerString

Max Bruckner 8 年之前
父节点
当前提交
4ba6bafe34
共有 1 个文件被更改,包括 21 次插入8 次删除
  1. 21 8
      cJSON_Utils.c

+ 21 - 8
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 */