| 197 | } |
| 198 | |
| 199 | CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON *const object, const cJSON *const target) |
| 200 | { |
| 201 | size_t child_index = 0; |
| 202 | cJSON *current_child = 0; |
| 203 | |
| 204 | if ((object == NULL) || (target == NULL)) |
| 205 | { |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | if (object == target) |
| 210 | { |
| 211 | /* found */ |
| 212 | return (char *)cJSONUtils_strdup((const unsigned char *)""); |
| 213 | } |
| 214 | |
| 215 | /* recursively search all children of the object or array */ |
| 216 | for (current_child = object->child; current_child != NULL; |
| 217 | (void)(current_child = current_child->next), child_index++) |
| 218 | { |
| 219 | unsigned char *target_pointer = (unsigned char *)cJSONUtils_FindPointerFromObjectTo(current_child, target); |
| 220 | /* found the target? */ |
| 221 | if (target_pointer != NULL) |
| 222 | { |
| 223 | if (cJSON_IsArray(object)) |
| 224 | { |
| 225 | /* reserve enough memory for a 64 bit integer + '/' and '\0' */ |
| 226 | unsigned char *full_pointer = |
| 227 | (unsigned char *)cJSON_malloc(strlen((char *)target_pointer) + 20 + sizeof("/")); |
| 228 | /* check if conversion to unsigned long is valid |
| 229 | * This should be eliminated at compile time by dead code elimination |
| 230 | * if size_t is an alias of unsigned long, or if it is bigger */ |
| 231 | if (child_index > ULONG_MAX) |
| 232 | { |
| 233 | cJSON_free(target_pointer); |
| 234 | cJSON_free(full_pointer); |
| 235 | return NULL; |
| 236 | } |
| 237 | sprintf((char *)full_pointer, "/%lu%s", (unsigned long)child_index, |
| 238 | target_pointer); /* /<array_index><path> */ |
| 239 | cJSON_free(target_pointer); |
| 240 | |
| 241 | return (char *)full_pointer; |
| 242 | } |
| 243 | |
| 244 | if (cJSON_IsObject(object)) |
| 245 | { |
| 246 | unsigned char *full_pointer = |
| 247 | (unsigned char *)cJSON_malloc(strlen((char *)target_pointer) + |
| 248 | pointer_encoded_length((unsigned char *)current_child->string) + 2); |
| 249 | full_pointer[0] = '/'; |
| 250 | encode_string_as_pointer(full_pointer + 1, (unsigned char *)current_child->string); |
| 251 | strcat((char *)full_pointer, (char *)target_pointer); |
| 252 | cJSON_free(target_pointer); |
| 253 | |
| 254 | return (char *)full_pointer; |
| 255 | } |
| 256 |
nothing calls this directly
no test coverage detected