Skip to content

[Bug]: Undefined behavior in DataSpan #663

@VzdornovNA88

Description

@VzdornovNA88

return ptr[index * sizeof(T)]; - This is about incorrect use of pointer arithmetic in C++(under the standard) and also from the point of view of common sense if you have an int array with 4 elements , then this formula will give you the result of 4 at index 1 , which will lead to access to memory that does not belong to the object.

This issue did not manifest because the only place in the code where this type is used is its specialization for uint8_t(using CANDataSpan = DataSpan;), which fortunately happens to work (I think it's obvious why).

Since span has been in the standard for quite some time now, I'm providing a link to the standard here, as the current implementation suffers from a non-idiomatic approach beyond just this particular issue.

I'm also providing a link to the implementation of this type in the ETL library, but generally speaking, there are implementations available in standard libraries as well.

This issue serves as yet another reminder that tests for basic components are essential—and currently, they are missing.

If no one takes this on, I'll fix it myself later.

https://eel.is/c++draft/views.contiguous#views.span
https://github.com/ETLCPP/etl/blob/558c04b0a0d6714a319e99f50d89048e5686c9d2/include/etl/span.h#L137

Metadata

Metadata

Assignees

No one assigned

    Labels

    investigatingLooking into this issue / need more info

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions