OSDN Git Service

2007-01-12 Paolo Carlini <pcarlini@suse.de>
authorpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 12 Jan 2007 11:09:26 +0000 (11:09 +0000)
committerpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 12 Jan 2007 11:09:26 +0000 (11:09 +0000)
PR libstdc++/30416
* include/std/valarray (valarray<>::shift, valarray<>::cshift):
Do not segfault when |n| > size.
* testsuite/26_numerics/valarray/30416.cc: New.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@120720 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/valarray
libstdc++-v3/testsuite/26_numerics/valarray/30416.cc [new file with mode: 0644]

index a60678f..6bfbcc1 100644 (file)
@@ -1,3 +1,10 @@
+2007-01-12  Paolo Carlini  <pcarlini@suse.de>
+
+       PR libstdc++/30416
+       * include/std/valarray (valarray<>::shift, valarray<>::cshift):
+       Do not segfault when |n| > size.
+       * testsuite/26_numerics/valarray/30416.cc: New.
+
 2007-01-06  Paolo Carlini  <pcarlini@suse.de>
 
        PR libstdc++/30365
index 91b1637..95f2701 100644 (file)
@@ -782,34 +782,33 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
      inline valarray<_Tp>
      valarray<_Tp>::shift(int __n) const
      {
+       if (_M_size == 0 || __n == 0)
+        return *this;
+     
        valarray<_Tp> __ret;
        _Tp* __restrict__ __tmp_M_data =
         std::__valarray_get_storage<_Tp>(_M_size);
 
-       if (__n == 0)                          // no shift
-         std::__valarray_copy_construct(_M_data, _M_data + _M_size,
-                                       __tmp_M_data);
-       else if (__n > 0)         // __n > 0: shift left
-         {                 
-           if (size_t(__n) > _M_size)
-             std::__valarray_default_construct(__tmp_M_data,
-                                              __tmp_M_data + __n);
-           else
-             {
-               std::__valarray_copy_construct(_M_data + __n,
-                                             _M_data + _M_size,
-                                             __tmp_M_data);
-               std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
-                                                __tmp_M_data + _M_size);
-             }
-         }
-       else                        // __n < 0: shift right
-         {                          
-           std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
+       if (__n > 0)      // shift left
+        {
+          if (size_t(__n) > _M_size)
+            __n = _M_size;
+
+          std::__valarray_copy_construct(_M_data + __n,
+                                         _M_data + _M_size, __tmp_M_data);
+          std::__valarray_default_construct(__tmp_M_data + _M_size - __n,
+                                            __tmp_M_data + _M_size);
+        }
+       else              // shift right
+        {
+          if (size_t(-__n) > _M_size)
+            __n = -_M_size;
+
+          std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
                                          __tmp_M_data - __n);
-           std::__valarray_default_construct(__tmp_M_data,
+          std::__valarray_default_construct(__tmp_M_data,
                                             __tmp_M_data - __n);
-         }
+        }
 
        __ret._M_size = _M_size;
        __ret._M_data = __tmp_M_data;
@@ -820,27 +819,33 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
      inline valarray<_Tp>
      valarray<_Tp>::cshift(int __n) const
      {
+       if (_M_size == 0 || __n == 0)
+        return *this;
+
        valarray<_Tp> __ret;
        _Tp* __restrict__ __tmp_M_data =
         std::__valarray_get_storage<_Tp>(_M_size);
+       
+       if (__n > 0)      // cshift left
+        {
+          if (size_t(__n) > _M_size)
+            __n = __n % _M_size;
 
-       if (__n == 0)               // no cshift
-         std::__valarray_copy_construct(_M_data, _M_data + _M_size,
-                                       __tmp_M_data);
-       else if (__n > 0)           // cshift left
-         {               
-           std::__valarray_copy_construct(_M_data, _M_data + __n,
+          std::__valarray_copy_construct(_M_data, _M_data + __n,
                                          __tmp_M_data + _M_size - __n);
-           std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
+          std::__valarray_copy_construct(_M_data + __n, _M_data + _M_size,
                                          __tmp_M_data);
-         }
-       else                        // cshift right
-         {                       
-           std::__valarray_copy_construct
-             (_M_data + _M_size + __n, _M_data + _M_size, __tmp_M_data);
-           std::__valarray_copy_construct
-             (_M_data, _M_data + _M_size + __n, __tmp_M_data - __n);
-         }
+        }
+       else              // cshift right
+        {
+          if (size_t(-__n) > _M_size)
+            __n = -(-__n % _M_size);
+
+          std::__valarray_copy_construct(_M_data + _M_size + __n,
+                                         _M_data + _M_size, __tmp_M_data);
+          std::__valarray_copy_construct(_M_data, _M_data + _M_size + __n,
+                                         __tmp_M_data - __n);
+        }
 
        __ret._M_size = _M_size;
        __ret._M_data = __tmp_M_data;
diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/30416.cc b/libstdc++-v3/testsuite/26_numerics/valarray/30416.cc
new file mode 100644 (file)
index 0000000..c513292
--- /dev/null
@@ -0,0 +1,190 @@
+// 2007-01-11  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2007 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+// USA.
+
+// As a special exception, you may use this file as part of a free software
+// library without restriction.  Specifically, if other files instantiate
+// templates or use macros or inline functions from this file, or you compile
+// this file and link it with other files to produce an executable, this
+// file does not by itself cause the resulting executable to be covered by
+// the GNU General Public License.  This exception does not however
+// invalidate any other reasons why the executable file might be covered by
+// the GNU General Public License.
+
+#include <valarray>
+#include <testsuite_hooks.h>
+
+bool
+comp_vala(const std::valarray<int>& v1, const std::valarray<int>& v2)
+{
+  if (v1.size() == v2.size())
+    {
+      for (size_t i = 0; i < v1.size(); ++i)
+       if (v1[i] != v2[i])
+         return false;
+      return true;
+    }
+  return false;
+}
+
+void
+init_vala(std::valarray<int>& v, size_t first, size_t last, int val)
+{
+  for (size_t i = first; i <= last; ++i)
+    v[i] = val++;
+}
+
+// libstdc++/30416
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+  using namespace std;
+
+  // shift
+  valarray<int> v1;
+  valarray<int> v1_ris(v1.shift(0));
+  VERIFY( comp_vala(v1, v1_ris) );
+  
+  valarray<int> v2;
+  valarray<int> v2_ris(v2.shift(10));
+  VERIFY( comp_vala(v2, v2_ris) );
+  
+  valarray<int> v3;
+  valarray<int> v3_ris(v3.shift(-10));
+  VERIFY( comp_vala(v3, v3_ris) );
+  
+  valarray<int> v4(10);
+  valarray<int> v4_ris(v4.shift(0));
+  VERIFY( comp_vala(v4, v4_ris) );
+  
+  valarray<int> v5(10);
+  init_vala(v5, 0, 9, 1);
+  valarray<int> v5_ref(10);  
+  
+  valarray<int> v5_ris(v5.shift(16));
+  VERIFY( comp_vala(v5_ris, v5_ref) );
+  
+  valarray<int> v6(10);
+  init_vala(v6, 0, 9, 1);
+  valarray<int> v6_ref(10);
+  
+  valarray<int> v6_ris(v6.shift(-16));
+  VERIFY( comp_vala(v6_ris, v6_ref) );
+
+  valarray<int> v7(10);
+  init_vala(v7, 0, 9, 1);
+  valarray<int> v7_ref(10);  
+  
+  valarray<int> v7_ris(v7.shift(10));
+  VERIFY( comp_vala(v7_ris, v7_ref) );
+  
+  valarray<int> v8(10);
+  init_vala(v8, 0, 9, 1);
+  valarray<int> v8_ref(10);
+  
+  valarray<int> v8_ris(v8.shift(-10));
+  VERIFY( comp_vala(v8_ris, v8_ref) );
+
+  valarray<int> v9(10);
+  init_vala(v9, 0, 9, 1);
+  valarray<int> v9_ref(10);  
+  init_vala(v9_ref, 0, 3, 7);
+  
+  valarray<int> v9_ris(v9.shift(6));
+  VERIFY( comp_vala(v9_ris, v9_ref) );
+  
+  valarray<int> v10(10);
+  init_vala(v10, 0, 9, 1);
+  valarray<int> v10_ref(10);
+  init_vala(v10_ref, 6, 9, 1);
+
+  valarray<int> v10_ris(v10.shift(-6));
+  VERIFY( comp_vala(v10_ris, v10_ref) );
+
+  // cshift
+  valarray<int> v11;
+  valarray<int> v11_ris(v11.cshift(0));
+  VERIFY( comp_vala(v11, v11_ris) );
+  
+  valarray<int> v12;
+  valarray<int> v12_ris(v12.cshift(10));
+  VERIFY( comp_vala(v12, v12_ris) );
+  
+  valarray<int> v13;
+  valarray<int> v13_ris(v13.cshift(-10));
+  VERIFY( comp_vala(v13, v13_ris) );
+  
+  valarray<int> v14(10);
+  valarray<int> v14_ris(v14.cshift(0));
+  VERIFY( comp_vala(v14, v14_ris) );
+  
+  valarray<int> v15(10);
+  init_vala(v15, 0, 9, 1);
+  valarray<int> v15_ref(10);
+  init_vala(v15_ref, 0, 3, 7);
+  init_vala(v15_ref, 4, 9, 1);
+  
+  valarray<int> v15_ris(v15.cshift(16));
+  VERIFY( comp_vala(v15_ris, v15_ref) );
+
+  valarray<int> v16(10);
+  init_vala(v16, 0, 9, 1);
+  valarray<int> v16_ref(10);
+  init_vala(v16_ref, 0, 5, 5);
+  init_vala(v16_ref, 6, 9, 1);
+  
+  valarray<int> v16_ris(v16.cshift(-16));
+  VERIFY( comp_vala(v16_ris, v16_ref) );
+
+  valarray<int> v17(10);
+  init_vala(v17, 0, 9, 1);
+  
+  valarray<int> v17_ris(v15.cshift(10));
+  VERIFY( comp_vala(v17, v17_ris) );
+  
+  valarray<int> v18(10);
+  init_vala(v18, 0, 9, 1);
+  
+  valarray<int> v18_ris(v18.cshift(-10));
+  VERIFY( comp_vala(v18, v18_ris) );
+
+  valarray<int> v19(10);
+  init_vala(v19, 0, 9, 1);
+  valarray<int> v19_ref(10);
+  init_vala(v19_ref, 0, 3, 7);
+  init_vala(v19_ref, 4, 9, 1);
+  
+  valarray<int> v19_ris(v15.cshift(6));
+  VERIFY( comp_vala(v19_ris, v19_ref) );
+  
+  valarray<int> v20(10);
+  init_vala(v20, 0, 9, 1);
+  valarray<int> v20_ref(10);
+  init_vala(v20_ref, 0, 5, 5);
+  init_vala(v20_ref, 6, 9, 1);
+  
+  valarray<int> v20_ris(v20.cshift(-6));
+  VERIFY( comp_vala(v20_ris, v20_ref) );
+}
+
+int main()
+{
+  test01();
+  return 0;
+}