Skip to content

loadmsgpack fails on empty arrays #63

Closed
@stfnp

Description

@stfnp

The function loadmsgpack fails on files that contain an empty array. I've attached an example file for testing here. It's a MessagePack file with a single field "test" that contains an empty array. The equivalent JSON would look like this:

{
    "test": []
}

Trying to load this file with loadmsgpack('test.msg') fails with the following error:

Index exceeds the number of array elements (0).

Error in loadmsgpack>parsearray (line 225)
    if(isnumeric(out{1}))

Error in loadmsgpack>parse (line 90)
    [obj, idx] = parsearray(len, bytes, idx+1, varargin{:});

Error in loadmsgpack>parsemap (line 243)
    [out.(encodevarname(char(key))), idx] = parse(bytes, idx, varargin{:});

Error in loadmsgpack>parse (line 85)
    [obj, idx] = parsemap(len, bytes, idx+1, varargin{:});

Error in loadmsgpack (line 39)
    [obj, idx] = parse(uint8(bytes(:)), 1, opt);

It seems like the parsearray sub-function doesn't expect an empty array and fails when trying to access the first element. Inserting a simple length check, i.e. changing line 225 of loadmsgpack.m from if(isnumeric(out{1})) to if(len ~= 0 && isnumeric(out{1})) seems to fix the problem.

However, I think there is also an underlying inconsistency in the handling of empty arrays by JSONLab. I've noticed that loadjson and savejson can actually read empty arrays without problems (they map to a 0x1 cell) and also serialize them back as empty arrays. The function savemsgpack however serializes empty arrays as null instead of []. And that might be the reason why loadmsgpack doesn't expect empty arrays and can't handle them.

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