Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GLIBCXX_ASSERTIONS to recommended compiler flags #158

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ryanf55
Copy link

@Ryanf55 Ryanf55 commented Jan 20, 2024

When migrating legacy code from uint8_t my_arr[N] to std::array<uint8_t, N>, there are places where bracket access is used. This does not perform bounds checking, even at compile time.

For example, you could construct an array with 6 elements, then access element 42 at runtime with no errors, even with all the flags enabled that are currently recommended.
https://godbolt.org/z/3KWqe1vbs

Even if you set -Weverything and compile in clang, it's not caught.

I figured out you can enable bounds checking on bracket access with GLIBCXX_ASSERTIONS
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html

There are runtime costs with bounds checking, so it should not be enabled in production, however this would be great flag to add to a debug build that is tested in CI.

With this enabled in debug mode, you seem to get the best of both worlds.

#include <array>
#include <iostream>

int main() {

    std::array<double, 6> a;
    std::cout << "The UB value of a is " << a[42] << std::endl;
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant