From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id F214E385802A; Fri, 16 Feb 2024 15:12:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F214E385802A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708096350; bh=IBgoamxcli9ZAFD5eLxdN+xr27JptRoS6/LS3rv4YuI=; h=From:To:Subject:Date:From; b=gOC/iJdgNf3NGJwcGafhfmUbEpI7pTD4EsLljsVnoJaIHEV0KqKtBo0uEcWPm4sTR /uAxpb40ClpfTUJbyFrZLlyxOXVdoCvrXbX79i/p+x21dPruNgtLM+zOH+BZ7kGA7u QfFMPlZhtGWJDIe2EYDbKw0O/TJrHRjazbCavUJ4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-8334] libstdc++: Avoid aliasing violation in std::valarray [PR99117] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 423018acfecf603881d2e3eacc1d6862ae4cd53e X-Git-Newrev: 3a72c717b311ce8093042d927a1f2f2b940a969c Message-Id: <20240216151230.F214E385802A@sourceware.org> Date: Fri, 16 Feb 2024 15:12:30 +0000 (GMT) List-Id: https://gcc.gnu.org/g:3a72c717b311ce8093042d927a1f2f2b940a969c commit r13-8334-g3a72c717b311ce8093042d927a1f2f2b940a969c Author: Jonathan Wakely 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. (cherry picked from commit b58f0e5216a3053486e7f1aa96c3f2443b14d630) 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 f172db6c623e..ddf10b993da8 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 +#include +#include + +int main() +{ + std::vector> v = {{1,1}, {2,2}}; + std::valarray sum(2); + for (const auto& e : v) + sum = sum + e; + VERIFY(sum[0]==3); + VERIFY(sum[1]==3); +}