From 924d84acf093b1c912443edad90152d353211fbf Mon Sep 17 00:00:00 2001 From: "Benjamin A. Beasley" Date: Fri, 30 Jul 2021 16:37:28 -0400 Subject: [PATCH] Cast to uintmax_t *before* multiplying in space calculations This ensures that all cases where the result actually does fit in a uintmax_t are correctly handled. Before, the multiplication could be performed in a smaller type, leading to an incorrect result or, worse, undefined behavior due to signed integer overflow. This fixes a test failure that was actually observed on an i686 Linux system. --- include/ghc/filesystem.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index 2c7538b..4924c73 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -4875,7 +4875,7 @@ GHC_INLINE space_info space(const path& p, std::error_code& ec) noexcept ec = detail::make_system_error(); return {static_cast(-1), static_cast(-1), static_cast(-1)}; } - return {static_cast(sfs.f_blocks * sfs.f_frsize), static_cast(sfs.f_bfree * sfs.f_frsize), static_cast(sfs.f_bavail * sfs.f_frsize)}; + return {static_cast(sfs.f_blocks) * static_cast(sfs.f_frsize), static_cast(sfs.f_bfree) * static_cast(sfs.f_frsize), static_cast(sfs.f_bavail) * static_cast(sfs.f_frsize)}; #endif }