Skip to content

libiso15118: Reduce the usage of std::vector #1678

@Dalachowsky

Description

@Dalachowsky

Describe the problem

Inside libiso15118 std::vector is extensively used to represent arrays of iso15118 messages. During conversion from libiso15118 to libcbv2g structs there is no bounds checking introduced. For me this is a bit of a red flag.
All of the arrays inside iso15118 have fixed maximum sizes, so there is no need of using std::vector to represent them. std::vector relies on dynamic allocation so it can be problematic if libiso15118 was to be used on an mcu, which I know that @razvanphp wants to do.

EVerest Domain

ISO15118

Affected EVerest Module

libiso15118

Describe your solution

We could use etl::vector from the etl library. It introduces the SIZE parameter to the template and throws etl::vector_full if the size is exceeded. I think that this is the best solution if we consider using libiso15118 on an mcu. However this introduces another dependency for libiso15118.
Another solution would be to just introduce bounds checking in the convert() functions.

Additional context

Example of this case would be the ServiceList

struct Service {
    ServiceCategory service_id;
    bool free_service;
};
using ServiceList = std::vector<Service>; // max: 8

It is stated in the comment that the max size is 8, but nothing prevents you from exceeding this limit. Inside the convert() function to iso20_ struct the conversion looks like this:

    uint8_t index = 0;
    for (const auto& service : in.energy_transfer_service_list) {
        auto& out_service = out.EnergyTransferServiceList.Service.array[index++];
        cb_convert_enum(service.service_id, out_service.ServiceID);
        out_service.FreeService = service.free_service;
    }
    out.EnergyTransferServiceList.Service.arrayLen = in.energy_transfer_service_list.size();

The array inside iso20_ struct has a fixed size of 8, so the code above can access it out of bounds.

struct iso20_ServiceListType {
    // Service, ServiceType
    struct {
        struct iso20_ServiceType array[iso20_ServiceType_8_ARRAY_SIZE];
        uint16_t arrayLen;
    } Service;
};

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