From patchwork Tue Sep 3 15:19:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1980104 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LMSeSt+u; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wyq7J75Bjz1yg9 for ; Wed, 4 Sep 2024 01:20:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E4989384F02C for ; Tue, 3 Sep 2024 15:20:09 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id DD6CA385DDCA for ; Tue, 3 Sep 2024 15:19:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD6CA385DDCA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DD6CA385DDCA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725376771; cv=none; b=qJmcOHYs/SzRmkID7UuY999qByapc/mR8Tv9Q7Q1cyxKBdN+GqSGxxtEgO3TlzH5BaD9jyQHyaICLOoDVF3OBDtNvE4bD4HsmwbRWNfAn1lN1S3aT1OHT2qo6fsX17jdXh6VOAn0JJsNHsQIMjR1TrwoxAZI7VysoBBH0c7gWxQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725376771; c=relaxed/simple; bh=lJ2vdIKwjB+4UBVAffK5PddBtrTBYql4RIh0FdaAJBE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=AHF03hWBt+F8sPuUJ5LPlewa9FdpeLO1Q+tYH6Klzm19A4fz3KCX81VeWzZ7W5Jd5yru1QkynY2djpA1AxxUHr8OqZD7VHgSL4EpWMggebsChXwx2Yg1S61bgjGY47pJD8EeMidAnMKJwxmr7POz5dz8JyL4J5AgJbShSNnNKLQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725376768; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NvaU/gvPIJh0KMphuZkhHLrk+Jt3ktT7K7uQfqDsrFI=; b=LMSeSt+uE28VamiLmkf6PLkECH4ZTwy2L79gzzajSWeFfbQFWd+nj2olGOQYK9x19iTw6E DCeLOW5GTpi6RBnTj2yaRie4Pj0np87Di+dD+EN1AUUsrYvVjnpErtfF1jOje4BM90bOEy g7am0cJwnUw0FEvyoXwR6g5apBTr7ns= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-319-wlESNHCjN42deYYZfG6XYA-1; Tue, 03 Sep 2024 11:19:25 -0400 X-MC-Unique: wlESNHCjN42deYYZfG6XYA-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 39CDB19352FD; Tue, 3 Sep 2024 15:19:24 +0000 (UTC) Received: from localhost (unknown [10.42.28.101]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B43FE3001FEF; Tue, 3 Sep 2024 15:19:23 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix error handling in fs::hard_link_count for Windows Date: Tue, 3 Sep 2024 16:19:18 +0100 Message-ID: <20240903151922.2701831-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- The recent change to use auto_win_file_handle for std::filesystem::hard_link_count caused a regression. The std::error_code argument should be cleared if no error occurs, but this no longer happens. Add a call to ec.clear() in fs::hard_link_count to fix this. Also change the auto_win_file_handle class to take a reference to the std::error_code and set it if an error occurs, to slightly simplify the control flow in the fs::equiv_files function. libstdc++-v3/ChangeLog: * src/c++17/fs_ops.cc (auto_win_file_handle): Add error_code& member and set it if CreateFileW or GetFileInformationByHandle fails. (fs::equiv_files) [_GLIBCXX_FILESYSTEM_IS_WINDOWS]: Simplify control flow. (fs::hard_link_count) [_GLIBCXX_FILESYSTEM_IS_WINDOWS]: Clear ec on success. * testsuite/27_io/filesystem/operations/hard_link_count.cc: Check error handling. --- libstdc++-v3/src/c++17/fs_ops.cc | 59 +++++++++++-------- .../filesystem/operations/hard_link_count.cc | 24 ++++++++ 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc index 9606afa9f1f..946fefd9e44 100644 --- a/libstdc++-v3/src/c++17/fs_ops.cc +++ b/libstdc++-v3/src/c++17/fs_ops.cc @@ -829,23 +829,37 @@ namespace struct auto_win_file_handle { explicit - auto_win_file_handle(const wchar_t* p) + auto_win_file_handle(const wchar_t* p, std::error_code& ec) noexcept : handle(CreateFileW(p, 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, - 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)) - { } + 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)), + ec(ec) + { + if (handle == INVALID_HANDLE_VALUE) + ec = std::__last_system_error(); + } ~auto_win_file_handle() { if (*this) CloseHandle(handle); } - explicit operator bool() const + explicit operator bool() const noexcept { return handle != INVALID_HANDLE_VALUE; } - bool get_info() - { return GetFileInformationByHandle(handle, &info); } + bool get_info() noexcept + { + if (GetFileInformationByHandle(handle, &info)) + return true; + ec = std::__last_system_error(); + return false; + } HANDLE handle; BY_HANDLE_FILE_INFORMATION info; + // Like errno, we only set this on error and never clear it. + // This propagates an error_code to the caller when something goes wrong, + // but the caller should not assume a non-zero ec means an error happened + // unless they explicitly cleared it before passing it to our constructor. + std::error_code& ec; }; } #endif @@ -866,23 +880,14 @@ fs::equiv_files([[maybe_unused]] const char_type* p1, const stat_type& st1, if (st1.st_mode != st2.st_mode || st1.st_dev != st2.st_dev) return false; - // Need to use GetFileInformationByHandle to get more info about the files. - auto_win_file_handle h1(p1); - auto_win_file_handle h2(p2); - if (!h1 || !h2) - { - if (!h1 && !h2) - ec = __last_system_error(); - return false; - } - if (!h1.get_info() || !h2.get_info()) - { - ec = __last_system_error(); - return false; - } - return h1.info.dwVolumeSerialNumber == h2.info.dwVolumeSerialNumber - && h1.info.nFileIndexHigh == h2.info.nFileIndexHigh - && h1.info.nFileIndexLow == h2.info.nFileIndexLow; + // Use GetFileInformationByHandle to get more info about the files. + if (auto_win_file_handle h1{p1, ec}) + if (auto_win_file_handle h2{p2, ec}) + if (h1.get_info() && h2.get_info()) + return h1.info.dwVolumeSerialNumber == h2.info.dwVolumeSerialNumber + && h1.info.nFileIndexHigh == h2.info.nFileIndexHigh + && h1.info.nFileIndexLow == h2.info.nFileIndexLow; + return false; #endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS } #endif // NEED_DO_COPY_FILE @@ -1007,10 +1012,12 @@ std::uintmax_t fs::hard_link_count(const path& p, error_code& ec) noexcept { #if _GLIBCXX_FILESYSTEM_IS_WINDOWS - auto_win_file_handle h(p.c_str()); + auto_win_file_handle h(p.c_str(), ec); if (h && h.get_info()) - return static_cast(h.info.nNumberOfLinks); - ec = __last_system_error(); + { + ec.clear(); + return static_cast(h.info.nNumberOfLinks); + } return static_cast(-1); #elif defined _GLIBCXX_HAVE_SYS_STAT_H return do_stat(p, ec, std::mem_fn(&stat_type::st_nlink), diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/hard_link_count.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/hard_link_count.cc index 8b2fb4f190e..4bff39ca308 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/operations/hard_link_count.cc +++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/hard_link_count.cc @@ -30,8 +30,32 @@ void test01() VERIFY( fs::hard_link_count(p2) == 2 ); } +void +test02() +{ + std::error_code ec; + fs::path p1 = __gnu_test::nonexistent_path(); + VERIFY( fs::hard_link_count(p1, ec) == -1 ); + VERIFY( ec == std::errc::no_such_file_or_directory ); + +#if __cpp_exceptions + try { + fs::hard_link_count(p1); // { dg-warning "ignoring return value" } + VERIFY( false ); + } catch (const fs::filesystem_error& e) { + VERIFY( e.path1() == p1 ); + VERIFY( e.path2().empty() ); + } +#endif + + __gnu_test::scoped_file f1(p1); + fs::hard_link_count(f1.path, ec); // { dg-warning "ignoring return value" } + VERIFY( !ec ); // Should be cleared on success. +} + int main() { test01(); + test02(); }