From: Jonathan Wakely Date: Thu, 3 May 2018 20:39:31 +0000 (+0100) Subject: PR libstdc++/85632 fix wraparound in filesystem::space X-Git-Tag: releases/gcc-6.5.0~342 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2bec01fac4828894c77e9417fcca4b211268ce66;p=thirdparty%2Fgcc.git 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. * testsuite/experimental/filesystem/operations/space.cc: New. From-SVN: r259911 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 79564c588976..c6ae45be6332 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +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. + * testsuite/experimental/filesystem/operations/space.cc: New. + 2018-03-02 Jonathan Wakely Backport from mainline diff --git a/libstdc++-v3/src/filesystem/ops.cc b/libstdc++-v3/src/filesystem/ops.cc index d4bd5207b635..056fa92a0e6d 100644 --- a/libstdc++-v3/src/filesystem/ops.cc +++ b/libstdc++-v3/src/filesystem/ops.cc @@ -1391,10 +1391,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/experimental/filesystem/operations/space.cc b/libstdc++-v3/testsuite/experimental/filesystem/operations/space.cc new file mode 100644 index 000000000000..aa6a6aeaa602 --- /dev/null +++ b/libstdc++-v3/testsuite/experimental/filesystem/operations/space.cc @@ -0,0 +1,57 @@ +// Copyright (C) 2017-2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { 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 + +namespace fs = std::experimental::filesystem; + +void +test01() +{ + fs::space_info s = fs::space("/"); + std::error_code ec = make_error_code(std::errc::invalid_argument); + s = fs::space("/", ec); + VERIFY( !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(); +}