mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-15 23:36:34 +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)
|
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
|
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);
|
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;
|
int n = -1;
|
||||||
while (filesize > D_FILE_SIZE_DIVIDER * 100) {
|
while (filesize > D_FILE_SIZE_DIVIDER * 100 && n < UNKNOWN_SUFFIX) {
|
||||||
n += 1;
|
n += 1;
|
||||||
filesize /= D_FILE_SIZE_DIVIDER;
|
filesize /= D_FILE_SIZE_DIVIDER;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp = (uint32_t) filesize;
|
tmp = (uint32_t) filesize;
|
||||||
return snprintf_P(buf, len, PSTR("%u" D_DECIMAL_POINT "%02u %s"), tmp / 100, tmp % 100, suffix[n]);
|
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