mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-04-19 12:57:19 +00:00
Just in case file size in the TiB range gets passed, make sure we
don't index beyond the end of the array.
This commit is contained in:
parent
452c619e97
commit
e37ca4c318
@ -199,16 +199,18 @@ bool Parser::is_only_digits(const char* s)
|
||||
|
||||
int Parser::format_bytes(uint64_t filesize, char* buf, size_t len)
|
||||
{
|
||||
filesize *= 100; // Warning - If filesize up in the Ei range (2^60), we will overflow.
|
||||
filesize *= 102400; // Warning - If filesize up in the Ei range (2^60), we will overflow.
|
||||
uint32_t tmp = (uint32_t) filesize; // cast to unsigned int here to saye ugly casts in next line
|
||||
if(filesize < D_FILE_SIZE_DIVIDER) return snprintf_P(buf, len, PSTR("%u " D_FILE_SIZE_BYTES), tmp);
|
||||
|
||||
const char* suffix[] = {D_FILE_SIZE_KILOBYTES, D_FILE_SIZE_MEGABYTES, D_FILE_SIZE_GIGABYTES};
|
||||
const char* suffix[] = {D_FILE_SIZE_KILOBYTES, D_FILE_SIZE_MEGABYTES, D_FILE_SIZE_GIGABYTES, "oops"};
|
||||
#define UNKNOWN_SUFFIX 2
|
||||
int n = -1;
|
||||
while (filesize > D_FILE_SIZE_DIVIDER * 100) {
|
||||
while (filesize > D_FILE_SIZE_DIVIDER * 100 && n < UNKNOWN_SUFFIX) {
|
||||
n += 1;
|
||||
filesize /= D_FILE_SIZE_DIVIDER;
|
||||
}
|
||||
|
||||
tmp = (uint32_t) filesize;
|
||||
return snprintf_P(buf, len, PSTR("%u" D_DECIMAL_POINT "%02u %s"), tmp / 100, tmp % 100, suffix[n]);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user