opendroneid / opendroneid-core-c

Open Drone ID Core C Library
Apache License 2.0
170 stars 64 forks source link

Parsing failure will generate invalid messages #67

Closed MauroMombelli closed 1 year ago

MauroMombelli commented 1 year ago

The function odid_message_build_pack will call different enconding call like 'encodeBasicIDMessage', 'encodeLocationMessage' and so on. The problem is, those encode function CAN fail, and if they do, the error is ignored

Current code:

if (UAS_Data->LocationValid) {
    if (msg_pack.MsgPackSize >= ODID_PACK_MAX_MESSAGES)
        return -EINVAL;
    encodeLocationMessage((void *)&msg_pack.Messages[msg_pack.MsgPackSize], &UAS_Data->Location);
    msg_pack.MsgPackSize++;
}

The quick and proper fix is very simple, we do to NOT increment msg_pack.MsgPackSize on failure:

if (UAS_Data->LocationValid) {
    if (msg_pack.MsgPackSize >= ODID_PACK_MAX_MESSAGES)
        return -EINVAL;
    if (encodeLocationMessage((void *)&msg_pack.Messages[msg_pack.MsgPackSize], &UAS_Data->Location) == ODID_SUCCESS)
        msg_pack.MsgPackSize++;
}

this will make sure that the invalid message will be override by the next message, or ignored if last one

friissoren commented 1 year ago

Thanks for noticing. PR opened. Feel free to review.