public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/39480]  New: generated memcpy causes trouble in assignment
@ 2009-03-17 10:55 fpbeekhof at gmail dot com
  2009-03-17 10:59 ` [Bug libstdc++/39480] " rguenth at gcc dot gnu dot org
                   ` (29 more replies)
  0 siblings, 30 replies; 31+ messages in thread
From: fpbeekhof at gmail dot com @ 2009-03-17 10:55 UTC (permalink / raw)
  To: gcc-bugs

The following code is unsafe, and I think it's not my fault.

$ g++ -Wall -g testIterSwap.cc ; valgrind -q ./a.out 
==1674== Source and destination overlap in memcpy(0x7FF000500, 0x7FF000500, 20)
==1674==    at 0x4C2508B: memcpy (mc_replace_strmem.c:402)
==1674==    by 0x4008D2: void swap_from_glibcxx<HarrisPoint<float, int>
>(HarrisPoint<float, int>&, HarrisPoint<float, int>&) (testIterSwap.cc:22)
==1674==    by 0x400793: main (testIterSwap.cc:68)

It would appear that the auto-generated operator=() uses a memcpy() which is
not safe. The operator=() that is provided below solves the problem.

-------- cut here ------------

#include <tr1/array>
// #include <algorithm>


 /**
   *  @brief Swaps two values.
   *  @param  a  A thing of arbitrary type.
   *  @param  b  Another thing of arbitrary type.
   *  @return   Nothing.
   *
   *  This is the simple classic generic implementation.  It will work on
   *  any type which has a copy constructor and an assignment operator.
  */
  template<typename _Tp>
    inline void
    swap_from_glibcxx(_Tp& __a, _Tp& __b)
    {
      // concept requirements
//      __glibcxx_function_requires(_SGIAssignableConcept<_Tp>)

      _Tp __tmp = __a;
      __a = __b;
      __b = __tmp;
    }



template <typename T>
struct Point2D :  public std::tr1::array<T, 2>
{
        template <typename U, typename V>
        Point2D(const U x, const V y)
        {
                (*this)[0] = x;
                (*this)[1] = y;
        }
};

template <typename T, typename U>
class FeaturePoint : public Point2D<U>
{
        public:
                typedef T data_type;

                FeaturePoint(const U h, const U w) : Point2D<U>(h,w) {}
};

template <typename T, typename U>
class HarrisPoint : public FeaturePoint<T, U>
{
        public:
                typedef T data_type;

                HarrisPoint(const U h, const U w, const T value) :
                        FeaturePoint<T, U>(h, w), value_(value) { }
/*
                // THIS FIXES IT.
                HarrisPoint &operator=(const HarrisPoint &rhs)
                {
                        std::memmove(this, &rhs, sizeof(HarrisPoint));
                        return *this;
                }
*/
        private:
                T value_;
};

int main()
{
        HarrisPoint<float, int> a(1, 2, 3.0f);
//      std::tr1::array<int, 2> a; a[0] = 3; a[1] = 5; // fine

        a = a; // fine
//      std::swap(a, a);
        swap_from_glibcxx(a, a);

        return 0;
}


-- 
           Summary: generated memcpy causes trouble in assignment
           Product: gcc
           Version: 4.2.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fpbeekhof at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39480


^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2009-04-14  3:31 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-17 10:55 [Bug libstdc++/39480] New: generated memcpy causes trouble in assignment fpbeekhof at gmail dot com
2009-03-17 10:59 ` [Bug libstdc++/39480] " rguenth at gcc dot gnu dot org
2009-03-17 11:05 ` paolo dot carlini at oracle dot com
2009-03-17 11:13 ` fpbeekhof at gmail dot com
2009-03-17 11:15 ` fpbeekhof at gmail dot com
2009-03-17 11:28 ` paolo dot carlini at oracle dot com
2009-03-17 12:10 ` falk at debian dot org
2009-03-17 12:21 ` fpbeekhof at gmail dot com
2009-03-17 12:33 ` paolo dot carlini at oracle dot com
2009-03-17 13:11 ` fpbeekhof at gmail dot com
2009-03-17 13:15 ` paolo dot carlini at oracle dot com
2009-03-17 13:19 ` fpbeekhof at gmail dot com
2009-03-17 13:25 ` paolo dot carlini at oracle dot com
2009-03-17 13:26 ` paolo dot carlini at oracle dot com
2009-03-17 13:29 ` fpbeekhof at gmail dot com
2009-03-17 13:32 ` paolo dot carlini at oracle dot com
2009-03-17 13:34 ` paolo dot carlini at oracle dot com
2009-03-17 13:35 ` [Bug c++/39480] " paolo dot carlini at oracle dot com
2009-03-18  3:29 ` jason at gcc dot gnu dot org
2009-03-18  9:07 ` falk at debian dot org
2009-03-18  9:10 ` jakub at gcc dot gnu dot org
2009-03-18  9:47 ` rguenth at gcc dot gnu dot org
2009-03-18  9:56 ` fpbeekhof at gmail dot com
2009-03-18 10:05 ` fpbeekhof at gmail dot com
2009-03-18 10:11 ` jakub at gcc dot gnu dot org
2009-04-09 16:05 ` jason at gcc dot gnu dot org
2009-04-13 20:08 ` jason at gcc dot gnu dot org
2009-04-13 20:53 ` jason at gcc dot gnu dot org
2009-04-13 20:57 ` jason at gcc dot gnu dot org
2009-04-14  3:30 ` jason at gcc dot gnu dot org
2009-04-14  3:31 ` jason at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).