OpenCyphal/libcanard

Typical usage of the library should not require casting away const qualifiers

pavel-kirienko opened this issue · 0 comments

@thirtytwobits says:

the CanardTransfer payload is const void* but is <...> used to free the payload when you are done with it. This leads to const_cast<void*>(transfer->payload) in C++ which is not something you ever want to see. const is part of the API so if you violate the constness you are, technically, entering undefined behaviour.

to which I say:

This is a known issue, yes: https://github.com/UAVCAN/libcanard/blob/2a116170285fb47fcaae150ad21c2ccde0756a5f/libcanard/canard.h#L251-L252
The reason the deallocation function accepts a mutable void* is because the free() from the standard library is defined this way.
We could make it const void* but then if you are using the standard heap or pretty much any other heap manager implemented in C (like o1heap), you will have to cast away your const anyway before calling the deallocation function.
Making o1heapFree() accept a const void would require it to cast away the const also because it has to mutate the returned memory fragment.

Fixing this requires breaking API changes so it is postponed until v2.0.