From 658e7fd008724d8316bf824dd344b8410c376b50 Mon Sep 17 00:00:00 2001 From: redi Date: Thu, 3 May 2018 18:58:00 +0000 Subject: [PATCH] PR libstdc++/85632 fix wraparound in filesystem::space On 32-bit targets any values over 4GB would wrap and produce the wrong result. PR libstdc++/85632 use uintmax_t for arithmetic * src/filesystem/ops.cc (experimental::filesystem::space): Perform arithmetic in result type. * src/filesystem/std-ops.cc (filesystem::space): Likewise. * testsuite/27_io/filesystem/operations/space.cc: Check total capacity is greater than free space. * testsuite/experimental/filesystem/operations/space.cc: New. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@259901 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/ChangeLog | 8 ++++++++ libstdc++-v3/src/filesystem/ops.cc | 7 ++++--- libstdc++-v3/src/filesystem/std-ops.cc | 7 ++++--- .../testsuite/27_io/filesystem/operations/space.cc | 9 +++++++++ .../filesystem/operations/space.cc | 23 ++++++++++++++++------ 5 files changed, 42 insertions(+), 12 deletions(-) copy libstdc++-v3/testsuite/{27_io => experimental}/filesystem/operations/space.cc (75%) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index f3473016fd5..c2a224da55b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,13 @@ 2018-05-03 Jonathan Wakely + PR libstdc++/85632 use uintmax_t for arithmetic + * src/filesystem/ops.cc (experimental::filesystem::space): Perform + arithmetic in result type. + * src/filesystem/std-ops.cc (filesystem::space): Likewise. + * testsuite/27_io/filesystem/operations/space.cc: Check total capacity + is greater than free space. + * testsuite/experimental/filesystem/operations/space.cc: New. + * testsuite/20_util/remove_cvref/requirements/alias_decl.cc: New. * testsuite/20_util/remove_cvref/requirements/explicit_instantiation.cc: New. diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc index 328332a8a82..4a9e265d1d6 100644 --- a/libstdc++-v3/src/filesystem/ops.cc +++ b/libstdc++-v3/src/filesystem/ops.cc @@ -1132,10 +1132,11 @@ fs::space(const path& p, error_code& ec) noexcept ec.assign(errno, std::generic_category()); else { + uintmax_t fragment_size = f.f_frsize; info = space_info{ - f.f_blocks * f.f_frsize, - f.f_bfree * f.f_frsize, - f.f_bavail * f.f_frsize + f.f_blocks * fragment_size, + f.f_bfree * fragment_size, + f.f_bavail * fragment_size }; ec.clear(); } diff --git a/libstdc++-v3/src/filesystem/std-ops.cc b/libstdc++-v3/src/filesystem/std-ops.cc index 930b186e88c..74868cd48e6 100644 --- a/libstdc++-v3/src/filesystem/std-ops.cc +++ b/libstdc++-v3/src/filesystem/std-ops.cc @@ -1378,10 +1378,11 @@ fs::space(const path& p, error_code& ec) noexcept ec.assign(errno, std::generic_category()); else { + uintmax_t fragment_size = f.f_frsize; info = space_info{ - f.f_blocks * f.f_frsize, - f.f_bfree * f.f_frsize, - f.f_bavail * f.f_frsize + f.f_blocks * fragment_size, + f.f_bfree * fragment_size, + f.f_bavail * fragment_size }; ec.clear(); } diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/space.cc b/libstdc++-v3/testsuite/27_io/filesystem/operations/space.cc index bdda24183bc..3d64342fb8f 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/operations/space.cc +++ b/libstdc++-v3/testsuite/27_io/filesystem/operations/space.cc @@ -32,6 +32,7 @@ test01() std::error_code ec = make_error_code(std::errc::invalid_argument); s = std::filesystem::space("/", ec); VERIFY( !ec ); + s = std::filesystem::space(__gnu_test::nonexistent_path(), ec); VERIFY( ec ); VERIFY( s.capacity == static_cast(-1) ); @@ -39,8 +40,16 @@ test01() VERIFY( s.available == static_cast(-1) ); } +void +test02() +{ + std::filesystem::space_info s = std::filesystem::space("."); + VERIFY( s.capacity >= s.free ); +} + int main() { test01(); + test02(); } diff --git a/libstdc++-v3/testsuite/27_io/filesystem/operations/space.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/space.cc similarity index 75% copy from libstdc++-v3/testsuite/27_io/filesystem/operations/space.cc copy to libstdc++-v3/testsuite/experimental/filesystem/operations/space.cc index bdda24183bc..aa6a6aeaa60 100644 --- a/libstdc++-v3/testsuite/27_io/filesystem/operations/space.cc +++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/space.cc @@ -15,32 +15,43 @@ // with this library; see the file COPYING3. If not see // . -// { dg-options "-std=gnu++17 -lstdc++fs" } -// { dg-do run { target c++17 } } +// { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" } +// { dg-do run { target c++11 } } // { dg-require-filesystem-ts "" } // 30.10.14.3 Permissions [fs.op.space] -#include +#include #include #include +namespace fs = std::experimental::filesystem; + void test01() { - std::filesystem::space_info s = std::filesystem::space("/"); + fs::space_info s = fs::space("/"); std::error_code ec = make_error_code(std::errc::invalid_argument); - s = std::filesystem::space("/", ec); + s = fs::space("/", ec); VERIFY( !ec ); - s = std::filesystem::space(__gnu_test::nonexistent_path(), ec); + + s = fs::space(__gnu_test::nonexistent_path(), ec); VERIFY( ec ); VERIFY( s.capacity == static_cast(-1) ); VERIFY( s.free == static_cast(-1) ); VERIFY( s.available == static_cast(-1) ); } +void +test02() +{ + fs::space_info s = fs::space("."); + VERIFY( s.capacity >= s.free ); +} + int main() { test01(); + test02(); } -- 2.11.4.GIT