From 1a6a417f89c8901dad646efce74ae9d3ddebfd59 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 29 Oct 2025 09:07:20 +0100 Subject: [PATCH 1/2] check boundaries converting float/double to String --- cores/arduino/WString.cpp | 15 +++++++++++++-- cores/arduino/WString.h | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 043fda7c0..17aa3e3ad 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -21,6 +21,13 @@ #include "WString.h" +/*********************************************/ +/* Static Member Initialisation */ +/*********************************************/ + +size_t const String::FLT_MAX_DECIMAL_PLACES; +size_t const String::DBL_MAX_DECIMAL_PLACES; + /*********************************************/ /* Constructors */ /*********************************************/ @@ -107,15 +114,19 @@ String::String(unsigned long value, unsigned char base) String::String(float value, unsigned char decimalPlaces) { + static size_t const FLOAT_BUF_SIZE = 38 + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); - char buf[33]; + char buf[FLOAT_BUF_SIZE]; + decimalPlaces = decimalPlaces < FLT_MAX_DECIMAL_PLACES ? decimalPlaces : FLT_MAX_DECIMAL_PLACES; *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } String::String(double value, unsigned char decimalPlaces) { + static size_t const DOUBLE_BUF_SIZE = 38 + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); - char buf[33]; + char buf[DOUBLE_BUF_SIZE]; + decimalPlaces = decimalPlaces < DBL_MAX_DECIMAL_PLACES ? decimalPlaces : DBL_MAX_DECIMAL_PLACES; *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); } diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h index 2cf4cd747..de2a22977 100644 --- a/cores/arduino/WString.h +++ b/cores/arduino/WString.h @@ -50,6 +50,9 @@ class String typedef void (String::*StringIfHelperType)() const; void StringIfHelper() const {} + static size_t const FLT_MAX_DECIMAL_PLACES = 10; + static size_t const DBL_MAX_DECIMAL_PLACES = FLT_MAX_DECIMAL_PLACES; + public: // constructors // creates a copy of the initial value. From db1b6a0540321cf0f7b9eb4881bcc3cc70849f76 Mon Sep 17 00:00:00 2001 From: pennam Date: Fri, 28 Nov 2025 09:15:45 +0100 Subject: [PATCH 2/2] use FLT_MAX_10_EXP and DBL_MAX_10_EXP instead of hardcoded values --- cores/arduino/WString.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp index 17aa3e3ad..9bd9edad6 100644 --- a/cores/arduino/WString.cpp +++ b/cores/arduino/WString.cpp @@ -20,6 +20,7 @@ */ #include "WString.h" +#include /*********************************************/ /* Static Member Initialisation */ @@ -114,7 +115,7 @@ String::String(unsigned long value, unsigned char base) String::String(float value, unsigned char decimalPlaces) { - static size_t const FLOAT_BUF_SIZE = 38 + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; + static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); char buf[FLOAT_BUF_SIZE]; decimalPlaces = decimalPlaces < FLT_MAX_DECIMAL_PLACES ? decimalPlaces : FLT_MAX_DECIMAL_PLACES; @@ -123,7 +124,7 @@ String::String(float value, unsigned char decimalPlaces) String::String(double value, unsigned char decimalPlaces) { - static size_t const DOUBLE_BUF_SIZE = 38 + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; + static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */; init(); char buf[DOUBLE_BUF_SIZE]; decimalPlaces = decimalPlaces < DBL_MAX_DECIMAL_PLACES ? decimalPlaces : DBL_MAX_DECIMAL_PLACES;