public inbox for gcc-patches@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++: Reset filesystem::recursive_directory_iterator on error
Date: Wed, 2 Feb 2022 20:47:21 +0000	[thread overview]
Message-ID: <CACb0b4mKHa63+HiD_TUTeTQR+8meQr_HV1Ty=JOA5sLW=yR3aw@mail.gmail.com> (raw)
In-Reply-To: <20220201215903.2191074-1-jwakely@redhat.com>

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

On Tue, 1 Feb 2022 at 22:00, Jonathan Wakely via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Tested powerpc64le-linux, pushed to trunk.
>
>
> The standard requires directory iterators to become equal to the end
> iterator value if they report an error. Some members functions of
> filesystem::recursive_directory_iterator fail to do that.
>
> libstdc++-v3/ChangeLog:
>
>         * src/c++17/fs_dir.cc (recursive_directory_iterator::increment):
>         Reset state to past-the-end iterator on error.
>         (fs::recursive_directory_iterator::pop(error_code&)): Likewise.
>         (fs::recursive_directory_iterator::pop()): Check _M_dirs before
>         it might get reset.
>         * src/filesystem/dir.cc (recursive_directory_iterator): Likewise,
>         for the TS implementation.
>         * testsuite/27_io/filesystem/iterators/error_reporting.cc: New test.
>         * testsuite/experimental/filesystem/iterators/error_reporting.cc: New test.
> ---
>  libstdc++-v3/src/c++17/fs_dir.cc              |  12 +-
>  libstdc++-v3/src/filesystem/dir.cc            |  12 +-
>  .../filesystem/iterators/error_reporting.cc   | 135 +++++++++++++++++
>  .../filesystem/iterators/error_reporting.cc   | 136 ++++++++++++++++++
>  4 files changed, 291 insertions(+), 4 deletions(-)
>  create mode 100644 libstdc++-v3/testsuite/27_io/filesystem/iterators/error_reporting.cc
>  create mode 100644 libstdc++-v3/testsuite/experimental/filesystem/iterators/error_reporting.cc
>
> diff --git a/libstdc++-v3/src/c++17/fs_dir.cc b/libstdc++-v3/src/c++17/fs_dir.cc
> index e050304c19a..149a8b0740c 100644
> --- a/libstdc++-v3/src/c++17/fs_dir.cc
> +++ b/libstdc++-v3/src/c++17/fs_dir.cc
> @@ -311,6 +311,10 @@ fs::recursive_directory_iterator::increment(error_code& ec)
>           return *this;
>         }
>      }
> +
> +  if (ec)
> +    _M_dirs.reset();
> +
>    return *this;
>  }
>
> @@ -334,16 +338,20 @@ fs::recursive_directory_iterator::pop(error_code& ec)
>         ec.clear();
>         return;
>        }
> -  } while (!_M_dirs->top().advance(skip_permission_denied, ec));
> +  } while (!_M_dirs->top().advance(skip_permission_denied, ec) && !ec);
> +
> +  if (ec)
> +    _M_dirs.reset();
>  }
>
>  void
>  fs::recursive_directory_iterator::pop()
>  {
> +  const bool dereferenceable = _M_dirs != nullptr;
>    error_code ec;
>    pop(ec);
>    if (ec)
> -    _GLIBCXX_THROW_OR_ABORT(filesystem_error(_M_dirs
> +    _GLIBCXX_THROW_OR_ABORT(filesystem_error(dereferenceable
>           ? "recursive directory iterator cannot pop"
>           : "non-dereferenceable recursive directory iterator cannot pop",
>           ec));

This gives -Wunused warnings when libstdc++ is built with exceptions disabled.

Fixed by the attached, pushed to trunk.

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

commit 2905e1af94519b7ba3c43a57af8a7d5e10815950
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Feb 2 11:40:28 2022

    libstdc++: Fix -Wunused-variable warning for -fno-exceptions build
    
    If _GLIBCXX_THROW_OR_ABORT expands to just __builtin_abort() then the
    bool variable used in the filesystem_error constructor is unused. Mark
    it as maybe_unused to there's no warning for -fno-exceptions builds.
    
    libstdc++-v3/ChangeLog:
    
            * src/c++17/fs_dir.cc (fs::recursive_directory_iterator::pop):
            Add [[maybe_unused]] attribute.
            * src/filesystem/dir.cc (fs::recursive_directory_iterator::pop):
            Likewise.

diff --git a/libstdc++-v3/src/c++17/fs_dir.cc b/libstdc++-v3/src/c++17/fs_dir.cc
index 149a8b0740c..a77aabb6dcc 100644
--- a/libstdc++-v3/src/c++17/fs_dir.cc
+++ b/libstdc++-v3/src/c++17/fs_dir.cc
@@ -347,7 +347,7 @@ fs::recursive_directory_iterator::pop(error_code& ec)
 void
 fs::recursive_directory_iterator::pop()
 {
-  const bool dereferenceable = _M_dirs != nullptr;
+  [[maybe_unused]] const bool dereferenceable = _M_dirs != nullptr;
   error_code ec;
   pop(ec);
   if (ec)
diff --git a/libstdc++-v3/src/filesystem/dir.cc b/libstdc++-v3/src/filesystem/dir.cc
index ac9e70da516..7cf8e62b5e6 100644
--- a/libstdc++-v3/src/filesystem/dir.cc
+++ b/libstdc++-v3/src/filesystem/dir.cc
@@ -334,7 +334,7 @@ fs::recursive_directory_iterator::pop(error_code& ec)
 void
 fs::recursive_directory_iterator::pop()
 {
-  const bool dereferenceable = _M_dirs != nullptr;
+  [[maybe_unused]] const bool dereferenceable = _M_dirs != nullptr;
   error_code ec;
   pop(ec);
   if (ec)

      parent reply	other threads:[~2022-02-02 20:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-01 21:59 Jonathan Wakely
2022-02-02  0:09 ` Jonathan Wakely
2022-02-02 20: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='CACb0b4mKHa63+HiD_TUTeTQR+8meQr_HV1Ty=JOA5sLW=yR3aw@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).