Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBORATTR_STRUCT_ARRY and CBORATTR_STRUCT_OBJECT not suitable with CborAttrByteStringType #78

Open
Cimex97 opened this issue Apr 21, 2020 · 0 comments

Comments

@Cimex97
Copy link

Cimex97 commented Apr 21, 2020

Hello,
I Want to parse a CBOR Payload into an own struct image[].
The CBOR Payload looks like that:

{
    "images": [{
       "slot": 0, 
       "version": "1.6.0",
       "hash": h'C0273A8B3D307B1D5607F52617F3A08B29582AE56D4C1763929A70BEF31685BD', 
       "bootable": true, 
       "pending": false, 
       "confirmed": true, 
       "active": true, 
       "permanent": false
    }], 
    "splitStatus": 0
}

I try to use the macros CBORATTR_STRUCT_ARRAY and CBORATTR_STRUCT_OBJECT
with different object types (This is based on this testcase):

struct image arr_objs[2] = {{0},{0}};

    int array_cnt;
    size_t hash_len= 0;

   struct cbor_attr_t sub_attr[] = {
        [0] = {
            .attribute = "slot",
            .type = CborAttrIntegerType,
            CBORATTR_STRUCT_OBJECT(struct image, slot),
            .nodefault = true,
         },
        [1] = {
            .attribute = "version",
            .type = CborAttrTextStringType,
            .len = sizeof(arr_objs[0].version),
            CBORATTR_STRUCT_OBJECT(struct image, version),
            .nodefault = true,
        },                
        [2] = {
            .attribute = "hash",
            .type = CborAttrByteStringType,
            .len = sizeof(arr_objs[0].hash),
            CBORATTR_STRUCT_OBJECT(struct image, hash),
            .nodefault = true,
        },
    …
    }


    struct cbor_attr_t test_attrs[] = {
        [0] = {
            .attribute = "images",
            .type = CborAttrArrayType,
            CBORATTR_STRUCT_ARRAY(arr_objs, sub_attr, &array_cnt),
            .nodefault = true
        },
        [1] = {
            .attribute = NULL
        }
    };
    int rc = cbor_read_object(&it, test_attrs);
…

The struct image:

struct image {
    int slot;
    char version[32];
    uint8_t hash[32];
    bool bootable;
    bool pending;
    bool confirmed;
    bool active;
    bool permanent;
};

The function: cbor_read_object calls always *cursor->addr.bytestring.len = len; so that I get a nullptr exception. If I understand that correctly, the function can not use .addr.bytestring.* if a parent is present with a type of CborAttrStructObjectType.

To solve this problem I added a if statement:

if (parent == NULL || parent->element_type != CborAttrStructObjectType) {
    *cursor->addr.bytestring.len = len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant