Giter Club home page Giter Club logo

cjson's Introduction

cJSON

Ultralightweight JSON parser in ANSI C.

Table of contents

License

MIT License

Copyright (c) 2009-2017 Dave Gamble and cJSON contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Usage

Welcome to cJSON.

cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file.

JSON is described best here: http://www.json.org/ It's like XML, but fat-free. You use it to move data around, store things, or just generally represent your program's state.

As a library, cJSON exists to take away as much legwork as it can, but not get in your way. As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it in one of two modes: Auto and Manual. Let's have a quick run-through.

I lifted some JSON from this page: http://www.json.org/fatfree.html That page inspired me to write cJSON, which is a parser that tries to share the same philosophy as JSON itself. Simple, dumb, out of the way.

Building

There are several ways to incorporate cJSON into your project.

copying the source

Because the entire library is only one C file and one header file, you can just copy cJSON.h and cJSON.c to your projects source and start using it.

cJSON is written in ANSI C (C89) in order to support as many platforms and compilers as possible.

CMake

With CMake, cJSON supports a full blown build system. This way you get the most features. CMake with an equal or higher version than 2.8.5 is supported. With CMake it is recommended to do an out of tree build, meaning the compiled files are put in a directory separate from the source files. So in order to build cJSON with CMake on a Unix platform, make a build directory and run CMake inside it.

mkdir build
cd build
cmake ..

This will create a Makefile and a bunch of other files. You can then compile it:

make

And install it with make install if you want. By default it installs the headers /usr/local/include/cjson and the libraries to /usr/local/lib. It also installs files for pkg-config to make it easier to detect and use an existing installation of CMake. And it installs CMake config files, that can be used by other CMake based projects to discover the library.

You can change the build process with a list of different options that you can pass to CMake. Turn them on with On and off with Off:

  • -DENABLE_CJSON_TEST=On: Enable building the tests. (on by default)
  • -DENABLE_CJSON_UTILS=On: Enable building cJSON_Utils. (off by default)
  • -DENABLE_TARGET_EXPORT=On: Enable the export of CMake targets. Turn off if it makes problems. (on by default)
  • -DENABLE_CUSTOM_COMPILER_FLAGS=On: Enable custom compiler flags (currently for Clang, GCC and MSVC). Turn off if it makes problems. (on by default)
  • -DENABLE_VALGRIND=On: Run tests with valgrind. (off by default)
  • -DENABLE_SANITIZERS=On: Compile cJSON with AddressSanitizer and UndefinedBehaviorSanitizer enabled (if possible). (off by default)
  • -DENABLE_SAFE_STACK: Enable the SafeStack instrumentation pass. Currently only works with the Clang compiler. (off by default)
  • -DBUILD_SHARED_LIBS=On: Build the shared libraries. (on by default)
  • -DBUILD_SHARED_AND_STATIC_LIBS=On: Build both shared and static libraries. (off by default)
  • -DCMAKE_INSTALL_PREFIX=/usr: Set a prefix for the installation.
  • -DENABLE_LOCALES=On: Enable the usage of localeconv method. ( on by default )
  • -DCJSON_OVERRIDE_BUILD_SHARED_LIBS=On: Enable overriding the value of BUILD_SHARED_LIBS with -DCJSON_BUILD_SHARED_LIBS.
  • -DENABLE_CJSON_VERSION_SO: Enable cJSON so version. ( on by default )

If you are packaging cJSON for a distribution of Linux, you would probably take these steps for example:

mkdir build
cd build
cmake .. -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TEST=Off -DCMAKE_INSTALL_PREFIX=/usr
make
make DESTDIR=$pkgdir install

On Windows CMake is usually used to create a Visual Studio solution file by running it inside the Developer Command Prompt for Visual Studio, for exact steps follow the official documentation from CMake and Microsoft and use the online search engine of your choice. The descriptions of the the options above still generally apply, although not all of them work on Windows.

Makefile

NOTE: This Method is deprecated. Use CMake if at all possible. Makefile support is limited to fixing bugs.

If you don't have CMake available, but still have GNU make. You can use the makefile to build cJSON:

Run this command in the directory with the source code and it will automatically compile static and shared libraries and a little test program (not the full test suite).

make all

If you want, you can install the compiled library to your system using make install. By default it will install the headers in /usr/local/include/cjson and the libraries in /usr/local/lib. But you can change this behavior by setting the PREFIX and DESTDIR variables: make PREFIX=/usr DESTDIR=temp install. And uninstall them with: make PREFIX=/usr DESTDIR=temp uninstall.

Meson

To make cjson work in a project using meson, the libcjson dependency has to be included:

project('c-json-example', 'c')

cjson = dependency('libcjson')

example = executable(
    'example',
    'example.c',
    dependencies: [cjson],
)

Vcpkg

You can download and install cJSON using the vcpkg dependency manager:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install cjson

The cJSON port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.

Including cJSON

If you installed it via CMake or the Makefile, you can include cJSON like this:

#include <cjson/cJSON.h>

Data Structure

cJSON represents JSON data using the cJSON struct data type:

/* The cJSON structure: */
typedef struct cJSON
{
    struct cJSON *next;
    struct cJSON *prev;
    struct cJSON *child;
    int type;
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    double valuedouble;
    char *string;
} cJSON;

An item of this type represents a JSON value. The type is stored in type as a bit-flag (this means that you cannot find out the type by just comparing the value of type).

To check the type of an item, use the corresponding cJSON_Is... function. It does a NULL check followed by a type check and returns a boolean value if the item is of this type.

The type can be one of the following:

  • cJSON_Invalid (check with cJSON_IsInvalid): Represents an invalid item that doesn't contain any value. You automatically have this type if you set the item to all zero bytes.
  • cJSON_False (check with cJSON_IsFalse): Represents a false boolean value. You can also check for boolean values in general with cJSON_IsBool.
  • cJSON_True (check with cJSON_IsTrue): Represents a true boolean value. You can also check for boolean values in general with cJSON_IsBool.
  • cJSON_NULL (check with cJSON_IsNull): Represents a null value.
  • cJSON_Number (check with cJSON_IsNumber): Represents a number value. The value is stored as a double in valuedouble and also in valueint. If the number is outside of the range of an integer, INT_MAX or INT_MIN are used for valueint.
  • cJSON_String (check with cJSON_IsString): Represents a string value. It is stored in the form of a zero terminated string in valuestring.
  • cJSON_Array (check with cJSON_IsArray): Represent an array value. This is implemented by pointing child to a linked list of cJSON items that represent the values in the array. The elements are linked together using next and prev, where the first element has prev.next == NULL and the last element next == NULL.
  • cJSON_Object (check with cJSON_IsObject): Represents an object value. Objects are stored same way as an array, the only difference is that the items in the object store their keys in string.
  • cJSON_Raw (check with cJSON_IsRaw): Represents any kind of JSON that is stored as a zero terminated array of characters in valuestring. This can be used, for example, to avoid printing the same static JSON over and over again to save performance. cJSON will never create this type when parsing. Also note that cJSON doesn't check if it is valid JSON.

Additionally there are the following two flags:

  • cJSON_IsReference: Specifies that the item that child points to and/or valuestring is not owned by this item, it is only a reference. So cJSON_Delete and other functions will only deallocate this item, not its child/valuestring.
  • cJSON_StringIsConst: This means that string points to a constant string. This means that cJSON_Delete and other functions will not try to deallocate string.

Working with the data structure

For every value type there is a cJSON_Create... function that can be used to create an item of that type. All of these will allocate a cJSON struct that can later be deleted with cJSON_Delete. Note that you have to delete them at some point, otherwise you will get a memory leak.
Important: If you have added an item to an array or an object already, you mustn't delete it with cJSON_Delete. Adding it to an array or object transfers its ownership so that when that array or object is deleted, it gets deleted as well. You also could use cJSON_SetValuestring to change a cJSON_String's valuestring, and you needn't to free the previous valuestring manually.

Basic types

  • null is created with cJSON_CreateNull
  • booleans are created with cJSON_CreateTrue, cJSON_CreateFalse or cJSON_CreateBool
  • numbers are created with cJSON_CreateNumber. This will set both valuedouble and valueint. If the number is outside of the range of an integer, INT_MAX or INT_MIN are used for valueint
  • strings are created with cJSON_CreateString (copies the string) or with cJSON_CreateStringReference (directly points to the string. This means that valuestring won't be deleted by cJSON_Delete and you are responsible for its lifetime, useful for constants)

Arrays

You can create an empty array with cJSON_CreateArray. cJSON_CreateArrayReference can be used to create an array that doesn't "own" its content, so its content doesn't get deleted by cJSON_Delete.

To add items to an array, use cJSON_AddItemToArray to append items to the end. Using cJSON_AddItemReferenceToArray an element can be added as a reference to another item, array or string. This means that cJSON_Delete will not delete that items child or valuestring properties, so no double frees are occurring if they are already used elsewhere. To insert items in the middle, use cJSON_InsertItemInArray. It will insert an item at the given 0 based index and shift all the existing items to the right.

If you want to take an item out of an array at a given index and continue using it, use cJSON_DetachItemFromArray, it will return the detached item, so be sure to assign it to a pointer, otherwise you will have a memory leak.

Deleting items is done with cJSON_DeleteItemFromArray. It works like cJSON_DetachItemFromArray, but deletes the detached item via cJSON_Delete.

You can also replace an item in an array in place. Either with cJSON_ReplaceItemInArray using an index or with cJSON_ReplaceItemViaPointer given a pointer to an element. cJSON_ReplaceItemViaPointer will return 0 if it fails. What this does internally is to detach the old item, delete it and insert the new item in its place.

To get the size of an array, use cJSON_GetArraySize. Use cJSON_GetArrayItem to get an element at a given index.

Because an array is stored as a linked list, iterating it via index is inefficient (O(n²)), so you can iterate over an array using the cJSON_ArrayForEach macro in O(n) time complexity.

Objects

You can create an empty object with cJSON_CreateObject. cJSON_CreateObjectReference can be used to create an object that doesn't "own" its content, so its content doesn't get deleted by cJSON_Delete.

To add items to an object, use cJSON_AddItemToObject. Use cJSON_AddItemToObjectCS to add an item to an object with a name that is a constant or reference (key of the item, string in the cJSON struct), so that it doesn't get freed by cJSON_Delete. Using cJSON_AddItemReferenceToArray an element can be added as a reference to another object, array or string. This means that cJSON_Delete will not delete that items child or valuestring properties, so no double frees are occurring if they are already used elsewhere.

If you want to take an item out of an object, use cJSON_DetachItemFromObjectCaseSensitive, it will return the detached item, so be sure to assign it to a pointer, otherwise you will have a memory leak.

Deleting items is done with cJSON_DeleteItemFromObjectCaseSensitive. It works like cJSON_DetachItemFromObjectCaseSensitive followed by cJSON_Delete.

You can also replace an item in an object in place. Either with cJSON_ReplaceItemInObjectCaseSensitive using a key or with cJSON_ReplaceItemViaPointer given a pointer to an element. cJSON_ReplaceItemViaPointer will return 0 if it fails. What this does internally is to detach the old item, delete it and insert the new item in its place.

To get the size of an object, you can use cJSON_GetArraySize, this works because internally objects are stored as arrays.

If you want to access an item in an object, use cJSON_GetObjectItemCaseSensitive.

To iterate over an object, you can use the cJSON_ArrayForEach macro the same way as for arrays.

cJSON also provides convenient helper functions for quickly creating a new item and adding it to an object, like cJSON_AddNullToObject. They return a pointer to the new item or NULL if they failed.

Parsing JSON

Given some JSON in a zero terminated string, you can parse it with cJSON_Parse.

cJSON *json = cJSON_Parse(string);

Given some JSON in a string (whether zero terminated or not), you can parse it with cJSON_ParseWithLength.

cJSON *json = cJSON_ParseWithLength(string, buffer_length);

It will parse the JSON and allocate a tree of cJSON items that represents it. Once it returns, you are fully responsible for deallocating it after use with cJSON_Delete.

The allocator used by cJSON_Parse is malloc and free by default but can be changed (globally) with cJSON_InitHooks.

If an error occurs a pointer to the position of the error in the input string can be accessed using cJSON_GetErrorPtr. Note though that this can produce race conditions in multithreading scenarios, in that case it is better to use cJSON_ParseWithOpts with return_parse_end. By default, characters in the input string that follow the parsed JSON will not be considered as an error.

If you want more options, use cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated). return_parse_end returns a pointer to the end of the JSON in the input string or the position that an error occurs at (thereby replacing cJSON_GetErrorPtr in a thread safe way). require_null_terminated, if set to 1 will make it an error if the input string contains data after the JSON.

If you want more options giving buffer length, use cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated).

Printing JSON

Given a tree of cJSON items, you can print them as a string using cJSON_Print.

char *string = cJSON_Print(json);

It will allocate a string and print a JSON representation of the tree into it. Once it returns, you are fully responsible for deallocating it after use with your allocator. (usually free, depends on what has been set with cJSON_InitHooks).

cJSON_Print will print with whitespace for formatting. If you want to print without formatting, use cJSON_PrintUnformatted.

If you have a rough idea of how big your resulting string will be, you can use cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt). fmt is a boolean to turn formatting with whitespace on and off. prebuffer specifies the first buffer size to use for printing. cJSON_Print currently uses 256 bytes for its first buffer size. Once printing runs out of space, a new buffer is allocated and the old gets copied over before printing is continued.

These dynamic buffer allocations can be completely avoided by using cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format). It takes a buffer to a pointer to print to and its length. If the length is reached, printing will fail and it returns 0. In case of success, 1 is returned. Note that you should provide 5 bytes more than is actually needed, because cJSON is not 100% accurate in estimating if the provided memory is enough.

Example

In this example we want to build and parse the following JSON:

{
    "name": "Awesome 4K",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        },
        {
            "width": 1920,
            "height": 1080
        },
        {
            "width": 3840,
            "height": 2160
        }
    ]
}

Printing

Let's build the above JSON and print it to a string:

//create a monitor with a list of supported resolutions
//NOTE: Returns a heap allocated string, you are required to free it after use.
char *create_monitor(void)
{
    const unsigned int resolution_numbers[3][2] = {
        {1280, 720},
        {1920, 1080},
        {3840, 2160}
    };
    char *string = NULL;
    cJSON *name = NULL;
    cJSON *resolutions = NULL;
    cJSON *resolution = NULL;
    cJSON *width = NULL;
    cJSON *height = NULL;
    size_t index = 0;

    cJSON *monitor = cJSON_CreateObject();
    if (monitor == NULL)
    {
        goto end;
    }

    name = cJSON_CreateString("Awesome 4K");
    if (name == NULL)
    {
        goto end;
    }
    /* after creation was successful, immediately add it to the monitor,
     * thereby transferring ownership of the pointer to it */
    cJSON_AddItemToObject(monitor, "name", name);

    resolutions = cJSON_CreateArray();
    if (resolutions == NULL)
    {
        goto end;
    }
    cJSON_AddItemToObject(monitor, "resolutions", resolutions);

    for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
    {
        resolution = cJSON_CreateObject();
        if (resolution == NULL)
        {
            goto end;
        }
        cJSON_AddItemToArray(resolutions, resolution);

        width = cJSON_CreateNumber(resolution_numbers[index][0]);
        if (width == NULL)
        {
            goto end;
        }
        cJSON_AddItemToObject(resolution, "width", width);

        height = cJSON_CreateNumber(resolution_numbers[index][1]);
        if (height == NULL)
        {
            goto end;
        }
        cJSON_AddItemToObject(resolution, "height", height);
    }

    string = cJSON_Print(monitor);
    if (string == NULL)
    {
        fprintf(stderr, "Failed to print monitor.\n");
    }

end:
    cJSON_Delete(monitor);
    return string;
}

Alternatively we can use the cJSON_Add...ToObject helper functions to make our lives a little easier:

//NOTE: Returns a heap allocated string, you are required to free it after use.
char *create_monitor_with_helpers(void)
{
    const unsigned int resolution_numbers[3][2] = {
        {1280, 720},
        {1920, 1080},
        {3840, 2160}
    };
    char *string = NULL;
    cJSON *resolutions = NULL;
    size_t index = 0;

    cJSON *monitor = cJSON_CreateObject();

    if (cJSON_AddStringToObject(monitor, "name", "Awesome 4K") == NULL)
    {
        goto end;
    }

    resolutions = cJSON_AddArrayToObject(monitor, "resolutions");
    if (resolutions == NULL)
    {
        goto end;
    }

    for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
    {
        cJSON *resolution = cJSON_CreateObject();

        if (cJSON_AddNumberToObject(resolution, "width", resolution_numbers[index][0]) == NULL)
        {
            goto end;
        }

        if (cJSON_AddNumberToObject(resolution, "height", resolution_numbers[index][1]) == NULL)
        {
            goto end;
        }

        cJSON_AddItemToArray(resolutions, resolution);
    }

    string = cJSON_Print(monitor);
    if (string == NULL)
    {
        fprintf(stderr, "Failed to print monitor.\n");
    }

end:
    cJSON_Delete(monitor);
    return string;
}

Parsing

In this example we will parse a JSON in the above format and check if the monitor supports a Full HD resolution while printing some diagnostic output:

/* return 1 if the monitor supports full hd, 0 otherwise */
int supports_full_hd(const char * const monitor)
{
    const cJSON *resolution = NULL;
    const cJSON *resolutions = NULL;
    const cJSON *name = NULL;
    int status = 0;
    cJSON *monitor_json = cJSON_Parse(monitor);
    if (monitor_json == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        status = 0;
        goto end;
    }

    name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name");
    if (cJSON_IsString(name) && (name->valuestring != NULL))
    {
        printf("Checking monitor \"%s\"\n", name->valuestring);
    }

    resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions");
    cJSON_ArrayForEach(resolution, resolutions)
    {
        cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width");
        cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height");

        if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height))
        {
            status = 0;
            goto end;
        }

        if ((width->valuedouble == 1920) && (height->valuedouble == 1080))
        {
            status = 1;
            goto end;
        }
    }

end:
    cJSON_Delete(monitor_json);
    return status;
}

Note that there are no NULL checks except for the result of cJSON_Parse because cJSON_GetObjectItemCaseSensitive checks for NULL inputs already, so a NULL value is just propagated and cJSON_IsNumber and cJSON_IsString return 0 if the input is NULL.

Caveats

Zero Character

cJSON doesn't support strings that contain the zero character '\0' or \u0000. This is impossible with the current API because strings are zero terminated.

Character Encoding

cJSON only supports UTF-8 encoded input. In most cases it doesn't reject invalid UTF-8 as input though, it just propagates it through as is. As long as the input doesn't contain invalid UTF-8, the output will always be valid UTF-8.

C Standard

cJSON is written in ANSI C (or C89, C90). If your compiler or C library doesn't follow this standard, correct behavior is not guaranteed.

NOTE: ANSI C is not C++ therefore it shouldn't be compiled with a C++ compiler. You can compile it with a C compiler and link it with your C++ code however. Although compiling with a C++ compiler might work, correct behavior is not guaranteed.

Floating Point Numbers

cJSON does not officially support any double implementations other than IEEE754 double precision floating point numbers. It might still work with other implementations but bugs with these will be considered invalid.

The maximum length of a floating point literal that cJSON supports is currently 63 characters.

Deep Nesting Of Arrays And Objects

cJSON doesn't support arrays and objects that are nested too deeply because this would result in a stack overflow. To prevent this cJSON limits the depth to CJSON_NESTING_LIMIT which is 1000 by default but can be changed at compile time.

Thread Safety

In general cJSON is not thread safe.

However it is thread safe under the following conditions:

  • cJSON_GetErrorPtr is never used (the return_parse_end parameter of cJSON_ParseWithOpts can be used instead)
  • cJSON_InitHooks is only ever called before using cJSON in any threads.
  • setlocale is never called before all calls to cJSON functions have returned.

Case Sensitivity

When cJSON was originally created, it didn't follow the JSON standard and didn't make a distinction between uppercase and lowercase letters. If you want the correct, standard compliant, behavior, you need to use the CaseSensitive functions where available.

Duplicate Object Members

cJSON supports parsing and printing JSON that contains objects that have multiple members with the same name. cJSON_GetObjectItemCaseSensitive however will always only return the first one.

Enjoy cJSON!

  • Dave Gamble (original author)
  • Max Bruckner and Alan Wang (current maintainer)
  • and the other cJSON contributors

cjson's People

Contributors

alanscut avatar anton-sergeev avatar bigbrainafk avatar casperinous avatar chisholmkyle avatar davegamble avatar deborag avatar dongwenhuang avatar ffontaine avatar fpnuseis avatar fsmaxb avatar gatzka avatar gitmensch avatar kbranigan avatar lntuition avatar miaoerduo avatar mjerris avatar pawelwms avatar peteralfredlee avatar randy408 avatar schmidtw avatar sdrsdr avatar simon-p-r avatar square789 avatar superh-0630 avatar timgates42 avatar up-wind avatar vemakereporter avatar yangfl avatar zhindes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cjson's Issues

Compilation fails if libm does not define fpclassify()/isnormal()

Commit e4b96fa introduced a nice fix that checks floating point numbers for normality and emits "null" if they aren't.

This is a good thing, however some libm implementations (e.g. the one I have available for my microchip PIC) don't implement the functions fpclassify() & isnormal() which are used in this check. For our local use of cJSON, I've folded in an implementation of these methods from the JuliaLang/openlibm project. I'm not sure if there is a good way to get this back into the trunk.

Any thoughts?

Compile fails on Particle with cJON.h errors

Here is the error messages:

In file included from subscribe2.cpp:1:0:
cJSON.h:58:24: error: expected ';' at end of member declaration
void _(_malloc_fn)(size_t sz);
^

cJSON.h:58:33: error: expected ')' before 'sz'
void _(_malloc_fn)(size_t sz);
^

Reducing the size of the cJSON struct typedef

Hi

When working in an embedded environment, you might want to reduce your memory footprint. In my case, I never use floating variables (unless I have a FPU in my controlling chip). I have therefore introduced a cJSON_INCLUDE_DOUBLE in the source that, if not include, wont add the valuedouble variable in the cJSON struct and also removes all calculations to that variable.

As the sourcecode recently became more simple to read, it seems like the struct can be even more compacted.

As the type is set and cant be larger than 6 bits, a char should be able to hold it. And the valuestring, valueint and valuedouble should be able to be put in a union as they are only used in that way depending on the jSON type.

I am unfortunately not very good at github etiquette, so instead of contributing, I just saw this as an opportunity to discuss the matter.

Best regads
Martin

Need public API to free returned strings (e.g. cJSON_Print)

There should be a public API to free returned strings like from cJSON_Print().

My usage case is that I'm writing a middleware library that uses cJSON. The user is free to use cJSON elsewhere in their project. I don't necessarily know what memory allocator they are using, so when I call cJSON_Print(), I don't know what free() function to call. cJSON_free is private/internal.

I'm thinking either adding a cJSON_FreeString() which wraps cJSON_free, or making CJSON_free public are the easiest solutions.

Thank you

Can union be used to store the value?

Hello, I have an idea to use an union structure to store the value. Just as follows:

`// union of a cJSON value
typedef union cJSON_Value {
// suppose the size of a pointer is 4-byte. valuestring[1] may be useless.
char *valuestring[2];
int valueint[2];
double valuedouble;
} cJSON_Value;

// struct of cJSON
typedef struct cJSON {
struct cJSON _next,_prev;
struct cJSON *child;
int type;
cJSON_Value value;
char *string;
} cJSON;`

The use of union can reduce the size of a JSON on the sacrifice of the portability. So, it's just a suggestion.

cJSON_Parse bug

A str is not JSON format, but cJSON_Parse successful.

Here is the code.

# include <stdio.h>
# include "c_json.h"

int main()
{
      //const char\* str = "11}"; //Parse OK
     const char\* str = "(11"; //Parse Error
    //const char\* str = "11"; //Partse OK
    //const char\* str = "{\"key\":\"value\"}"; //Parse OK
    cJSON *json = NULL;
    json = cJSON_Parse(str);
    if (!json)
    {
    printf("Parse error, [%s]!\n", cJSON_GetErrorPtr());
    return -1;
    }
    printf("Parse success!\n");
    return 0;
}

use the MAX constants from <float.h>?

compiler warns with
cJSON.c:170:4: warning: floating constant exceeds range of 'double' [-Woverflow]

i think it might be better to use the constants defined in <float.h> throughout the code, like DBL_MAX, etc...

heap-buffer-overflow on parse_string

Hello, we recently found an invalid memory access parsing JSON in cJSON (rev 94df772).
We tested this issue on ArchLinux (x86_64) but other configurations could be affected.
Technical details about the issue are:

==6069==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000effa at pc 0x7fb1b577b64d bp 0x7ffe28cd5740 sp 0x7ffe28cd5730
READ of size 1 at 0x60200000effa thread T0
--
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/g/Work/Code/cJSON/cJSON.c:198 in parse_string

gdb backtrace is as follows:

Program received signal SIGABRT, Aborted.
0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#0  0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#1  0x00007ffff68c247a in abort () from /usr/lib/libc.so.6
#2  0x00007ffff6f1a0e9 in __sanitizer::Abort () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc:122
#3  0x00007ffff6f0effb in __sanitizer::Die () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_common.cc:145
#4  0x00007ffff6f083f7 in __asan::ScopedInErrorReport::~ScopedInErrorReport (this=<synthetic pointer>, __in_chrg=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:689
#5  __asan::ReportGenericError (pc=<optimized out>, bp=bp@entry=140737488349952, sp=sp@entry=140737488349936, addr=<optimized out>, is_write=is_write@entry=false, access_size=access_size@entry=1, exp=<optimized out>, fatal=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:1074
#6  0x00007ffff6f08ae7 in __asan::__asan_report_load1 (addr=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_rtl.cc:127
#7  0x00007ffff6c2f64d in parse_string (item=0x60600000ef60, str=0x60200000eff1 "\"\221: nul}l", ep=0x7ffff6e375a0 <global_ep>) at /home/g/Work/Code/cJSON/cJSON.c:198
#8  0x00007ffff6c32102 in parse_object (item=0x60600000efc0, value=0x60200000eff1 "\"\221: nul}l", ep=0x7ffff6e375a0 <global_ep>) at /home/g/Work/Code/cJSON/cJSON.c:539
#9  0x00007ffff6c30ed0 in parse_value (item=0x60600000efc0, value=0x60200000eff0 "{\"\221: nul}l", ep=0x7ffff6e375a0 <global_ep>) at /home/g/Work/Code/cJSON/cJSON.c:380
#10 0x00007ffff6c30923 in cJSON_ParseWithOpts (value=0x60200000eff0 "{\"\221: nul}l", return_parse_end=0x0, require_null_terminated=0) at /home/g/Work/Code/cJSON/cJSON.c:345
#11 0x00007ffff6c30a37 in cJSON_Parse (value=0x60200000eff0 "{\"\221: nul}l") at /home/g/Work/Code/cJSON/cJSON.c:354
#12 0x0000000000400a62 in main ()

This issue was found using QuickFuzz, the file to reproduce it is attached.
Regards.

power2gt incorrect for non 32bit integers

pow2gt calculates the next biggest power of 2 for a given x. But it only does it for integers up to sizes of 32 bit, 64 bit Integers are not supported.

I'm not sure, but it might even introduce undefined behavior for 16 bit integers. I'll have to do more research into this.

One possible solution would be to write a loop that does everything during runtime and relying on the compiler to be smart enough to optimise it properly.

heap-buffer-overflow on parse_number

Hello, we recently found an invalid memory access parsing JSON in cJSON (rev 94df772).
We tested this issue on ArchLinux (x86_64) but other configurations could be affected.
Technical details about the issue are:

==7804==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000eff1 at pc 0x7fbe5bad331f bp 0x7ffc9ef69030 sp 0x7ffc9ef69020
READ of size 1 at 0x60200000eff1 thread T0
--
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/g/Work/Code/cJSON/cJSON.c:102 in parse_number
Shadow bytes around the buggy address:

gdb backtrace is as follows:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Program received signal SIGABRT, Aborted.
0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#0  0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#1  0x00007ffff68c247a in abort () from /usr/lib/libc.so.6
#2  0x00007ffff6f1a0e9 in __sanitizer::Abort () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc:122
#3  0x00007ffff6f0effb in __sanitizer::Die () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_common.cc:145
#4  0x00007ffff6f083f7 in __asan::ScopedInErrorReport::~ScopedInErrorReport (this=<synthetic pointer>, __in_chrg=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:689
#5  __asan::ReportGenericError (pc=<optimized out>, bp=bp@entry=140737488350032, sp=sp@entry=140737488350016, addr=<optimized out>, is_write=is_write@entry=false, access_size=access_size@entry=1, exp=<optimized out>, fatal=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:1074
#6  0x00007ffff6f08ae7 in __asan::__asan_report_load1 (addr=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_rtl.cc:127
#7  0x00007ffff6c2e31f in parse_number (item=0x60600000efc0, num=0x60200000eff1 "") at /home/g/Work/Code/cJSON/cJSON.c:102
#8  0x00007ffff6c30e1a in parse_value (item=0x60600000efc0, value=0x60200000eff0 "0", ep=0x7ffff6e375a0 <global_ep>) at /home/g/Work/Code/cJSON/cJSON.c:378
#9  0x00007ffff6c30923 in cJSON_ParseWithOpts (value=0x60200000eff0 "0", return_parse_end=0x0, require_null_terminated=0) at /home/g/Work/Code/cJSON/cJSON.c:345
#10 0x00007ffff6c30a37 in cJSON_Parse (value=0x60200000eff0 "0") at /home/g/Work/Code/cJSON/cJSON.c:354
#11 0x0000000000400a62 in main ()

This issue was found using QuickFuzz, the file to reproduce it is attached.
Regards.

.

nevermind

Problem with floor and pow routines in uclinux and emcraft modules

Hi Dave and all,
Please disregard this issue. I've resolved it. Stupid mistake on my part.
Regards,
-Roger

Hi Dave,
I've got your code compiling on my main linux development server just fine.
I also have to get it running on a emcraft NXP/Freescale k70 module which runs uCllinux and I have run into some linking errors that I haven't been able to solve.

I've searched around the web and tried a couple of suggestions to no avail.
Before pressing on I thought I would ask your opinion if you think this should work ok considering the cross-compiler is from 2009. Or perhaps if you are aware of anybody using cJSON on uClinux-Arm micro-controllers/uClinux

I've included the output from the compiler and the compiler version below. If you have any info to share that would be great.
I'm beginning to think they are not supported in the library used by uClinux.

Last but not least, this is my first post on github so if I forgot anything let me know and I will fix it up.

Best Regards,
-Roger

Here's a dump of the make operation with the last round of suggested changes

make
arm-uclinuxeabi-gcc -Os -mcpu=cortex-m3 -mthumb -I/opt/software/emcraft/rwtlinux-cortextm-1.12.2/A2F/root/usr/include -I../includes -lm -c -o test.o test.c
arm-uclinuxeabi-gcc -Os -mcpu=cortex-m3 -mthumb -I/opt/software/emcraft/rwtlinux-cortextm-1.12.2/A2F/root/usr/include -I../includes -lm -c -o cJSON.o cJSON.c
arm-uclinuxeabi-gcc -mcpu=cortex-m3 -mthumb -L/opt/software/emcraft/rwtlinux-cortextm-1.12.2/A2F/root/usr/lib -lm -o test test.o cJSON.o
cJSON.o: In function print_number': cJSON.c:(.text+0xa26): undefined reference tofloor'
cJSON.o: In function parse_value': cJSON.c:(.text+0x14cc): undefined reference topow'
collect2: ld returned 1 exit status
make: *** [test] Error 1

Here's a dump of the compiler version info

arm-uclinuxeabi-gcc --version
arm-uclinuxeabi-gcc (Sourcery G++ Lite 2010q1-189) 4.4.1

Future management of the project

Hey,

@FSMaxB this post is largely aimed at you :)

I'm finding that I don't have enough free time to manage this project properly. Would you consider taking the primary role in maintaining this project?

cjson vs cJSON?

I'm working on some projects where we bring in cJSON (or cjson) via yocto & would like to know if we should leave the name of the library set as libcjson or should we use libcJSON?

Coding best practices

Some coding best practices as a suggestion.
Can we simplify the function and have one free and or returning point in a function?
Reduce the variable scope to be close to their usage,
Not to use unsafe functions like strcat?
Avoid magic numbers on mallocs.
I'll give an example:
Thanks
Carlos Tangerino

char *cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target) {
    char *ret = NULL;
    if (object == target) {
        /* found */
        ret = cJSONUtils_strdup("");
    } else {
        cJSON *obj = NULL;          <<<------ REDUCE the scope
        int type = object->type;    <<<------ REDUCE the scope
        int c = 0;                  <<<------ REDUCE the scope
        /* recursively search all children of the object */
        for (obj = object->child; obj; obj = obj->next, c++) {
            char *found = cJSONUtils_FindPointerFromObjectTo(obj, target);
            if (found) {
                if (type == cJSON_Array) {
                    /* reserve enough memory for a 64 bit integer + '/' and '\0' */
                    size_t retLen = strlen(found) + 23;     <<<----- MAGIC NUMBER
                    ret = (char*) malloc(retLen);
                    // <<<<<------ SAFE snprintf
                    snprintf(ret,retLen,  "/%d%s", c, found); /* /<array_index><path> */ 
                } else if (type == cJSON_Object) {
                    size_t retLen = strlen(found) + cJSONUtils_PointerEncodedstrlen(obj->string) + 2;
                    ret = (char*) malloc(retLen);   <<<------- EASY TO READ & DEBUG
                    *ret = '/';
                    cJSONUtils_PointerEncodedstrcpy(ret + 1, obj->string);
                    strcat(ret, found);             <<<<-------- PREFER strncat instead
                }
                /* reached leaf of the tree, found nothing */
                free(found);    <<<<----- ONCE HERE
            }
        }
    }
    /* not found */
    return ret;                 <<<----- ONCE HERE
}

Code formatting problematic

Code has inconsistent formatting (see cJSON_InitHooks) and often puts multiple statements on a single line (see cJSON_Delete). Putting multiple statements on a single line makes debugging difficult (since you can't add break points in the middle of a line). There is a program (https://github.com/uncrustify/uncrustify) that can automatically fix the formatting. I am happy to fork, fix the code, and create a pull request. Just let me know.

Thanks for a great library!

Blake McBride
https://github.com/blakemcbride

cJSON can't handle an integer value with more than 53 bits of precision

JSON syntax allows me to specify an integer that requires more than 53 bits of precision to correctly represent. cJSON can't store that in cJSON.valueint (which is only an int, which is 32 bits on most platforms), but storing it in a double will lose information (IEEE double-precision float only has 53 bits of mantissa).

Is there a reason why valueint couldn't be made a long long? Nearly all modern platforms treat that as int64_t. (I have a local fork with this change, which I've been using for quite some time; I'd be happy to submit a PR if you think this is a good idea.)

Memory leak in cJSON_ReplaceItemInObject

Hi, I think I found a memory leak in cJSON_ReplaceItemInObject:

    ....
    
    if(c)
    {
        //THESE LINES SHOULD PREVENT THE LEAK
        if (!(newitem->type & cJSON_StringIsConst) && newitem->string)
        {
           cJSON_free(newitem->string);
        }

        newitem->string = cJSON_strdup(string);
        cJSON_ReplaceItemInArray(object, i, newitem); 

Thanks,
Diego.

Testing Improvements

As it currently is, the tests for cJSON only attempts to use some of the functions and prints out the results. Not all of the functions appear to be covered and unless the user knows exactly how the result should look, there's no verification that it is correct. At the very least I think that there should be a strcmp run against the output to assert that everything matches. Going further, maybe a unit testing framework should be incorporated to be more thorough.

Consider changing name of "bool" type in cJSON.c introduced in v1.0.1

If cJSON.c ends up being compiled with a C++ compiler, it complains because as of commit 6790049 cJSON.c internally defines a bool type to be int. Commenting it out then can lead to problems because the header file still has the parameters as 'int'.

Consider introducing a type with a different name, and using it uniformly in both header file and implementation.

merge_json

Hi David.
Recently I am using your library for manipulating JSON documents, and came
across the need to meet RFC 7386, which provided a pseudo-code in which I
made a small code based on it. And I would like you to analyze and find
important my small contribution to your library is the will.

cJSON * merge_json(Json target, Json patch) {
cJSON target_name = NULL, keyValue = NULL,
*target_search = NULL;
if (patch->type == cJSON_Object) {
    if (target->type != cJSON_Object)
        target = cJSON_CreateObject();

    // Loop in patch
    keyValue = patch->child!=NULL?patch->child:patch->next;
    for (keyValue; keyValue; keyValue = keyValue->next) {
        if (keyValue->type == cJSON_NULL &&
            (cJSON_GetObjectItem(target, keyValue->string) != NULL)) {
            cJSON_DeleteItemFromObject(target, keyValue->string);
        } else {
            target_name = cJSON_GetObjectItem(target,
                keyValue->string);
            target_name = merge_patch(target_name, keyValue);

            target_search = cJSON_GetObjectItem(target, target_name->string);

            if (target_name->type != cJSON_Object && target_name->valuestring)
                target_search->valuestring = target_name->valuestring;
            else if (target_name->type != cJSON_Object)
                cJSON_SetNumberValue(target_search, target_name->valuedouble);
        }
    }

    return target;
} else return patch;}

target:
{ "Test": { "item1": 0, "item2": 1 }, "id": 0, "name": "", "description":
"desc", "lvl1": { "lvl2": { "itlvl3": "foo" } } }
patch:
{ "Test": { "item1": 2, "item2": 9 }, "id": 0, "name": "David", "lvl1": {
"lvl2": { "itlvl3": null } } }
merged:
{
"Test": {
"item1": 2,
"item2": 9
},
"id": 0,
"name": "David",
"description": "desc",
"lvl1": {
"lvl2": {
}
}
}
Best Regards.

Heya,
Just to bring to your attention - support for this project has moved to
github: https://github.com/DaveGamble/cJSON/
This might be a stupid question, but this behaviour seems awfully similar
to the merging implemented in cJSON_Utils.c.. am I mistaken?
Could we implement this as a wrapper to cJSONUtils_ApplyPatch ?
Dave.
On Thu, May 12, 2016 at 3:44 PM, "David de Paulo Silva Júnior"

Write documentation.

Hi. Could you please make documentation?
Library is useful but lack of documentation and necessity to read code
in order to use the library is disappointing.
For example, it is not obvious how to extract string value from member.
Thanks.

heap-buffer-overflow on skip

Hello, we recently found an invalid memory access parsing JSON in cJSON (rev 94df772).
We tested this issue on ArchLinux (x86_64) but other configurations could be affected.
Technical details about the issue are:

==7858==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000eff2 at pc 0x7f969f36886c bp 0x7ffcc1466c30 sp 0x7ffcc1466c20
READ of size 1 at 0x60200000eff2 thread T0
--
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/g/Work/Code/cJSON/cJSON.c:335 in skip
Shadow bytes around the buggy address:

gdb backtrace is as follows:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Program received signal SIGABRT, Aborted.
0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#0  0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#1  0x00007ffff68c247a in abort () from /usr/lib/libc.so.6
#2  0x00007ffff6f1a0e9 in __sanitizer::Abort () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc:122
#3  0x00007ffff6f0effb in __sanitizer::Die () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_common.cc:145
#4  0x00007ffff6f083f7 in __asan::ScopedInErrorReport::~ScopedInErrorReport (this=<synthetic pointer>, __in_chrg=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:689
#5  __asan::ReportGenericError (pc=<optimized out>, bp=bp@entry=140737488350016, sp=sp@entry=140737488350000, addr=<optimized out>, is_write=is_write@entry=false, access_size=access_size@entry=1, exp=<optimized out>, fatal=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:1074
#6  0x00007ffff6f08ae7 in __asan::__asan_report_load1 (addr=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_rtl.cc:127
#7  0x00007ffff6c3086c in skip (in=0x60200000eff2 "") at /home/g/Work/Code/cJSON/cJSON.c:335
#8  0x00007ffff6c3203f in parse_object (item=0x60600000efc0, value=0x60200000eff0 "{\a", ep=0x7ffff6e375a0 <global_ep>) at /home/g/Work/Code/cJSON/cJSON.c:534
#9  0x00007ffff6c30ed0 in parse_value (item=0x60600000efc0, value=0x60200000eff0 "{\a", ep=0x7ffff6e375a0 <global_ep>) at /home/g/Work/Code/cJSON/cJSON.c:380
#10 0x00007ffff6c30923 in cJSON_ParseWithOpts (value=0x60200000eff0 "{\a", return_parse_end=0x0, require_null_terminated=0) at /home/g/Work/Code/cJSON/cJSON.c:345
#11 0x00007ffff6c30a37 in cJSON_Parse (value=0x60200000eff0 "{\a") at /home/g/Work/Code/cJSON/cJSON.c:354
#12 0x0000000000400a62 in main ()

This issue was found using QuickFuzz, the file to reproduce it is attached.
Regards.

Clean up the cJSON struct

Currently theres three ways the cJSON struct can be improved:

  1. Get rid of valueint
  2. Use a union for valuestring and valuedouble
  3. Using good names for the elements

If you don't think this is a good idea, please tell me why.

Let me explain:

Getting rid of valueint

  • Having both valueint and valuedouble introduces unnecessary complexity in cJSON.
  • Having both valueint and valuedouble increases the chance of misuse by users of the library
  • With an IEEE754 compliant floating point implementation, valueint is not bringing any extra value on platforms that have integers smaller than 32 bit
    • double can store integers up to 53 bits (> 32)
    • even if you have a 64 bit integer, this code prevents it from being useful, because it only works if the integer fits into the double type completely.
    • that means, that even on a floating point implementation that is not IEEE754 compliant, the range of supported numbers is completely bounded by the implementation of double, cJSON will never print an exact value of an integer bigger than the biggest exact integer that can be represented by double
  • The valueint is using between 2-8 bytes that are completely wasted
    • it might be even more because of alignment, if we have a processor that requires 8 byte alignment and has a 64 bit pointer type and a 32 bit integer type, the struct will look like this
      • next: 8 byte
      • prev 8 byte
      • child 8 byte
      • type 4 byte
      • 4 byte padding
      • valuestring 8 byte
      • valueint 4 byte
      • 4 byte padding
      • valuedouble 8 byte
      • string 8 byte
      • even though valueint has only 4 bytes, it takes up 8 bytes in the struct

Using a union of valuestring and valuedouble

As an instance of the cJSON struct can only be either a string type or a number type, not both at the same time. Therefore they can be stored at the same place in memory.

New version of the cJSON struct

typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *next;
    struct cJSON *previous;
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    struct cJSON *child;
    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    char *name;
    union {
        /* The item's string, if type==cJSON_String */
        char *string;
        /* The item's number, if type==cJSON_Number */
        double number;
    }
    /* The type of the item, as above. */
    int type;
} cJSON;
  • renamed prev -> previous
  • valueint removed
  • renamed valuedouble -> number
  • renamed string -> name
  • renamed valuestring -> string
  • put string and number in a union
  • reorder the elements in the struct

Time for a release tag ?

It would be great to tag a release of cjson as it will be easier to reference than a git revision.

Comments on recent patches

I have added some comments on recent patches:
@679004914fceb1c418545fb33c707fb454cbc4bb
@b88da9b0ded73bf63fd11e589b52d9cf1e95f7d8

Please take a look at that.

Make source code readable

Let's face it, cJSON's source code is looking quite horrible in the current form. The way it is it's bad for potential contributors because it is hard to read and it also makes tracking changes with git harder.

I would volunteer to reformat the code if you let me. I will follow the format you like, but here's a suggestion:

Currently:

/* Parse the input text to generate a number, and populate the result into item. */
static const char *parse_number(cJSON *item,const char *num)
{
    double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;

    if (*num=='-') sign=-1,num++;   /* Has sign? */
    if (*num=='0') num++;           /* is zero */
    if (*num>='1' && *num<='9') do  n=(n*10.0)+(*num++ -'0');   while (*num>='0' && *num<='9'); /* Number? */
    if (*num=='.' && num[1]>='0' && num[1]<='9') {num++;        do  n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');}  /* Fractional part? */
    if (*num=='e' || *num=='E')     /* Exponent? */
    {   num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++;      /* With sign? */
        while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0');   /* Number? */
    }

    n=sign*n*pow(10.0,(scale+subscale*signsubscale));   /* number = +/- number.fraction * 10^+/- exponent */

    item->valuedouble=n;
    item->valueint=(int)n;
    item->type=cJSON_Number;
    return num;
}

After reformatting:

/* Parse the input text to generate a number, and populate the result into item. */
static const char *parse_number(cJSON *item,const char *num) {
    double n = 0;
    double sign = 1;
    double scale = 0;
    int subscale = 0;
    int signsubscale = 1;

    /* Has sign? */
    if (*num == '-') {
        sign = -1;
        num++;
    }

    /* is zero */
    if (*num == '0') {
        num++;
    }
    if ((*num >= '1') && (*num <= '9')) {
        do {
            n = (n * 10.0) + (*num++ - '0');
        } while ((*num >= '0') && (*num <= '9')); /* Number? */
    }
    /* Fractional part? */
    if ((*num == '.') && (num[1] >= '0') && (num[1] <= '9')) {
        num++;
        do {
            n = (n * 10.0) + (*num++ - '0');
            scale--;
        } while ((*num >= '0') && (*num <= '9'));
    }
    /* Exponent? */
    if ((*num == 'e') || (*num == 'E')) {
        num++;
        /* With sign? */
        if (*num == '+') {
            num++;
        } else if (*num == '-') {
            signsubscale = -1;
            num++;
        }
        while ((*num >= '0') && (*num <= '9')) {/* Number? */
            subscale = (subscale * 10) + (*num++ - '0');
        }
    }

    /* number = +/- number.fraction * 10^+/- exponent */
    n = sign * n * pow(10.0, (scale + subscale * signsubscale));

    item->valuedouble = n;
    item->valueint = (int) n;
    item->type = cJSON_Number;
    return num;
}

Does not compile using the Makefile with GCC 6.2.1

The makefile uses -Wall -Werror which makes the build fail because of warnings about misleading indentation. This should be fixed by fixing the format of the code, see #24.

$ gcc --version
gcc (GCC) 6.2.1 20160830
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ make
cc -ansi -pedantic -c -fpic  -Wall -Werror -Wstrict-prototypes -Wwrite-strings -D_POSIX_C_SOURCE=200112L cJSON.c
cJSON.c: In function ‘cJSON_strcasecmp’:
cJSON.c:41:2: error: this ‘if’ clause does not guard... [-Werror=misleading-indentation]
  if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
  ^~
cJSON.c:41:31: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
  if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
                               ^~
cJSON.c: In function ‘print_object’:
cJSON.c:602:4: error: this ‘if’ clause does not guard... [-Werror=misleading-indentation]
    if (fmt) *ptr++='\n';*ptr=0;
    ^~
cJSON.c:602:25: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
    if (fmt) *ptr++='\n';*ptr=0;
                         ^
cJSON.c:652:4: error: this ‘if’ clause does not guard... [-Werror=misleading-indentation]
    if (fmt) *ptr++='\n';*ptr=0;
    ^~
cJSON.c:652:25: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
    if (fmt) *ptr++='\n';*ptr=0;
                         ^
cJSON.c: In function ‘cJSON_DetachItemFromArray’:
cJSON.c:682:2: error: this ‘if’ clause does not guard... [-Werror=misleading-indentation]
  if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
  ^~
cJSON.c:682:37: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
  if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
                                     ^~
cc1: all warnings being treated as errors
make: *** [Makefile:47: cJSON.o] Error 1

pow2gt: make it work with size_t

Because C89 doesn't define the SIZE_MAX constant, using a macro as in #27 for determining the size doesn't work anymore. It has to be done in another way.

I think the best option is a runtime check, that a sane compiler should optimize out though:

/* calculate the next largest power of 2 */
static size_t pow2gt (size_t x)
{
    --x;

    switch (sizeof(x))
    {
        case 8:
            x |= x >> 1;
            x |= x >> 2;
            x |= x >> 4;
            x |= x >> 8;
            x |= x >> 16;
            x |= x >> 32;
            break;

        case 4:
            x |= x >> 1;
            x |= x >> 2;
            x |= x >> 4;
            x |= x >> 8;
            x |= x >> 16;
            break;

        case 2:
            x |= x >> 1;
            x |= x >> 2;
            x |= x >> 4;
            x |= x >> 8;
            break;

        default:
            assert(0);
    }

    return x + 1;
}

I expect a compiler to replace sizeof(x) with the correct number and determining that all the other branches in the switch case are dead code and then removing them as well as the branch altogether. Then the generated code has the same performance as with conditional compilation based on macros. I should test this claim though.

Run error in Windows 64bit

test.c:
dofile("../../tests/test1");
dofile("../../tests/test2");
dofile("../../tests/test3");
dofile("../../tests/test4");
dofile("../../tests/test5");
dofile("../../tests/test6");
Error:
dofile-bug

Implement new 'safe` versions of the functions provided by cJSON.

For every function in cJSON.h that takes a string or returns one or allocates memory. Let's create a new function cJSON_..._s that takes/returns size arguments and gets a pointer to an instance of cJSON_Hooks, see #57 and #5.

By doing this, the current functions could still remain, but be wrappers to the safer alternatives. Once the handling of size arguments are implemented, the wrappers would need to get it using strlen.

Examples:

cJSON *cJSON_Parse_s(const char *value, size_t length, const cJSON_Hooks * const allocators);
char *cJSON_Print_s(const cJSON *item, size_t * const printed_length, const cJSON_Hooks * const allocators);

the valueint,valuedouble always get 0 on PowerPC

env:
system: Freescale T4240
QorIQ SDK: QorIQ-SDK-V1.6-20140619-yocto
linux: Linux t4240qds 3.12.19-rt30-QorIQ-SDK-V1.6+gc29fe1a #5 SMP Thu Jan 15 02:16:53 UTC 2015 ppc64 GNU/Linux

problem:
I use cJSON on a T4240 machine.the code like this:

      cJSON* fs = cJSON_GetObjectItem(context, "frameSize"); 
      printf("val: %d %f\n", fs->valueint, fs->valuedouble);

always print:
val: 0 0.000000f
but the code run very well on my PC(GentooLinux, intel I7).

resolve:
modify cJSON.h, add "#pragma pack(1)" before define cJSON struct:

#pragma pack(1)
/* The cJSON structure: */
typedef struct cJSON {

but I don't know why it happen.

cJSON buffer overflow with odd formatted JSON string

Hi,

I would like to report a buffer overflow in cJSON, you can test this PoC with a ODD string

#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include "cJSON.h"

static const char *my_json = "\"000000000000000000\\";

int main(int argc, const char * argv[]) {
    cJSON * root = cJSON_Parse(my_json);
    char * rendered = cJSON_Print(root);
    printf("%s\n", rendered);
    return 0;
}

building it with ASAN will trigger a out of bound on the input string, which it can be problematic since in lot of use cases the JSON input to applications using cJSON is untrusted.

Thanks

Marco

==72479==ERROR: AddressSanitizer: global-buffer-overflow on address 0x0001000138d5 at pc 0x0001000062fb bp 0x7fff5fbff2a0 sp 0x7fff5fbff298
READ of size 1 at 0x0001000138d5 thread T0
#0 0x1000062fa in parse_string cJSON.c:198
#1 0x100001a01 in parse_value cJSON.c:366
#2 0x1000015bb in cJSON_ParseWithOpts cJSON.c:334
#3 0x100001dfa in cJSON_Parse cJSON.c:343
#4 0x10000e481 in main main.c:17
#5 0x7fff834bb5ac in start (libdyld.dylib+0x35ac)

0x0001000138d5 is located 0 bytes to the right of global variable '' defined in '/Users/marco/Downloads/cjsontest/cjsontest/main.c:14:30' (0x1000138c0) of size 21
'' is ascii string '"000000000000000000'
SUMMARY: AddressSanitizer: global-buffer-overflow cJSON.c:198 in parse_string
Shadow bytes around the buggy address:
0x1000200026c0: 02 f9 f9 f9 f9 f9 f9 f9 03 f9 f9 f9 f9 f9 f9 f9
0x1000200026d0: 05 f9 f9 f9 f9 f9 f9 f9 03 f9 f9 f9 f9 f9 f9 f9
0x1000200026e0: 03 f9 f9 f9 f9 f9 f9 f9 03 f9 f9 f9 f9 f9 f9 f9
0x1000200026f0: 00 f9 f9 f9 f9 f9 f9 f9 06 f9 f9 f9 f9 f9 f9 f9
0x100020002700: 03 f9 f9 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
=>0x100020002710: 04 f9 f9 f9 f9 f9 f9 f9 00 00[05]f9 f9 f9 f9 f9
0x100020002720: 00 00 00 00 00 00 00 00 01 f9 f9 f9 f9 f9 f9 f9
0x100020002730: 06 f9 f9 f9 f9 f9 f9 f9 03 f9 f9 f9 f9 f9 f9 f9
0x100020002740: 05 f9 f9 f9 f9 f9 f9 f9 04 f9 f9 f9 f9 f9 f9 f9
0x100020002750: 07 f9 f9 f9 f9 f9 f9 f9 00 f9 f9 f9 f9 f9 f9 f9
0x100020002760: 05 f9 f9 f9 f9 f9 f9 f9 05 f9 f9 f9 f9 f9 f9 f9
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Heap right redzone: fb
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack partial redzone: f4
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==72479==ABORTING

Breaking changes in 2.0.0

With 2.0.0 let's try to get things right with the following breaking changes:

  • Use size_t instead of int for lengths
  • Clean up the cJSON struct #63
  • Remove cJSON_InitHooks in favor of #5
  • Remove cJSON_GetErrorPtr in favor of #59
  • Maybe remove cJSON_CreateIntArray, cJSON_CreateFloatArray and cJSON_CreateDoubleArray in favor of a cJSON_CreateNumberArray macro???
  • Use case sensitive string comparison #13
  • Maybe add realloc support. (fall back to malloc if not specified available)
  • Return value for cJSON_AddItemToObject and other void functions #107
  • error position instead of error pointer
  • Allow allocator functions that take a void * pointer with user data that get's passed through.
  • cJSON_PrintPreallocated: Return size and use unsigned char*

heap-buffer-overflow on parse_value

Hello, we recently found an invalid memory access parsing JSON in cJSON (rev 94df772).
We tested this issue on ArchLinux (x86_64) but other configurations could be affected.
Technical details about the issue are:

==7786==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000eff3 at pc 0x7fd8e6b174db bp 0x7ffdba02adc0 sp 0x7ffdba02a568
READ of size 4 at 0x60200000eff3 thread T0
--
SUMMARY: AddressSanitizer: heap-buffer-overflow /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:251 in __interceptor_strncmp
Shadow bytes around the buggy address:

gdb backtrace is as follows:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Program received signal SIGABRT, Aborted.
0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#0  0x00007ffff68c104f in raise () from /usr/lib/libc.so.6
#1  0x00007ffff68c247a in abort () from /usr/lib/libc.so.6
#2  0x00007ffff6f1a0e9 in __sanitizer::Abort () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_posix_libcdep.cc:122
#3  0x00007ffff6f0effb in __sanitizer::Die () at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_common.cc:145
#4  0x00007ffff6f083f7 in __asan::ScopedInErrorReport::~ScopedInErrorReport (this=<synthetic pointer>, __in_chrg=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:689
#5  __asan::ReportGenericError (pc=<optimized out>, bp=bp@entry=140737488350112, sp=sp@entry=140737488347976, addr=addr@entry=105690555281395, is_write=is_write@entry=false, access_size=access_size@entry=4, exp=<optimized out>, fatal=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/asan/asan_report.cc:1074
#6  0x00007ffff6e7c4fa in __interceptor_strncmp (s1=<optimized out>, s2=0x7ffff6c35d20 "null", size=<optimized out>) at /build/gcc-multilib/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:251
#7  0x00007ffff6c30bf8 in parse_value (item=0x60600000efc0, value=0x60200000eff0 "nul", ep=0x7ffff6e375a0 <global_ep>) at /home/g/Work/Code/cJSON/cJSON.c:374
#8  0x00007ffff6c30923 in cJSON_ParseWithOpts (value=0x60200000eff0 "nul", return_parse_end=0x0, require_null_terminated=0) at /home/g/Work/Code/cJSON/cJSON.c:345
#9  0x00007ffff6c30a37 in cJSON_Parse (value=0x60200000eff0 "nul") at /home/g/Work/Code/cJSON/cJSON.c:354
#10 0x0000000000400a62 in main ()

This issue was found using QuickFuzz, the file to reproduce it is attached.
Regards.

Static and dynamic are always built

Currently cjson always builds the static and the dynamic version of the cjson and cjson_utils libraries? This is not usual as most of the time, packages only build the static or the dynamic version depending on compilation option (for example, we can control it through the CMAKE configuration option BUILD_SHARED_LIBS). Is it intentional?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.