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:
FreeBear 2024-05-22 15:21:44 +01:00
parent 452c619e97
commit e37ca4c318

View File

@ -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]);
}