Skip to content

Parse error when arrays are empty #46

@mboelstra

Description

@mboelstra

Hi,

The parser fails when the following JSON is provided: {"myArray": [] }

In code:
static char str[] = "{ "myArray": [] }";
json_value * root = json_parse(str, strlen(str));

This is because malloc is called with a size of 0, due to the empty array. However some implementations of malloc will return NULL when asked to allocate 0 bytes and in this case the parser stops. I think this is wrong. In my opinion the parser should not try to allocate 0 bytes. A simple check in the newValue method for the json_array case can prevent this:

In code:
if (value->u.array.length == 0) {
break;
}

With these three lines of code the parser can also handle the above input.

Regards,
Matthijs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions