Skip to content
Snippets Groups Projects
Commit 2bc1b3e4 authored by Yorhel's avatar Yorhel
Browse files

util.c: Fix possible buffer overflow in fillsize() and formatsize()

They should now be able to cope with file sizes in the full (positive)
range of a signed integer, i.e. 0 bytes to 8 EiB minus one byte. The
size calculation of directories, however, may still overflow and cause
negative integers to be passed around. That should be fixed.
parent 3fd25ffb
No related branches found
No related tags found
No related merge requests found
...@@ -62,15 +62,17 @@ char *formatsize(int64_t from) { ...@@ -62,15 +62,17 @@ char *formatsize(int64_t from) {
else if(r < 1023e3f) { c = 'K'; r/=1024.0f; } else if(r < 1023e3f) { c = 'K'; r/=1024.0f; }
else if(r < 1023e6f) { c = 'M'; r/=1048576.0f; } else if(r < 1023e6f) { c = 'M'; r/=1048576.0f; }
else if(r < 1023e9f) { c = 'G'; r/=1073741824.0f; } else if(r < 1023e9f) { c = 'G'; r/=1073741824.0f; }
else { c = 'T'; r/=1099511627776.0f; } else if(r < 1023e12f){ c = 'T'; r/=1099511627776.0f; }
else if(r < 1023e15f){ c = 'P'; r/=1125899906842624.0f; }
else { c = 'E'; r/=1152921504606846976.0f; }
sprintf(dat, "%5.1f%c%cB", r, c, c == ' ' ? ' ' : 'i'); sprintf(dat, "%5.1f%c%cB", r, c, c == ' ' ? ' ' : 'i');
return dat; return dat;
} }
char *fullsize(int64_t from) { char *fullsize(int64_t from) {
static char dat[20]; /* max: 999.999.999.999.999 */ static char dat[26]; /* max: 9.223.372.036.854.775.807 (= 2^63-1) */
char tmp[20]; char tmp[26];
int64_t n = from; int64_t n = from;
int i, j; int i, j;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment