equinor / equinor/resdata

rd_kw_struct uses `char *data` which is UB.

Open
#1,087 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
127
Forks
103
Avg merge
2d 5h
Merged PRs (30d)
18

Description

The `rd_kw_struct` uses `char *data` to store different types (int, float, double, bool), then casts it to typed pointers:

https://github.com/equinor/resdata/blob/091b69598571e9c9869d1d5a4ab6b4078bdfb52d/lib/resdata/rd_kw.cpp#L979

Compilers assume pointers of different types don't alias, enabling aggressive optimizations that can break the code.

It should either
* Use std::aligned_storage and placement new
* Use a union
* Compile with `-fno-strict-aliasing`

We should probably also run tests with `-fsanitize=undefined -fsanitize=address`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.