From 68ae3137c1690f1f6e758c4d282504de8bf97390 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Fri, 1 Feb 2008 05:08:38 +0000 Subject: [PATCH] [SIGAR-85] Fix possible integer overflow in sigar_file_system_usage --- src/os/darwin/darwin_sigar.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index 8d67a6c7..42cae603 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -416,9 +416,6 @@ int sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem) return SIGAR_OK; } -#define SIGAR_FS_BLOCKS_TO_BYTES(buf, f) \ - (((sigar_uint64_t)buf.f * (buf.f_bsize / 512)) >> 1) - #define SWI_MAXMIB 3 #ifdef SIGAR_FREEBSD5 @@ -1721,19 +1718,26 @@ int sigar_disk_usage_get(sigar_t *sigar, const char *name, #endif } +#define SIGAR_FS_BLOCKS_TO_BYTES(val, bsize) ((val * bsize) >> 1) + int sigar_file_system_usage_get(sigar_t *sigar, const char *dirname, sigar_file_system_usage_t *fsusage) { struct statfs buf; + sigar_uint64_t val, bsize; if (statfs(dirname, &buf) < 0) { return errno; } - fsusage->total = SIGAR_FS_BLOCKS_TO_BYTES(buf, f_blocks); - fsusage->free = SIGAR_FS_BLOCKS_TO_BYTES(buf, f_bfree); - fsusage->avail = SIGAR_FS_BLOCKS_TO_BYTES(buf, f_bavail); + bsize = buf.f_bsize / 512; + val = buf.f_blocks; + fsusage->total = SIGAR_FS_BLOCKS_TO_BYTES(val, bsize); + val = buf.f_bfree; + fsusage->free = SIGAR_FS_BLOCKS_TO_BYTES(val, bsize); + val = buf.f_bavail; + fsusage->avail = SIGAR_FS_BLOCKS_TO_BYTES(val, bsize); fsusage->used = fsusage->total - fsusage->free; fsusage->files = buf.f_files; fsusage->free_files = buf.f_ffree;