diff --git a/doc/ncdu.pod b/doc/ncdu.pod index ef2d4b0f3527e6a7385f21df5ffb56aaa5383f6b..3d8df5b335ce93af91fe21550a5c7a2c3096aaac 100644 --- a/doc/ncdu.pod +++ b/doc/ncdu.pod @@ -276,6 +276,10 @@ links, and will thus be scanned and counted multiple times. Some minor glitches may appear when displaying filenames that contain multibyte or multicolumn characters. +All sizes are internally represented as a signed 64bit integer. If you have a +directory larger than 8 EiB minus one byte, ncdu will clip its size to 8 EiB +minus one byte. + Please report any other bugs you may find at the bug tracker, which can be found on the web site at http://dev.yorhel.nl/ncdu diff --git a/src/dir_mem.c b/src/dir_mem.c index 3001c014afa943b430c58c4973b3b34e7983d56f..d13ba24b8561bfaf0d50604b4710f540488dd7fb 100644 --- a/src/dir_mem.c +++ b/src/dir_mem.c @@ -84,8 +84,8 @@ static void hlink_check(struct dir *d) { if(pt==par) i=0; if(i) { - par->size += d->size; - par->asize += d->asize; + par->size = adds64(par->size, d->size); + par->asize = adds64(par->size, d->asize); } } } diff --git a/src/global.h b/src/global.h index 7249bcd0c664dbcaf70eda216834c48c095d4dd6..d6eb2cb4a41ab45ca07cee5939973c63cc98de98 100644 --- a/src/global.h +++ b/src/global.h @@ -29,6 +29,7 @@ #include "config.h" #include <stdio.h> #include <stddef.h> +#include <limits.h> #include <sys/types.h> #include <sys/stat.h> diff --git a/src/util.c b/src/util.c index d519581970f56deb429e87b5aec7f7e22659e5a8..ce000e6498a09103010105f2ffca5931cef25b7e 100644 --- a/src/util.c +++ b/src/util.c @@ -287,8 +287,8 @@ struct dir *getroot(struct dir *d) { void addparentstats(struct dir *d, int64_t size, int64_t asize, int items) { while(d) { - d->size += size; - d->asize += asize; + d->size = adds64(d->size, size); + d->asize = adds64(d->asize, asize); d->items += items; d = d->parent; } diff --git a/src/util.h b/src/util.h index 8b1b959a3cb30175dc1bea2c4d17a2c1148cfbf0..5c638766f1c4d7306f96530221d7d66dec0ecb58 100644 --- a/src/util.h +++ b/src/util.h @@ -81,6 +81,12 @@ char *getpath(struct dir *); /* returns the root element of the given dir struct */ struct dir *getroot(struct dir *); +/* Add two positive signed 64-bit integers. Returns INT64_MAX if the result + * would overflow. + * I use uint64_t's to detect the overflow, as (a + b < 0) relies on undefined + * behaviour, and (INT64_MAX - b >= a) didn't work for some reason. */ +#define adds64(a, b) ((uint64_t)(a) + (uint64_t)(b) > (uint64_t)INT64_MAX ? INT64_MAX : (a)+(b)) + /* Adds a value to the size, asize and items fields of *d and its parents */ void addparentstats(struct dir *, int64_t, int64_t, int);