From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTPS id 6A3FF383583E; Wed, 22 Jun 2022 06:05:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6A3FF383583E Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 1C4301160FA; Wed, 22 Jun 2022 02:05:01 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id aBA8zAl5RED1; Wed, 22 Jun 2022 02:05:01 -0400 (EDT) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id D9A6B1160F1; Wed, 22 Jun 2022 02:05:00 -0400 (EDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 25M64rmR723475 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 22 Jun 2022 03:04:53 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: testsuite: avoid predictable mkstemp Organization: Free thinker, does not speak for AdaCore Errors-To: aoliva@lxoliva.fsfla.org Date: Wed, 22 Jun 2022 03:04:53 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, 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: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Jun 2022 06:05:03 -0000 This patch was originally meant to reduce the likelihood that nonexistent_path() returns the same pathname for from and to. It was prompted by a target system with a non-random implementation of mkstemp, that returns a predictable sequence of filenames and selects the first one that isn't already taken. That turned out not to be enough: nonexistent_path adds a suffix to the filename chosen by mkstemp and removes the file it created, so mkstemp may very well insist on the same basename, and the case that doesn't use mkstemp doesn't even check whether the file already exists. Anyway, by the time I realized this wasn't enough, I'd already implemented some of the changes, and I figured I might as well contribute them, even though they don't really solve any problem, and even if they did, they'd be just a partial solution. Regstrapped on x86_64-linux-gnu, also tested with a cross to aarch64-rtems6. Ok to install? for libstdc++-v3/ChangeLog * testsuite/27_io/filesystem/operations/copy.cc (test02): Select TO after creating FROM. (test03, test04): Likewise. * testsuite/experimental/filesystem/operations/copy.cc (test02, test03, test04): Likewise. --- .../testsuite/27_io/filesystem/operations/copy.cc | 7 ++++--- .../experimental/filesystem/operations/copy.cc | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc index b936e04493b5c..f3081f4b64ebc 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc +++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/copy.cc @@ -73,7 +73,6 @@ test02() const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); auto from = __gnu_test::nonexistent_path(); - auto to = __gnu_test::nonexistent_path(); std::error_code ec; ec = bad_ec; @@ -81,6 +80,7 @@ test02() VERIFY( !ec ); VERIFY( fs::exists(from) ); + auto to = __gnu_test::nonexistent_path(); ec = bad_ec; fs::copy(from, to, fs::copy_options::skip_symlinks, ec); VERIFY( !ec ); @@ -117,12 +117,13 @@ void test03() { auto from = __gnu_test::nonexistent_path(); - auto to = __gnu_test::nonexistent_path(); // test empty file std::ofstream{from}; VERIFY( fs::exists(from) ); VERIFY( fs::file_size(from) == 0 ); + + auto to = __gnu_test::nonexistent_path(); fs::copy(from, to); VERIFY( fs::exists(to) ); VERIFY( fs::file_size(to) == 0 ); @@ -145,11 +146,11 @@ test04() { const std::error_code bad_ec = make_error_code(std::errc::invalid_argument); auto from = __gnu_test::nonexistent_path(); - auto to = __gnu_test::nonexistent_path(); std::error_code ec; create_directories(from/"a/b/c"); + auto to = __gnu_test::nonexistent_path(); { __gnu_test::scoped_file f(to); copy(from, to, ec); diff --git a/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc index 5cd6b483c269b..ca38328c5da15 100644 --- a/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/copy.cc @@ -73,7 +73,6 @@ test02() #endif auto from = __gnu_test::nonexistent_path(); - auto to = __gnu_test::nonexistent_path(); std::error_code ec, bad = std::make_error_code(std::errc::invalid_argument); ec = bad; @@ -81,6 +80,7 @@ test02() VERIFY( !ec ); VERIFY( fs::exists(from) ); + auto to = __gnu_test::nonexistent_path(); ec = bad; fs::copy(from, to, fs::copy_options::skip_symlinks, ec); VERIFY( !ec ); @@ -116,12 +116,13 @@ void test03() { auto from = __gnu_test::nonexistent_path(); - auto to = __gnu_test::nonexistent_path(); // test empty file std::ofstream{from.c_str()}; VERIFY( fs::exists(from) ); VERIFY( fs::file_size(from) == 0 ); + + auto to = __gnu_test::nonexistent_path(); fs::copy(from, to); VERIFY( fs::exists(to) ); VERIFY( fs::file_size(to) == 0 ); @@ -143,11 +144,11 @@ void test04() { auto from = __gnu_test::nonexistent_path(); - auto to = __gnu_test::nonexistent_path(); std::error_code ec; create_directories(from/"a/b/c"); + auto to = __gnu_test::nonexistent_path(); { __gnu_test::scoped_file f(to); copy(from, to, ec); -- Alexandre Oliva, happy hacker https://FSFLA.org/blogs/lxo/ Free Software Activist GNU Toolchain Engineer Disinformation flourishes because many people care deeply about injustice but very few check the facts. Ask me about