public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r14-9000] libstdc++: Avoid aliasing violation in std::valarray [PR99117]
Date: Thu, 15 Feb 2024 11:44:17 +0000 (GMT)	[thread overview]
Message-ID: <20240215114417.BA595384DEE5@sourceware.org> (raw)

https://gcc.gnu.org/g:b58f0e5216a3053486e7f1aa96c3f2443b14d630

commit r14-9000-gb58f0e5216a3053486e7f1aa96c3f2443b14d630
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Feb 8 13:59:42 2024 +0000

    libstdc++: Avoid aliasing violation in std::valarray [PR99117]
    
    The call to __valarray_copy constructs an _Array object to refer to
    this->_M_data but that means that accesses to this->_M_data are through
    a restrict-qualified pointer. This leads to undefined behaviour when
    copying from an _Expr object that actually aliases this->_M_data.
    
    Replace the call to __valarray_copy with a plain loop. I think this
    removes the only use of that overload of __valarray_copy, so it could
    probably be removed. I haven't done that here.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/99117
            * include/std/valarray (valarray::operator=(const _Expr&)):
            Use loop to copy instead of __valarray_copy with _Array.
            * testsuite/26_numerics/valarray/99117.cc: New test.

Diff:
---
 libstdc++-v3/include/std/valarray                    |  8 +++++++-
 libstdc++-v3/testsuite/26_numerics/valarray/99117.cc | 17 +++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/valarray b/libstdc++-v3/include/std/valarray
index a4eecd833f71..46cd57e79821 100644
--- a/libstdc++-v3/include/std/valarray
+++ b/libstdc++-v3/include/std/valarray
@@ -840,7 +840,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // 630. arrays of valarray.
       if (_M_size == __e.size())
-	std::__valarray_copy(__e, _M_size, _Array<_Tp>(_M_data));
+	{
+	  // Copy manually instead of using __valarray_copy, because __e might
+	  // alias _M_data and the _Array param type of __valarray_copy uses
+	  // restrict which doesn't allow aliasing.
+	  for (size_t __i = 0; __i < _M_size; ++__i)
+	    _M_data[__i] = __e[__i];
+	}
       else
 	{
 	  if (_M_data)
diff --git a/libstdc++-v3/testsuite/26_numerics/valarray/99117.cc b/libstdc++-v3/testsuite/26_numerics/valarray/99117.cc
new file mode 100644
index 000000000000..81621bd079a1
--- /dev/null
+++ b/libstdc++-v3/testsuite/26_numerics/valarray/99117.cc
@@ -0,0 +1,17 @@
+// { dg-do run { target c++11 } }
+
+// PR libstdc++/99117 cannot accumulate std::valarray
+
+#include <valarray>
+#include <vector>
+#include <testsuite_hooks.h>
+
+int main()
+{
+    std::vector<std::valarray<int>> v = {{1,1}, {2,2}};
+    std::valarray<int> sum(2);
+    for (const auto& e : v)
+      sum = sum + e;
+    VERIFY(sum[0]==3);
+    VERIFY(sum[1]==3);
+}

                 reply	other threads:[~2024-02-15 11:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240215114417.BA595384DEE5@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).