diff mbox series

Fix free space calculation being truncated

Message ID 20220825152618.3577241-1-JPEWhacker@gmail.com
State Accepted
Headers show
Series Fix free space calculation being truncated | expand

Commit Message

Joshua Watt Aug. 25, 2022, 3:26 p.m. UTC
The free space calculation was incorrectly being truncated to long
because that is the type of both f_bfree and f_bsize in struct statvfs.

This can result in erroneous failures to install an update when there is
actually enough free space.

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
---
 core/util.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Stefano Babic Aug. 25, 2022, 4:22 p.m. UTC | #1
On 25.08.22 17:26, Joshua Watt wrote:
> The free space calculation was incorrectly being truncated to long
> because that is the type of both f_bfree and f_bsize in struct statvfs.
> 
> This can result in erroneous failures to install an update when there is
> actually enough free space.
> 
> Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
> ---
>   core/util.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/core/util.c b/core/util.c
> index 3724182..e7745b6 100644
> --- a/core/util.c
> +++ b/core/util.c
> @@ -1277,15 +1277,17 @@ static bool check_free_space(int fd, long long size, char *fname)
>   #define fstatvfs fstatfs
>   #endif
>   	struct statvfs statvfs;
> +	unsigned long long free_space;
>   
>   	if (fstatvfs(fd, &statvfs)) {
>   		ERROR("Statfs failed on %s, skipping free space check", fname);
>   		return true;
>   	}
> +	free_space = (unsigned long long)statvfs.f_bfree * statvfs.f_bsize;
>   
> -	if (statvfs.f_bfree * statvfs.f_bsize < size) {
> +	if (free_space < size) {
>   		ERROR("Not enough free space to extract %s (needed %llu, got %llu)",
> -		       fname, size, (unsigned long long)statvfs.f_bfree * statvfs.f_bsize);
> +		       fname, size, free_space);
>   		return false;
>   	}
>   

Reviewed-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index 3724182..e7745b6 100644
--- a/core/util.c
+++ b/core/util.c
@@ -1277,15 +1277,17 @@  static bool check_free_space(int fd, long long size, char *fname)
 #define fstatvfs fstatfs
 #endif
 	struct statvfs statvfs;
+	unsigned long long free_space;
 
 	if (fstatvfs(fd, &statvfs)) {
 		ERROR("Statfs failed on %s, skipping free space check", fname);
 		return true;
 	}
+	free_space = (unsigned long long)statvfs.f_bfree * statvfs.f_bsize;
 
-	if (statvfs.f_bfree * statvfs.f_bsize < size) {
+	if (free_space < size) {
 		ERROR("Not enough free space to extract %s (needed %llu, got %llu)",
-		       fname, size, (unsigned long long)statvfs.f_bfree * statvfs.f_bsize);
+		       fname, size, free_space);
 		return false;
 	}