On Tue, Sep 27, 2011 at 8:03 PM, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote: > On 28 September 2011 00:41, Jeffrey Walton <noloader@xxxxxxxxx> wrote: >> Hi All, >> >> What is the preferred way to tame the optimizer when two class >> functions are producing incorrect results? > > Fix your code ;-) That's what I thought, too. I'm kind of surprised that I haven't gotten a warning out of any of the tools (I run with maximum on all platforms). Below is a brief version of what the declarations and definitions look like. Obviously, the ASSERTs are removed in the 'release' configuration. The best I can tell, the call to "m_vector->insert(pos, first, last);" is not being made, but I could be wrong. We are using LeBlanc's SafeInt, which itself uses unsigned math (so there should be no undefined behavior). But I don't believe the SafeInts are being used on the code path leading to the unexpected behavior. Oh, and trying to use GDB to debug this is really fun. Sometimes I think GDB is guessing where it might be after a 'si'. >> There's not much to the class - its a vector with copy semantics. So >> it mirrors all vector's public functions, and forwards calls through >> the internal vector pointer. The pointer is a boost shared_ptr. >> >> -O2 is giving problems. -O0 is OK, Intel ICC is OK, and Visual Studio is OK. > > As suggested at http://gcc.gnu.org/bugs/ you can try > -fno-strict-aliasing and -fwrapv, if they help your code is broken. I actually decided against -fwrapv because I did not want to affect all compilation units. > You can also turn off other individual optimisations with -fno-foobar, > see the manual for the various optimizations that are active at -O1 > and -O2 I was thinking __attribute__((optimize(0))) for the two functions in question. Is it available in modern GCC versions (ie, 4.0 and above)? Jeff template <typename T> class SecureArray { public: typedef typename std::vector< T, zallocator<T> > SecureVector; typedef typename zallocator<T>::size_type size_type; typedef typename SecureVector::value_type value_type; ... // One of the problem functions void insert(iterator pos, InputIterator first, InputIterator last); private: boost::shared_ptr<SecureVector> m_vector; } template <typename T> template <class InputIterator> void SecureArray<T>::insert(iterator pos, InputIterator first, InputIterator last) { ASSERT2(first, "Bad first input iterator"); ASSERT2(first >= last, "Input iterators are not valid"); if(!(first >= last)) throw std::length_error("Input iterators are not valid"); // Not sure what the conatiner does here.... ASSERT2(first % sizeof(T) == 0, "InputIterator first slices elements"); ASSERT2(last % sizeof(T) == 0, "InputIterator last slices elements"); ASSERT1(m_vector.get()); m_vector->insert(pos, first, last); }