cJSON
[Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065)
Brought by drakestapleton
cJSON / cJSON_Utils.h
| 1 | /* |
| 2 | Copyright (c) 2009-2017 Dave Gamble and cJSON contributors |
| 3 | |
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | of this software and associated documentation files (the "Software"), to deal |
| 6 | in the Software without restriction, including without limitation the rights |
| 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | copies of the Software, and to permit persons to whom the Software is |
| 9 | furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | The above copyright notice and this permission notice shall be included in |
| 12 | all copies or substantial portions of the Software. |
| 13 | |
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef cJSON_Utils__h |
| 24 | #define cJSON_Utils__h |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" |
| 28 | { |
| 29 | #endif |
| 30 | |
| 31 | #include "cJSON.h" |
| 32 | |
| 33 | /* Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. */ |
| 34 | CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer); |
| 35 | CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointerCaseSensitive(cJSON * const object, const char *pointer); |
| 36 | |
| 37 | /* Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. */ |
| 38 | /* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */ |
| 39 | CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to); |
| 40 | CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatchesCaseSensitive(cJSON * const from, cJSON * const to); |
| 41 | /* Utility for generating patch array entries. */ |
| 42 | CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value); |
| 43 | /* Returns 0 for success. */ |
| 44 | CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches); |
| 45 | CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, const cJSON * const patches); |
| 46 | |
| 47 | /* |
| 48 | // Note that ApplyPatches is NOT atomic on failure. To implement an atomic ApplyPatches, use: |
| 49 | //int cJSONUtils_AtomicApplyPatches(cJSON **object, cJSON *patches) |
| 50 | //{ |
| 51 | // cJSON *modme = cJSON_Duplicate(*object, 1); |
| 52 | // int error = cJSONUtils_ApplyPatches(modme, patches); |
| 53 | // if (!error) |
| 54 | // { |
| 55 | // cJSON_Delete(*object); |
| 56 | // *object = modme; |
| 57 | // } |
| 58 | // else |
| 59 | // { |
| 60 | // cJSON_Delete(modme); |
| 61 | // } |
| 62 | // |
| 63 | // return error; |
| 64 | //} |
| 65 | // Code not added to library since this strategy is a LOT slower. |
| 66 | */ |
| 67 | |
| 68 | /* Implement RFC7386 (https://tools.ietf.org/html/rfc7396) JSON Merge Patch spec. */ |
| 69 | /* target will be modified by patch. return value is new ptr for target. */ |
| 70 | CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch); |
| 71 | CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch); |
| 72 | /* generates a patch to move from -> to */ |
| 73 | /* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */ |
| 74 | CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to); |
| 75 | CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatchCaseSensitive(cJSON * const from, cJSON * const to); |
| 76 | |
| 77 | /* Given a root object and a target object, construct a pointer from one to the other. */ |
| 78 | CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target); |
| 79 | |
| 80 | /* Sorts the members of the object into alphabetical order. */ |
| 81 | CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object); |
| 82 | CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object); |
| 83 | |
| 84 | #ifdef __cplusplus |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | #endif |