Commit 2e023647 by Jonathan Wakely Committed by Jonathan Wakely

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.

From-SVN: r259901
parent d18734b5
2018-05-03 Jonathan Wakely <jwakely@redhat.com> 2018-05-03 Jonathan Wakely <jwakely@redhat.com>
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/alias_decl.cc: New.
* testsuite/20_util/remove_cvref/requirements/explicit_instantiation.cc: * testsuite/20_util/remove_cvref/requirements/explicit_instantiation.cc:
New. New.
......
...@@ -1132,10 +1132,11 @@ fs::space(const path& p, error_code& ec) noexcept ...@@ -1132,10 +1132,11 @@ fs::space(const path& p, error_code& ec) noexcept
ec.assign(errno, std::generic_category()); ec.assign(errno, std::generic_category());
else else
{ {
uintmax_t fragment_size = f.f_frsize;
info = space_info{ info = space_info{
f.f_blocks * f.f_frsize, f.f_blocks * fragment_size,
f.f_bfree * f.f_frsize, f.f_bfree * fragment_size,
f.f_bavail * f.f_frsize f.f_bavail * fragment_size
}; };
ec.clear(); ec.clear();
} }
......
...@@ -1378,10 +1378,11 @@ fs::space(const path& p, error_code& ec) noexcept ...@@ -1378,10 +1378,11 @@ fs::space(const path& p, error_code& ec) noexcept
ec.assign(errno, std::generic_category()); ec.assign(errno, std::generic_category());
else else
{ {
uintmax_t fragment_size = f.f_frsize;
info = space_info{ info = space_info{
f.f_blocks * f.f_frsize, f.f_blocks * fragment_size,
f.f_bfree * f.f_frsize, f.f_bfree * fragment_size,
f.f_bavail * f.f_frsize f.f_bavail * fragment_size
}; };
ec.clear(); ec.clear();
} }
......
...@@ -32,6 +32,7 @@ test01() ...@@ -32,6 +32,7 @@ test01()
std::error_code ec = make_error_code(std::errc::invalid_argument); std::error_code ec = make_error_code(std::errc::invalid_argument);
s = std::filesystem::space("/", ec); s = std::filesystem::space("/", ec);
VERIFY( !ec ); VERIFY( !ec );
s = std::filesystem::space(__gnu_test::nonexistent_path(), ec); s = std::filesystem::space(__gnu_test::nonexistent_path(), ec);
VERIFY( ec ); VERIFY( ec );
VERIFY( s.capacity == static_cast<uintmax_t>(-1) ); VERIFY( s.capacity == static_cast<uintmax_t>(-1) );
...@@ -39,8 +40,16 @@ test01() ...@@ -39,8 +40,16 @@ test01()
VERIFY( s.available == static_cast<uintmax_t>(-1) ); VERIFY( s.available == static_cast<uintmax_t>(-1) );
} }
void
test02()
{
std::filesystem::space_info s = std::filesystem::space(".");
VERIFY( s.capacity >= s.free );
}
int int
main() main()
{ {
test01(); test01();
test02();
} }
// 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
// <http://www.gnu.org/licenses/>.
// { 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 <experimental/filesystem>
#include <testsuite_fs.h>
#include <testsuite_hooks.h>
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<uintmax_t>(-1) );
VERIFY( s.free == static_cast<uintmax_t>(-1) );
VERIFY( s.available == static_cast<uintmax_t>(-1) );
}
void
test02()
{
fs::space_info s = fs::space(".");
VERIFY( s.capacity >= s.free );
}
int
main()
{
test01();
test02();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment