Skip to content

Commit

Permalink
btdu.browser: Fix integer overflow when calculating displayed size
Browse files Browse the repository at this point in the history
Fixes #1.
  • Loading branch information
CyberShadow committed Nov 13, 2020
1 parent 4b6f06f commit 7ea958c
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions source/btdu/browser.d
Expand Up @@ -184,7 +184,7 @@ struct Browser

["- Size: " ~ (browserRoot.samples
? format!"~%s (%d sample%s)"(
humanSize(currentPath.samples * totalSize / browserRoot.samples),
humanSize(currentPath.samples * real(totalSize) / browserRoot.samples),
currentPath.samples,
currentPath.samples == 1 ? "" : "s",
)
Expand Down Expand Up @@ -479,7 +479,7 @@ struct Browser
buf.clear();
{
auto size = browserRoot.samples
? "~" ~ humanSize(child.samples * totalSize / browserRoot.samples)
? "~" ~ humanSize(child.samples * real(totalSize) / browserRoot.samples)
: "?";
buf.formattedWrite!"%12s "(size);
}
Expand Down Expand Up @@ -791,17 +791,16 @@ struct Browser

private:

string humanSize(ulong size)
string humanSize(real size)
{
static immutable prefixChars = " KMGTPEZY";
double fpSize = size;
size_t power = 0;
while (fpSize > 1024 && power + 1 < prefixChars.length)
while (size > 1024 && power + 1 < prefixChars.length)
{
fpSize /= 1024;
size /= 1024;
power++;
}
return format("%3.1f %s%sB", fpSize, prefixChars[power], prefixChars[power] == ' ' ? ' ' : 'i');
return format("%3.1f %s%sB", size, prefixChars[power], prefixChars[power] == ' ' ? ' ' : 'i');
}

/// Helper type for formatting pointers without passing their contents by-value.
Expand Down

0 comments on commit 7ea958c

Please sign in to comment.