From aa09f4bfd43a396816d902428a12a2b940d2f486 Mon Sep 17 00:00:00 2001 From: FreeBear Date: Wed, 22 May 2024 00:51:02 +0100 Subject: [PATCH] Using size_t limits max number to 2^32. When dealing with SD cards we could be asked to print a uint64 (for example, from SD.totalGytes()). --- src/hasp/hasp_parser.cpp | 26 ++++++++++++++------------ src/hasp/hasp_parser.h | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/hasp/hasp_parser.cpp b/src/hasp/hasp_parser.cpp index eee1175d..f6db3854 100644 --- a/src/hasp/hasp_parser.cpp +++ b/src/hasp/hasp_parser.cpp @@ -197,24 +197,26 @@ bool Parser::is_only_digits(const char* s) return strlen(s) == digits; } -int Parser::format_bytes(size_t filesize, char* buf, size_t len) +int Parser::format_bytes(uint64_t filesize, char* buf, size_t len) { - if(filesize < D_FILE_SIZE_DIVIDER) return snprintf_P(buf, len, PSTR("%d " D_FILE_SIZE_BYTES), filesize); - filesize = filesize * 100; + 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); - filesize = filesize / D_FILE_SIZE_DIVIDER; // multiply by 100 for 2 decimal place + filesize = filesize / (D_FILE_SIZE_DIVIDER/100); // multiply by 100 for 2 decimal place + tmp = (uint32_t) filesize; if(filesize < D_FILE_SIZE_DIVIDER * 100) - return snprintf_P(buf, len, PSTR("%d" D_DECIMAL_POINT "%02d " D_FILE_SIZE_KILOBYTES), filesize / 100, - filesize % 100); + return snprintf_P(buf, len, PSTR("%u" D_DECIMAL_POINT "%02u " D_FILE_SIZE_KILOBYTES), tmp / 100, + tmp % 100); - filesize = filesize / D_FILE_SIZE_DIVIDER; // multiply by 100 for 2 decimal place + filesize = filesize / D_FILE_SIZE_DIVIDER; + tmp = (uint32_t) filesize; if(filesize < D_FILE_SIZE_DIVIDER * 100) - return snprintf_P(buf, len, PSTR("%d" D_DECIMAL_POINT "%02d " D_FILE_SIZE_MEGABYTES), filesize / 100, - filesize % 100); + return snprintf_P(buf, len, PSTR("%u" D_DECIMAL_POINT "%02u " D_FILE_SIZE_MEGABYTES), tmp / 100, + tmp % 100); - filesize = filesize / D_FILE_SIZE_DIVIDER; // multiply by 100 for 2 decimal place - return snprintf_P(buf, len, PSTR("%d" D_DECIMAL_POINT "%02d " D_FILE_SIZE_GIGABYTES), filesize / 100, - filesize % 100); + tmp = (uint32_t) (filesize / D_FILE_SIZE_DIVIDER); + return snprintf_P(buf, len, PSTR("%u" D_DECIMAL_POINT "%02u " D_FILE_SIZE_GIGABYTES), tmp / 100, + tmp % 100); } uint8_t Parser::get_action_id(const char* action) diff --git a/src/hasp/hasp_parser.h b/src/hasp/hasp_parser.h index d1c44bea..84dcfd1b 100644 --- a/src/hasp/hasp_parser.h +++ b/src/hasp/hasp_parser.h @@ -19,7 +19,7 @@ class Parser { static bool is_true(const char* s); static bool is_true(JsonVariant json); static bool is_only_digits(const char* s); - static int format_bytes(size_t filesize, char* buf, size_t len); + static int format_bytes(uint64_t filesize, char* buf, size_t len); }; #ifndef ARDUINO