public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: "libstdc++" <libstdc++@gcc.gnu.org>,
	gcc Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [committed] libstdc++: Fix range access for empty std::valarray [PR103022]
Date: Fri, 5 Nov 2021 21:47:18 +0000	[thread overview]
Message-ID: <CACb0b4kFM7HWobgCUxDar6D1rO6_QpUsdNC3JQN8rv1iDt7-VQ@mail.gmail.com> (raw)
In-Reply-To: <20211101132732.1413142-1-jwakely@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1874 bytes --]

On Mon, 1 Nov 2021 at 13:27, Jonathan Wakely via Libstdc++ <
libstdc++@gcc.gnu.org> wrote:

> Tested powerpc64le-linux, pushed to trunk.
>
>
> The std::begin and std::end overloads for std::valarray are defined in
> terms of std::addressof(v[0]) which is undefined for an empty valarray.
>
> libstdc++-v3/ChangeLog:
>
>         PR libstdc++/103022
>         * include/std/valarray (begin, end): Do not dereference an empty
>         valarray. Add noexcept and [[nodiscard]].
>         * testsuite/26_numerics/valarray/range_access.cc: Check empty
>         valarray. Check iterator properties. Run as well as compiling.
>         * testsuite/26_numerics/valarray/range_access2.cc: Likewise.
>         * testsuite/26_numerics/valarray/103022.cc: New test.
> ---
>  libstdc++-v3/include/std/valarray             | 30 +++++++++---
>  .../testsuite/26_numerics/valarray/103022.cc  | 15 ++++++
>  .../26_numerics/valarray/range_access.cc      | 49 ++++++++++++++++---
>  .../26_numerics/valarray/range_access2.cc     | 22 ++++++++-
>  4 files changed, 100 insertions(+), 16 deletions(-)
>  create mode 100644 libstdc++-v3/testsuite/26_numerics/valarray/103022.cc
>
> diff --git a/libstdc++-v3/include/std/valarray
> b/libstdc++-v3/include/std/valarray
> index 5adc94282ee..c6242eb4db9 100644
> --- a/libstdc++-v3/include/std/valarray
> +++ b/libstdc++-v3/include/std/valarray
> @@ -1210,9 +1210,10 @@ _DEFINE_BINARY_OPERATOR(>=, __greater_equal)
>     *  @param  __va  valarray.
>     */
>    template<class _Tp>
> +    [[__nodiscard__]]
>      inline _Tp*
> -    begin(valarray<_Tp>& __va)
> -    { return std::__addressof(__va[0]); }
> +    begin(valarray<_Tp>& __va) noexcept
> +    { return __va.size() ? std::__addressof(__va[0]) : nullptr; }
>
>

I forgot to also add noexcept to the declarations in <bits/range_access.h>.

Fixed by the attached patch, committed to trunk.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 1426 bytes --]

commit 2b2d97fc545635a0f6aa9c9ee3b017394bc494bf
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Nov 5 21:42:20 2021

    libstdc++: Fix inconsistent noexcept-specific for valarray begin/end
    
    These declarations should be noexcept after I added it to the
    definitions in <valarray>.
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/range_access.h (begin(valarray), end(valarray)):
            Add noexcept.

diff --git a/libstdc++-v3/include/bits/range_access.h b/libstdc++-v3/include/bits/range_access.h
index 3dec687dd94..5e4c4727ebf 100644
--- a/libstdc++-v3/include/bits/range_access.h
+++ b/libstdc++-v3/include/bits/range_access.h
@@ -110,10 +110,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _Tp> class valarray;
   // These overloads must be declared for cbegin and cend to use them.
-  template<typename _Tp> _Tp* begin(valarray<_Tp>&);
-  template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
-  template<typename _Tp> _Tp* end(valarray<_Tp>&);
-  template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
+  template<typename _Tp> _Tp* begin(valarray<_Tp>&) noexcept;
+  template<typename _Tp> const _Tp* begin(const valarray<_Tp>&) noexcept;
+  template<typename _Tp> _Tp* end(valarray<_Tp>&) noexcept;
+  template<typename _Tp> const _Tp* end(const valarray<_Tp>&) noexcept;
 
   /**
    *  @brief  Return an iterator pointing to the first element of

      reply	other threads:[~2021-11-05 21:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-01 13:27 Jonathan Wakely
2021-11-05 21:47 ` Jonathan Wakely [this message]

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=CACb0b4kFM7HWobgCUxDar6D1rO6_QpUsdNC3JQN8rv1iDt7-VQ@mail.gmail.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@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).