Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC 7, aligned_storage and "dereferencing type-punned pointer will break strict-aliasing rules"

Tags:

A code that I wrote was warning-free in GCC 4.9, GCC 5 and GCC 6. It was also warning-free with some older GCC 7 experimental snapshots (for example 7-20170409). But in the most recent snapshot (including the first RC), it started to produce a warning about aliasing. The code basically boils down to this:

#include <type_traits>

std::aligned_storage<sizeof(int), alignof(int)>::type storage;

int main()
{
    *reinterpret_cast<int*>(&storage) = 42;
}

Compilation with latest GCC 7 RC:

$ g++ -Wall -O2 -c main.cpp
main.cpp: In function 'int main()':
main.cpp:7:34: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
  *reinterpret_cast<int*>(&storage) = 42;

(interesting observation is that the warning is not produced when optimizations are disabled)

Compilation with GCC 6 gives no warnings at all.

Now I'm wondering, the code above definitely HAS type-punning, no question about that, but isn't std::aligned_storage meant to be used that way?

For instance the example code given here generally produces no warning with GCC 7 but only because:

  • std::string somehow is not affected,
  • std::aligned_storage is accessed with an offset.

By changing std::string into int, removing offset access to std::aligned_storage and removing irrelevant parts you get this:

#include <iostream>
#include <type_traits>
#include <string>

template<class T, std::size_t N>
class static_vector
{
    // properly aligned uninitialized storage for N T's
    typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];
    std::size_t m_size = 0;

public:

    // Access an object in aligned storage
    const T& operator[](std::size_t pos) const
    {
        return *reinterpret_cast<const T*>(data/*+pos*/); // <- note here, offset access disabled
    }
};

int main()
{
    static_vector<int, 10> v1;
    std::cout << v1[0] << '\n' << v1[1] << '\n';
}

And this produces exactly the same warning:

main.cpp: In instantiation of 'const T& static_vector<T, N>::operator[](std::size_t) const [with T = int; unsigned int N = 10; std::size_t = unsigned int]':
main.cpp:24:22:   required from here
main.cpp:17:16: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
         return *reinterpret_cast<const T*>(data/*+pos*/);
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So my question is - is this a bug or a feature?

like image 304
Freddie Chopin Avatar asked Apr 30 '17 21:04

Freddie Chopin


1 Answers

I can't answer whether or not there really is a potential for undefined behavior due to aliasing or if the warning is unwarranted. I find the aliasing topic to be a rather complex minefield.

However, I think that the following variation of your code eliminates the aliasing problem without any overhead (and perhaps is more readable).

#include <iostream>
#include <type_traits>
#include <string>

template<class T, std::size_t N>
class static_vector
{
    // properly aligned uninitialized storage for N T's
    union storage_t_ {
        T item;
        typename std::aligned_storage<sizeof(T), alignof(T)>::type aligned_member;
    };
    storage_t_ data[N];

    std::size_t m_size = 0;

public:

    // Access an object in aligned storage
    const T& operator[](std::size_t pos) const
    {
        return data[0].item;
    }
};

int main()
{
    static_vector<int, 10> v1;
    std::cout << v1[0] << '\n' << v1[1] << '\n';
}

Whether it's acceptable for your situation, I can't be sure.

like image 150
Michael Burr Avatar answered Sep 25 '22 09:09

Michael Burr